-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·54 lines (45 loc) · 1.49 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
import urllib
import xmltodict
import unicodecsv
from bs4 import BeautifulSoup
import json
FEED_URL = 'https://feeds.feedburner.com/thetimferrissshow'
FEED_FILE = 'rss.xml'
CSV_FILE = 'extract.csv'
# AUDIO_BOOK_PATTERN = '<a href= "http://www.audible.com'
findings = {}
print "Fetching XML"
urllib.urlretrieve (FEED_URL, FEED_FILE)
with open(FEED_FILE) as fd:
doc = xmltodict.parse(fd.read())
for item in doc['rss']['channel']['item']:
findings[item['title']] = item['description']
soup = BeautifulSoup(item['description'], "html.parser")
#print(soup.prettify())
findings[item['title']] = []
link_enum = 0
for link in soup.find_all('a'):
if link.get('href').find("amazon") > 1:
findings[item['title']][link_enum] = {}
findings[item['title']][link_enum]['link'] = link.get('href')
findings[item['title']][link_enum]['type'] = 'Book?'
link_enum += 1
#print link.get('href')
if link.get('href').find('audible') > 1:
print link
#print link.get['href'
findings[item['title']][link_enum] = {}
findings[item['title']][link_enum]['link'] = link.get('href')
findings[item['title']][link_enum]['type'] = 'Audiobook?'
link_enum += 1
#print link.get('href')
with open(CSV_FILE, 'w') as csvfile:
fieldnames = ['Episode', 'Description']
writer = unicodecsv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for ep in findings:
#print ep
writer.writerow({'Episode': ep})
#if removing the below, remove import
# print json.dumps(findings)