]> git.gir.st - subscriptionfeed.git/blob - app/common/common.py
port yt-dlp#3233 agegate bypass
[subscriptionfeed.git] / app / common / common.py
1 import os
2 import re
3 import json
4 import base64
5 import sqlite3
6 import requests
7 import hmac, hashlib
8 import requests_cache
9 import dateutil.parser
10 from xml.etree import ElementTree
11 from configparser import ConfigParser
12 from datetime import datetime, timezone
13 from urllib.parse import parse_qs, urlparse
14
15 cf = ConfigParser()
16 config_filename = os.environ.get('YT_CONFIG', '/etc/yt/config.ini')
17 cf.read(config_filename)
18 if not 'global' in cf: # todo: full config check
19 raise Exception("Configuration file not found or empty")
20
21 # Note: currently expiring after 10 minutes. googlevideo-urls are valid for 5h59m, but this makes reddit very stale and premiere videos won't start. TODO: exipre when video is livestream/premiere/etc
22 requests_cache.install_cache(backend='memory', expire_after=10*60, allowable_codes=(200,), allowable_methods=('GET', 'HEAD', 'POST'))
23
24 # Note: requests-cache doesn't use redis expiry, so we need this in all backends:
25 # https://github.com/reclosedev/requests-cache/issues/58#issuecomment-164537971
26 # TODO: only run for long-running processes, i.e. the frontend
27 from threading import Timer
28 def purge_cache(sec):
29 requests_cache.remove_expired_responses()
30 t = Timer(sec, purge_cache, args=(sec,))
31 t.setDaemon(True)
32 t.start()
33 purge_cache(10*60)
34
35 # for debugging purposes, monkey patch requests session to store each requests-request in a flask-request's g object (url and response). we can then use a flask error_handler to include the request data in the error log.
36 # since we also call config from outside the flask appcontext, it is wrapped in a try-catch block.
37 from flask import g
38 import requests
39 from requests import Session as OriginalSession
40 class _NSASession(OriginalSession):
41 def request(self, method, url, params=None, data=None, **kwargs):
42 response = super(_NSASession, self).request(
43 method, url, params, data, **kwargs
44 )
45 try:
46 if 'api_requests' not in g:
47 g.api_requests = []
48 g.api_requests.append((url, params, response.text))
49 except RuntimeError: pass # not within flask (e.g. utils.py)
50 return response
51 requests.Session = requests.sessions.Session = _NSASession
52
53 def fetch_xml(feed_type, feed_id):
54 # TODO: handle requests.exceptions.ConnectionError
55 r = requests.get("https://www.youtube.com/feeds/videos.xml", {
56 feed_type: feed_id,
57 })
58 if not r.ok:
59 return None
60
61 return r.content
62
63 def parse_xml(xmldata):
64 ns = {
65 'atom':"http://www.w3.org/2005/Atom",
66 'yt': "http://www.youtube.com/xml/schemas/2015",
67 'media':"http://search.yahoo.com/mrss/",
68 'at': "http://purl.org/atompub/tombstones/1.0",
69 }
70
71 feed = ElementTree.fromstring(xmldata)
72
73 if feed.find('at:deleted-entry',ns):
74 (_,_,vid) = feed.find('at:deleted-entry',ns).get('ref').rpartition(':')
75 return None, None, [{'deleted': True, 'video_id': vid}], None, None
76
77 title = feed.find('atom:title',ns).text
78 author = feed.find('atom:author/atom:name',ns).text \
79 if feed.find('atom:author',ns) else None
80 # for /user/<> endpoint: find out UC-id:
81 # for playlists: this is who created the playlist:
82 try: channel_id = feed.find('yt:channelId',ns).text
83 except:channel_id=None # XXX: why does ternary not work!?
84 # for pullsub: if this exists, we're looking at a playlist:
85 try: playlist_id = feed.find('yt:playlistId',ns).text
86 except:playlist_id=None # XXX: why does ternary not work!?
87 videos = []
88 for entry in feed.findall('atom:entry',ns):
89 videos.append({
90 'video_id': entry.find('yt:videoId',ns).text,
91 'title': entry.find('atom:title',ns).text,
92 'published': entry.find('atom:published',ns).text,
93 'channel_id': entry.find('yt:channelId',ns).text,
94 'author': entry.find('atom:author',ns).find('atom:name',ns).text,
95 # extra fields for pull_subs/webhook:
96 'updated': entry.find('atom:updated',ns).text,
97 })
98
99 return title, author, videos, channel_id, playlist_id
100
101 def update_channel(db, xmldata, from_webhook=False):
102 if not xmldata: return False
103
104 # Note: websub does not return global author, hence taking from first video
105 title, author, videos, channel, playlist = parse_xml(xmldata)
106
107 c = db.cursor()
108 for i, video in enumerate(videos):
109 if video.get('deleted'):
110 # Note: Deletion events are not just fired for actual deletions,
111 # but also for unlisting videos and livestreams that just ended
112 # (even postLiveDVR ones). Hence, we don't follow it.
113 flask_logger(f"ignoring deleted/unlisted/ended video/stream {video['video_id']}")
114 break
115
116 c.execute("SELECT 1 FROM videos WHERE id=?",(video['video_id'],))
117 new_video = len(c.fetchall()) < 1
118 if new_video:
119 # TODO: call store_video_metadata(video_id) here instead and pass video-fallback-metadata to it
120 _, _, meta, _, _ = get_video_info(video['video_id'], metaOnly=True)
121 # The 'published' timestamp sent in websub POSTs are often wrong (e.g.:
122 # video gets uploaded as unlisted on day A and set to public on day B;
123 # the webhook is sent on day B, but 'published' says A. The video
124 # therefore looks like it's just an update to an older video).
125 # g_v_i gives is the date the video was published to viewers, so we
126 # prefer that. But since g_v_i only returns the date without time,
127 # we still use xmlfeed's date if it's the same date.
128 published = dateutil.parser.parse(video['published'])
129 length = None
130 livestream = None
131 premiere = None
132 if meta:
133 meta = video_metadata(meta)
134 published2 = dateutil.parser.parse(meta['published'])
135 if published < published2: # g_v_i date is more accurate:
136 published = published2
137 length = meta['length']
138 livestream = meta['livestream']
139 premiere = meta['premiere']
140
141 now = datetime.now(timezone.utc)
142
143 # we pretend that all videos uploaded this week were uploaded just
144 # now, so the user sees it at the top of the feed, and it doesn't
145 # get inserted somewhere further down.
146 if (now - published).days < 7:
147 timestamp = now
148 else:#, it's just an update to an older video.
149 timestamp = published
150
151 c.execute("""
152 INSERT OR IGNORE INTO videos
153 (id, channel_id, title, length, livestream, premiere, published, crawled)
154 VALUES (?, ?, ?, ?, ?, ?, datetime(?), datetime(?))
155 """, (
156 video['video_id'],
157 video['channel_id'],
158 video['title'],
159 length,
160 livestream,
161 premiere,
162 published,
163 timestamp
164 ))
165 else:
166 # update video title (everything else can't change)
167 c.execute("""
168 UPDATE OR IGNORE videos
169 SET title = ?
170 WHERE id = ?
171 """, (
172 video['title'],
173 video['video_id'],
174 ))
175
176 # for channels, this is obviously always the same, but playlists can
177 # consist of videos from different channels:
178 if i == 0 or playlist:
179 c.execute("""
180 INSERT OR REPLACE INTO channels (id, name)
181 VALUES (?, ?)
182 """, (video['channel_id'], video['author']))
183
184 # keep track of which videos are in a playlist, so we can show the user
185 # why a video is in their feed:
186 if playlist:
187 c.execute("""
188 INSERT OR IGNORE INTO playlist_videos (video_id, playlist_id)
189 VALUES (?, ?)
190 """, (video['video_id'], playlist))
191
192 if playlist and not from_webhook: # Note: playlists can't get updated via websub
193 c.execute("""
194 INSERT OR REPLACE INTO playlists (id, name, author)
195 VALUES (?, ?, ?)
196 """, (playlist, title, channel))
197 c.execute("""
198 INSERT OR REPLACE INTO channels (id, name)
199 VALUES (?, ?)
200 """, (channel, author))
201
202 db.commit()
203
204 return True
205
206 def is_agegated(metadata):
207 playabilityStatus = metadata['playabilityStatus']
208 return bool(
209 playabilityStatus.get("status") == "CONTENT_CHECK_REQUIRED"
210 or playabilityStatus.get("desktopLegacyAgeGateReason")
211 )
212
213 def get_video_info(video_id, *, metaOnly=False, _embed=True):
214 """
215 returns: best-quality muxed video stream, stream map, player_response, error-type/mesage
216 error types: player, malformed, livestream, geolocked, agegated, no-url, exhausted
217 """
218 player_error, metadata = None, None # for 'exhausted'
219 with sqlite3.connect(cf['global']['database']) as conn:
220 c = conn.cursor()
221 c.execute("SELECT * FROM captcha_cookies")
222 cookies = dict(c.fetchall())
223 today = datetime.now(timezone.utc).strftime("%Y%m%d")
224 # XXX: anticaptcha hasn't been adapted
225 key = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
226 r = requests.post("https://www.youtube-nocookie.com/youtubei/v1/player", params={'key': key}, json={
227 'videoId': video_id,
228 'context': {
229 'client': {
230 'gl': 'US',
231 'hl': 'en',
232 # ANDROID returns streams that are not throttled or cipher-scambled, but less metadata than WEB.
233 # TVHTML5* returns throttled and possibly ciphered streams, but bypasses age-gate. we don't decode them currently.
234 'clientName': ('ANDROID' if _embed else 'TVHTML5_SIMPLY_EMBEDDED_PLAYER') if not metaOnly else 'WEB',
235 'clientVersion': (f'16.20' if _embed else '2.0') if not metaOnly else f'2.{today}.01.01',
236 },
237 'thirdParty': {'embedUrl': 'https://www.youtube.com/'}
238 },
239 }, cookies=cookies)
240
241 if not r or r.status_code == 429:
242 return None, None, None, 'banned', 'possible IP ban'
243
244 metadata = r.json()
245 if "error" in metadata:
246 return None, None, metadata, "malformed", metadata.get("error",{}).get("message","")
247 playabilityStatus = metadata['playabilityStatus']['status']
248 if playabilityStatus != "OK":
249 playabilityReason = metadata['playabilityStatus'].get('reason',
250 '//'.join(metadata['playabilityStatus'].get('messages',[])))
251 player_error = f"{playabilityStatus}: {playabilityReason}"
252 if (is_agegated(metadata)
253 and not metaOnly # only need metadata (e.g. called from pubsubhubbub)
254 and _embed # already trying bypass
255 ):
256 _, _, metadata_embed, error_embed, errormsg_embed = get_video_info(video_id, _embed=False)
257 if not error_embed or error_embed in ('livestream','geolocked'):
258 metadata = metadata_embed
259 elif is_agegated(metadata_embed): # agegate bypass failed?
260 return None, None, metadata, 'agegated', player_error
261 else:
262 return None, None, metadata, error_embed, errormsg_embed
263 else:
264 # without videoDetails, there's only the error message
265 maybe_metadata = metadata if 'videoDetails' in metadata else None
266 return None, None, maybe_metadata, 'player', player_error
267
268 # livestreams have no adaptive/muxed formats:
269 is_live = metadata['videoDetails'].get('isLive', False)
270
271 if not 'formats' in metadata['streamingData'] and not is_live:
272 return None, None, metadata, 'no-url', player_error
273
274 formats = metadata['streamingData'].get('formats',[])
275 adaptive = metadata['streamingData'].get('adaptiveFormats',[])
276 stream_map = {
277 'adaptive_video': [a for a in adaptive if a['mimeType'].startswith('video/')],
278 'adaptive_audio': [a for a in adaptive if a['mimeType'].startswith('audio/')],
279 'muxed': formats,
280 'hlsManifestUrl': metadata['streamingData'].get('hlsManifestUrl'),
281 }
282
283 try:
284 url = sorted(formats, key=lambda k: k['height'], reverse=True)[0]['url']
285
286 # ip-locked videos can be recovered if the proxy module is loaded:
287 is_geolocked = 'gcr' in parse_qs(urlparse(url).query)
288 except:
289 url = None
290 is_geolocked = False
291
292 nonfatal = 'livestream' if is_live \
293 else 'geolocked' if is_geolocked \
294 else None
295
296 return url, stream_map, metadata, nonfatal, None
297
298 def video_metadata(metadata):
299 if not metadata:
300 return {}
301
302 meta1 = metadata['videoDetails']
303 # With ANDROID player API, we don't get microformat => no publishDate!
304 meta2 = metadata.get('microformat',{}).get('playerMicroformatRenderer',{})
305
306 # sometimes, we receive the notification so early that the length is not
307 # yet populated. Nothing we can do about it.
308 length = int(meta2.get('lengthSeconds',0)) or int(meta1.get('lengthSeconds',0)) or None
309
310 scheduled_time = metadata.get('playabilityStatus',{}) \
311 .get('liveStreamability',{}).get('liveStreamabilityRenderer',{}) \
312 .get('offlineSlate',{}).get('liveStreamOfflineSlateRenderer',{}) \
313 .get('scheduledStartTime')
314 if scheduled_time:
315 scheduled_time = datetime.fromtimestamp(int(scheduled_time)) \
316 .strftime("%Y-%m-%dT%H:%M:%SZ")
317 published_at = (
318 meta2.get('liveBroadcastDetails',{}) .get('startTimestamp') or
319 scheduled_time or
320 f"{meta2.get('publishDate','1970-01-01')}T00:00:00Z"
321 )
322
323 # Note: 'premiere' videos have livestream=False and published= will be the
324 # start of the premiere.
325 return {
326 'title': meta1['title'],
327 'author': meta1['author'],
328 'channel_id': meta1['channelId'],
329 'published': published_at,
330 'views': int(meta1['viewCount']),
331 'length': length,
332 'livestream': meta1['isLiveContent'],
333 'premiere': meta1.get('isUpcoming') and not meta1['isLiveContent'],
334 }
335
336 def store_video_metadata(video_id):
337 # check if we know about it, and if not, fetch and store video metadata
338 with sqlite3.connect(cf['global']['database']) as conn:
339 c = conn.cursor()
340 c.execute("SELECT 1 from videos where id = ?", (video_id,))
341 new_video = len(c.fetchall()) < 1
342 if new_video:
343 _, _, meta, _, _ = get_video_info(video_id, metaOnly=True)
344 if meta:
345 meta = video_metadata(meta)
346 c.execute("""
347 INSERT OR IGNORE INTO videos (id, channel_id, title, length, livestream, premiere, published, crawled)
348 VALUES (?, ?, ?, ?, ?, ?, datetime(?), datetime(?))
349 """, (
350 video_id,
351 meta['channel_id'],
352 meta['title'],
353 meta['length'],
354 meta['livestream'],
355 meta['premiere'],
356 meta['published'],
357 meta['published'],
358 ))
359 c.execute("""
360 INSERT OR REPLACE INTO channels (id, name)
361 VALUES (?, ?)
362 """, (meta['channel_id'], meta['author']))
363
364 def fetch_video_flags(token, video_ids):
365 with sqlite3.connect(cf['global']['database']) as conn:
366 c = conn.cursor()
367 c.execute("""
368 SELECT video_id,display
369 FROM flags
370 WHERE user = ?
371 AND display IS NOT NULL
372 AND video_id IN ({})
373 -- AND display = 'pinned'
374 """.format(",".join(["?"]*len(video_ids))), (token,*video_ids))
375 flags = c.fetchall()
376 pinned = [video for video,disp in flags if disp == 'pinned']
377 hidden = [video for video,disp in flags if disp == 'hidden']
378
379 return pinned, hidden
380
381 from werkzeug.exceptions import NotFound
382 class NoFallbackException(NotFound): pass
383 def fallback_route(*args, **kwargs): # TODO: worthy as a flask-extension?
384 """
385 finds the next route that matches the current url rule, and executes it.
386 args, kwargs: pass all arguments of the current route
387 """
388 from flask import current_app, request, g
389
390 # build a list of endpoints that match the current request's url rule:
391 matching = [
392 rule.endpoint
393 for rule in current_app.url_map.iter_rules()
394 if rule.rule == request.url_rule.rule
395 ]
396 current = matching.index(request.endpoint)
397
398 # since we can't change request.endpoint, we always get the original
399 # endpoint back. so for repeated fall throughs, we use the g object to
400 # increment how often we want to fall through.
401 if not '_fallback_next' in g:
402 g._fallback_next = 0
403 g._fallback_next += 1
404
405 next_ep = current + g._fallback_next
406
407 if next_ep < len(matching):
408 return current_app.view_functions[matching[next_ep]](*args, **kwargs)
409 else:
410 raise NoFallbackException
411
412 def websub_url_hmac(key, feed_id, timestamp, nonce):
413 """ generate sha1 hmac, as required by websub/pubsubhubbub """
414 sig_input = f"{feed_id}:{timestamp}:{nonce}".encode('ascii')
415 return hmac.new(key.encode('ascii'), sig_input, hashlib.sha1).hexdigest()
416
417 def websub_body_hmac(key, body):
418 return hmac.new(key.encode('ascii'), body, hashlib.sha1).hexdigest()
419
420 def flask_logger(msg, level="warning"):
421 level = dict(
422 CRITICAL=50,
423 ERROR=40,
424 WARNING=30,
425 INFO=20,
426 DEBUG=10,
427 NOTSET=0,
428 ).get(level.upper(), 0)
429 try:
430 from flask import current_app
431 current_app.logger.log(level, msg)
432 except:
433 pass
434
435 def pp(*args):
436 from pprint import pprint
437 import sys, codecs
438 pprint(args, stream=codecs.getwriter("utf-8")(sys.stderr.buffer))
Imprint / Impressum