-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPodcastEasy.py
153 lines (112 loc) · 4.22 KB
/
PodcastEasy.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
#qpy:qpyapp
#Podcast Easy 0.7 (06.05.2018) by r57zone
#http://r57zone.github.io
#QPython3
import urllib.request, re, os, androidhelper
droid = androidhelper.Android()
def HTTPGet(Url):
try:
Responce = urllib.request.urlopen(Url)
Source = str(Responce.read())
except:
Source = ''
return Source
def DownloadFile(Url, Path):
FileName = os.path.splitext(Url.split('/')[-1])[0]
FileExt = os.path.splitext(Url)[1]
if os.path.exists(Path + FileName + FileExt):
FileExistsCounter = 0
while True:
FileExistsCounter += 1
try:
if not os.path.exists(Path + FileName + '(' + str(FileExistsCounter) + ')' + FileExt):
DownloadFileName = Path + FileName + '(' + str(FileExistsCounter) + ')' + FileExt
break
except:
pass
else:
DownloadFileName = Path + FileName + FileExt
try:
urllib.request.urlretrieve(Url, DownloadFileName)
return True
except:
return False
def main():
#====================
#Настройки / Settings
#--------------------
DownloadPath = '/sdcard/Podcasts/'
#--------------------
#Загружать подкасты / Download podcasts
#After adding a new feed is recommended to skip downloading all new podcasts.
DownloadPodcasts = False
#DownloadPodcasts = True
#--------------------
#Расширения подкастов / Podcasts extensions
PodcastExt = 'mp3|aac|ogg|mp4';
#=====================
os.chdir(os.path.dirname(os.path.abspath(__file__)))
RSSList = open('RSS.txt', 'r').readlines()
RSSCount = len(RSSList)
Downloaded = open('Downloaded.txt', 'r').read()
#Список для загрузки файлов / List to download files
Download = []
droid.dialogCreateHorizontalProgress('Podcast Easy', 'Checking news feeds', RSSCount)
droid.dialogShow()
for i, Address in enumerate(RSSList):
droid.dialogSetCurrentProgress(i + 1)
#Лента / Rss
Source = HTTPGet(Address)
if Source == '':
continue
#Atom
Links = re.findall('<content.*?src="(.*?' + PodcastExt + ')"', Source)
#RSS 2.0
Links += re.findall('<enclosure.*?url="(.*?' + PodcastExt +')"', Source)
for Link in Links:
#Проверяем добавлялась ли ссылка в список загрузки / Checking if the link was added to the download list
if not Link in Download:
#Проверяем была ли загружена ссылка ранее / Checking if the link was previously downloaded
if not Link in Downloaded:
droid.makeToast('Found new podcast on ' + Address.replace("\n",''))
#Добавление ссылки в список для загрузки / Add link to download list
Download.append(Link)
droid.dialogDismiss()
DownloadedFile=open('Downloaded.txt', 'a')
#Счетчик загруженных файлов / Counter donwloaded files
DownloadedCount=0
#Счетчик ошибок загрузки / Counter error downloaded
ErrorCount=0
droid.dialogCreateHorizontalProgress('Podcast Easy', 'Podcast downloaded', len(Download))
droid.dialogShow()
if Download:
#Загрузка файлов / Download files
for Link in Download:
if DownloadPodcasts:
if DownloadFile(Link, DownloadPath):
#Сохранение ссылок на загруженные подкасты, чтобы не загружать их снова / Save links to downloaded podcasts to not download them again
DownloadedFile.write(Link + "\n")
DownloadedCount += 1
droid.dialogSetCurrentProgress(DownloadedCount)
else:
ErrorCount += 1
else:
DownloadedFile.write(Link + "\n")
DownloadedCount = 1
droid.dialogDismiss()
if not ErrorCount and DownloadedCount:
if DownloadPodcasts:
droid.makeToast('All podcasts downloaded')
else:
droid.makeToast('All podcasts skipped')
else:
if not (ErrorCount and DownloadedCount):
droid.makeToast('New podcasts not found')
else:
droid.makeToast('Download error, downloaded podcasts: {}'.format(DownloadedCount))
DownloadedFile.close()
#Предупреждение / Warning
if not DownloadPodcasts:
droid.makeToast('Downloading new podcasts is disabled. To enabled download, change parameter "DownloadPodcasts" to "True" in the code.')
if __name__=='__main__':
main()