-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
157 lines (122 loc) · 3.72 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
156
157
import argparser
import logging
import sys
import urllib
import requests
from error import BadUserInputError
from registry import Registry
from main import (
play_episode,
list_episodes,
list_subscriptions,
subscribe_to_podcast,
search_for_podcast,
)
from lib import (
initialize_subscription,
get_all_subscriptions,
)
from episodesynchronizer import EpisodeSynchronizer
from downloader import Downloader
from downloader import fetch
from filesystem import FileSystem
from prefs import get_subscription_names
from report import doreport
from subscription import Subscription
from mediaplayer import MediaPlayer
from podcasts import PodcastsAPI
def main():
p = argparser.argparser()
args = p.parse_args(sys.argv[1:])
if args.search:
dosearch(args)
elif args.subscribe:
dosubscribe(args)
elif args.play:
doplay(args)
elif args.update:
doupdate(args)
elif args.listsubscriptions:
dolistsubscriptions(args)
elif args.listepisodes:
dolistepisodes(args)
elif args.download:
dodownload(args)
elif args.report:
doreport("report.html")
if "verbose" in args:
logger = logging.getLogger()
logger.setLevel(logging.INFO)
return
def dosearch(args):
print(search_for_podcast(args))
def dosubscribe(args):
try:
subscribe_to_podcast(args, Registry(), PodcastsAPI())
except BadUserInputError as e:
print(e.msg())
def doplay(args):
try:
fs = FileSystem()
player = MediaPlayer()
subs = get_all_subscriptions()
play_episode(fs, player, args, subs)
except BadUserInputError as e:
print(e.msg())
def dolistepisodes(args):
try:
fs = FileSystem()
subs = get_all_subscriptions()
print(list_episodes(fs, args, subs))
except BadUserInputError as e:
print(e.msg())
def doupdate(args):
names = get_subscription_names()
try:
index = int(args.update) - 1
name = names[index]
download_rss_file(name, args)
except ValueError:
if args.update == "all":
for name in names:
download_rss_file(name, args)
def dolistsubscriptions(args):
subs = get_all_subscriptions()
print(list_subscriptions(subs))
def dodownload(args):
names = get_subscription_names()
if args.download[0] == "all":
for name in names:
download_subscription_by_name(name, args)
else:
i = int(args.download[0]) - 1
name = names[i]
download_subscription_by_name(name, args)
def download_subscription_by_name(name, args):
try:
subscription = download_rss_file(name, args)
db = subscription.get_db()
filesystem = FileSystem()
downloader = Downloader(filesystem, subscription, args)
es = EpisodeSynchronizer(filesystem, subscription, downloader)
es.dodownload(db)
except urllib.error.URLError as e:
logging.info("app.py catching URLError: \n %s\n\n" % str(e))
except requests.exceptions.HTTPError as e:
logging.info("app.py catching HTTPError: \n %s\n\n" % str(e))
except requests.exceptions.RequestException as e:
logging.info("app.py catching RequestException: \n %s\n\n" % str(e))
def download_rss_file(name, args):
sub = Subscription()
initialize_subscription(sub, name)
fetch(sub.feedurl, sub.rssfile, sub.title, args)
return sub
def print_debug(episodes, subscription, db, rssfile):
for e in episodes[0 : subscription.maxeps]:
print("guid:" + e.guid)
print("url:" + e.url)
print("title:" + e.title)
print("basename:" + e.basename())
print("\n\n")
if __name__ == "__main__":
main()