-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytPlaylists.py
286 lines (225 loc) · 9.3 KB
/
ytPlaylists.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import sqlite3
from urllib.request import urlopen
import bs4 as BeautifulSoup
import os
from difflib import SequenceMatcher
from datetime import datetime
def sanitize(filename):
ret = filename
ret = ret.replace("/", "_")
ret = ret.replace("\\", "_")
ret = ret.replace("\n", "_")
ret = ret.replace("!", "_")
ret = ret.replace("?", "_")
ret = ret.replace(":", "_")
ret = ret.replace("&", "_")
ret = ret.replace("|", "_")
ret = ret.replace("[", "_")
ret = ret.replace("]", "_")
ret = ret.replace("__", "_")
ret = ret.replace("_ _", "_")
ret = ret.replace("__", "_")
return ret
class FileExplorer(object):
def __init__(self, folder_path):
"""The folder must contain only the videos"""
self.folder = folder_path
self.files = self._setAllFiles(folder_path)
def _setAllFiles(self, folder_path):
f = []
for (dirpath, dirnames, filenames) in os.walk(folder_path):
filenames = [filename for filename in filenames if "srt" not in filename and "m3u" not in filename]
f.extend(filenames)
break
return f
def similar(self, str1, str2):
a = sanitize(str1)
b = sanitize(str2)
return SequenceMatcher(None, a, b).ratio()
def getSimilarityDictionary(self, title_sequence):
ret = {}
for (title, url) in title_sequence:
max_similarity = -1
out_file = None
for file in self.files:
similarity = self.similar(title, file)
if similarity > max_similarity:
max_similarity = similarity
out_file = file
ret[title] = out_file
return ret
class m3uMaker(object):
def __init__(self, videos, similarity_dict, folder_path, playlist_name, date=True):
self.videos = videos
self.similarity_dict = similarity_dict
self.folder_path = folder_path
self.playlist_name = sanitize(playlist_name)
self.date = ""
if date:
d = datetime.today()
self.date = str(d.year) + str(d.month) + str(d.day)
def generate(self):
files_ok = []
with open(self.folder_path + "/" + self.playlist_name + self.date + ".m3u", 'w', encoding='utf-8') as f:
for (video_title, video_url) in self.videos:
try:
real_name = self.similarity_dict[video_title]
except:
real_name = video_title
if real_name:
f.write(real_name)
f.write("\n")
files_ok.append(real_name)
return files_ok
class FourKVideoDB(object):
def __init__(self, path):
self.db = path
def getAllChainWithPath(self, asDict=True):
with sqlite3.connect(self.db) as conn:
c = conn.cursor()
query = """
SELECT sub.query as chain_url, sub.title as chain_title, subs.path
FROM subscriptions as sub
INNER JOIN subscription_settings as subs
ON subs.subscriptionId = sub.id;
"""
rows = c.execute(query)
ret = rows
if asDict:
ret = {}
for row in rows:
chain_url = row[0]
chain_title = row[1]
hdd_path = row[2]
ret[chain_title] = (
self.chainToPlaylist(chain_url),
hdd_path
)
return ret
def getChains(self, asDict=True):
with sqlite3.connect(self.db) as conn:
c = conn.cursor()
query = "SELECT id, query, title, lastReferenceStr, lastTimestamp FROM subscriptions;"
rows = c.execute(query)
ret = rows
if asDict:
ret = {}
for row in rows:
id = row[0]
query = row[1]
title = row[2]
lastReferenceStr = row[3]
lastTimestamp = row[4]
ret[title] = {
'id': id,
'query': self.chainToPlaylist(query),
'lastReferenceStr': lastReferenceStr,
'lastTimestamp': lastTimestamp,
}
return ret
def getAllPlaylistsUrls(self):
ret = {}
chains = self.getChains()
for key in chains.keys():
ret[key] = chains[key]['query']
return ret
def getVideosLinksBySubscription(self):
with sqlite3.connect(self.db) as conn:
c = conn.cursor()
query = "SELECT id, subscriptionId, reference FROM subscription_elements;"
rows = c.execute(query)
return rows
def getSubscriptionVideos(self, asDict=True):
with sqlite3.connect(self.db) as conn:
c = conn.cursor()
query = """
SELECT reference, title, lastTimestamp, query
FROM subscription_elements as sube
INNER JOIN subscriptions as sub
ON sube.subscriptionId = sub.id
"""
rows = c.execute(query)
ret = rows
if asDict:
ret = {}
for row in rows:
video = row[0]
chain = row[1]
lastTimestamp = row[2]
chain_url = row[3]
if not chain in ret.keys():
ret[chain] = []
ret[chain].append({
'video': video,
'chain_url': chain_url,
'lastTimestamp': lastTimestamp,
})
return ret
def chainToPlaylist(self, chain_url):
chain_url = chain_url.replace('/feed', '')
chain_url = chain_url.replace('/videos', '')
chain_url = chain_url.replace('/featured', '')
chain_url += "/playlists"
chain_url = chain_url.replace("//playlists", "/playlists")
return chain_url
class YTPlaylist(object):
def __init__(self, fourK_database_path):
self.fk = FourKVideoDB(fourK_database_path)
def run(self):
chains = self.fk.getAllChainWithPath()
for chain_title in chains.keys():
print("Doing: " + chain_title)
chain_url = chains[chain_title][0]
folder_path = chains[chain_title][1]
self.processChain(chain_url, folder_path)
def processChain(self, chain_url, folder_path):
playlists = self.getPlaylists(chain_url)
fe = FileExplorer(folder_path)
files_in_playlist = self.filesInPlaylistInit(fe.files)
for playlist in playlists:
playlist_title = playlist[0]
playlist_url = playlist[1]
print("--> " + playlist_title)
videos = self.getVideosNameOrdered(playlist_url)
similarity_dict = fe.getSimilarityDictionary(videos)
m3u = m3uMaker(videos, similarity_dict, folder_path, playlist_title)
files_ok = m3u.generate()
for filename in files_ok:
files_in_playlist[filename] = True
videos_not_in_playlist = []
for key in files_in_playlist.keys():
if not files_in_playlist[key]:
videos_not_in_playlist.append((key, "garbage"))
if len(videos_not_in_playlist) > 0:
m3u = m3uMaker(videos_not_in_playlist, None, folder_path, "NOT_CLASSIFIED")
m3u.generate()
return playlists
def filesInPlaylistInit(self, files):
ret = {}
for f in files:
ret[f] = False
return ret
def getVideosNameOrdered(self, playlist_url):
html = urlopen(playlist_url).read()
soup = BeautifulSoup.BeautifulSoup(html, "lxml")
links = soup.find_all('a', attrs={'class':'pl-video-title-link'})
ret = []
for link in links:
ret.append((sanitize(link.string), link['href']))
return ret
def getPlaylists(self, chain_url):
html = urlopen(chain_url).read()
soup = BeautifulSoup.BeautifulSoup(html, "lxml")
links = soup.find_all('a', attrs={'class': 'yt-uix-tile-link'})
ret = []
for link in links:
title = link.string
ret.append((title, "https://www.youtube.com" + link['href']))
return ret
##browse-items-primary > li:nth-child(3) > div.feed-item-dismissable > div > div > div > div.multirow-shelf.shelf-item.c4-visible-on-hover-container.yt-section-hover-container > ul > li:nth-child(1) > div > div.yt-lockup-dismissable > div.yt-lockup-content > h3 > a
#<a class="yt-uix-sessionlink yt-uix-tile-link spf-link yt-ui-ellipsis yt-ui-ellipsis-2" dir="ltr" title="Level 2 Tai Chi Test Review" aria-describedby="description-id-262410" data-sessionlink="ei=vVktWeDxF4br1gK0wZKYCQ&ved=CGcQ2yoYACITCKDEyPTDl9QCFYa1VQodtKAEkyibHA" href="/playlist?list=PLrNPUBAMZb8UccPNV6woSHhV2yCjpM1VN">Level 2 Tai Chi Test Review</a>
if __name__ == "__main__":
root = "C:/Users/Administrator/AppData/Local/4kdownload.com/4K Video Downloader/4K Video Downloader/"
db = "database.sqlite"
ytp = YTPlaylist(root + db)
ytp.run()