-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone-shot-feed-parser.py
145 lines (108 loc) · 4.17 KB
/
one-shot-feed-parser.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
import os
import codecs
import logging
import logging.handlers
import feedparser
import wikiatools
import episodeparsers
DEBUG = True
reload(sys)
sys.setdefaultencoding('utf-8')
file_output = '../core/userfiles/new_pages'
log_file = 'logs/episode_feed.log'
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
sys.stderr = codecs.getwriter('utf-8')(sys.stderr)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if DEBUG:
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s == %(message)s', '%y/%m/%d-%H:%M:%S')
filehandler = logging.handlers.TimedRotatingFileHandler(log_file, when='midnight', backupCount=7)
filehandler.setFormatter(formatter)
logger.addHandler(filehandler)
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(formatter)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
logger = logging.getLogger('episode feed')
def main():
logger.info('Main')
# ?paged=2 to go back pages
oneshot_rss = 'http://www.oneshotpodcast.com/feed/'
# clear episode file
logger.info('clear episode file')
wikiatools.clear_new_pages()
if os.path.isfile('.oneshot.etag') and not DEBUG:
with open('.oneshot.etag') as f:
etag = f.read()
logger.info('etag exists: %s', etag)
feed = feedparser.parse(oneshot_rss, etag=etag)
else:
feed = feedparser.parse(oneshot_rss)
logger.info('Feed status: %s', feed.status)
if feed.status == 304:
logger.info('No new episodes quiting')
return
if feed.status == 500:
logger.warning('Server error quiting')
return
# process episodes
commands = get_episodes(feed)
logger.info('Commands')
logger.info(commands)
logger.info('sending pages to wiki')
if not DEBUG:
wikiatools.post_pages()
for c in commands:
wikiatools.run_command(c)
with open('.oneshot.etag', 'w') as f:
logger.info('writing etag: %s' % feed.etag)
if not DEBUG:
f.write(feed.etag)
logger.info('Finished')
def get_episodes(feed):
logger.info('get episodes')
commands = []
for f in reversed(feed.entries):
podcast = f.links[0].href.split('.com/podcasts/')[1].split('/')[0]
# format to correct url based on podcast
# ONE-SHOT
if podcast == 'one-shot' or any(t.term == 'One Shot' for t in f.tags):
logger.info('One Shot Episode')
feed_episode = episodeparsers.OneShotParser(f)
# CAMPAIGN
elif podcast == 'campaign' or any(t.term == 'Campaign' for t in f.tags):
logger.info('Campaign Episode')
feed_episode = episodeparsers.CampaignParser(f)
# First WATCH
elif podcast == 'first-watch' or any(t.term == 'First Watch' for t in f.tags):
logger.info('First Watch Episode')
feed_episode = episodeparsers.FirstWatchParser(f)
# CRITICAL SUCCESS
elif podcast == 'critical-success' or any(t.term == 'Critical Success' for t in f.tags):
logger.info('Critical Success Episode')
feed_episode = episodeparsers.CriticalSuccessParser(f)
# BACKSTORY
elif podcast == 'backstory' or any(t.term == 'Backstory' for t in f.tags):
logger.info('Backstory Episode')
feed_episode = episodeparsers.BackstoryParser(f)
# MODIFIER
elif podcast == 'modifier' or any(t.term == 'Modifier' for t in f.tags):
logger.info('Modifier Episode')
feed_episode = episodeparsers.ModifierParser(f)
# TALKING-TABLETOP
elif podcast == 'talking-table-top' or any(t.term == 'Talking Table Top' for t in f.tags):
logger.info('Talking TableTop Episode')
feed_episode = episodeparsers.TalkingTableTopParser(f)
# UNKNOWN PODCAST
else:
logger.warning('UNKNOWN PODCAST: %s', podcast)
continue
commands.extend(feed_episode.commands)
logger.debug(feed_episode.wiki_content())
wikiatools.write_page(title=feed_episode.wiki_page, content=feed_episode.wiki_content())
return commands
main()