I'm missing something and its driving me bananas,. For some reason, the lookup to the aliases
list of dicts is not working.
import urllib.parse
def parse_source_from_url(url):
# Clean up the url
parse_url = urllib.parse.urlparse(url.strip().replace('\n', ''))
# Parse the interesting parts
url = parse_url.netloc.split('.')
# Words to omit
keywords = ['feeds', 'com']
# Determine which url words are not keywords to omit
feed_source = [u for u in url if u not in keywords][0]
# Map out aliases as needed
aliases = [
{'feed_source': 'example', 'alias': 'good'},
{'feed_source': 'feedburner', 'alias': 'notgood'}]
# Select alias if feed_source is shown in list of dict
# This is where the problem is
feed_source = [
a['alias']
if a['feed_source'] == feed_source
else feed_source
for a in aliases]
# Return only the aliased name
return feed_source[0]
urls = ['https://feeds.example.com/', 'https://feeds.feedburner.com/']
for u in urls:
test = parse_source_from_url(u)
print(test)
Results:
- The first result is correct ("example" aliased "good") but the second URL is incorrect.
- "feedburner" should be aliased as "notgood"
good
feedburner
Tried:
- The method here using
next()
and theget()
methods. - Same issue