nitter.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. from .common import InfoExtractor
  2. from ..compat import compat_urlparse
  3. from ..utils import (
  4. parse_count,
  5. unified_timestamp,
  6. remove_end,
  7. determine_ext,
  8. )
  9. import re
  10. import random
  11. class NitterIE(InfoExtractor):
  12. # Taken from https://github.com/zedeus/nitter/wiki/Instances
  13. NON_HTTP_INSTANCES = (
  14. '3nzoldnxplag42gqjs23xvghtzf6t6yzssrtytnntc6ppc7xxuoneoad.onion',
  15. 'nitter.l4qlywnpwqsluw65ts7md3khrivpirse744un3x7mlskqauz5pyuzgqd.onion',
  16. 'nitter7bryz3jv7e3uekphigvmoyoem4al3fynerxkj22dmoxoq553qd.onion',
  17. 'npf37k3mtzwxreiw52ccs5ay4e6qt2fkcs2ndieurdyn2cuzzsfyfvid.onion',
  18. 'nitter.v6vgyqpa7yefkorazmg5d5fimstmvm2vtbirt6676mt7qmllrcnwycqd.onion',
  19. 'i23nv6w3juvzlw32xzoxcqzktegd4i4fu3nmnc2ewv4ggiu4ledwklad.onion',
  20. '26oq3gioiwcmfojub37nz5gzbkdiqp7fue5kvye7d4txv4ny6fb4wwid.onion',
  21. 'vfaomgh4jxphpbdfizkm5gbtjahmei234giqj4facbwhrfjtcldauqad.onion',
  22. 'iwgu3cv7ywf3gssed5iqtavmrlszgsxazkmwwnt4h2kdait75thdyrqd.onion',
  23. 'erpnncl5nhyji3c32dcfmztujtl3xaddqb457jsbkulq24zqq7ifdgad.onion',
  24. 'ckzuw5misyahmg7j5t5xwwuj3bwy62jfolxyux4brfflramzsvvd3syd.onion',
  25. 'jebqj47jgxleaiosfcxfibx2xdahjettuydlxbg64azd4khsxv6kawid.onion',
  26. 'nttr2iupbb6fazdpr2rgbooon2tzbbsvvkagkgkwohhodjzj43stxhad.onion',
  27. 'nitraeju2mipeziu2wtcrqsxg7h62v5y4eqgwi75uprynkj74gevvuqd.onion',
  28. 'nitter.lqs5fjmajyp7rvp4qvyubwofzi6d4imua7vs237rkc4m5qogitqwrgyd.onion',
  29. 'ibsboeui2im5o7dxnik3s5yghufumgy5abevtij5nbizequfpu4qi4ad.onion',
  30. 'ec5nvbycpfa5k6ro77blxgkyrzbkv7uy6r5cngcbkadtjj2733nm3uyd.onion',
  31. 'nitter.i2p',
  32. 'u6ikd6zndl3c4dsdq4mmujpntgeevdk5qzkfb57r4tnfeccrn2qa.b32.i2p',
  33. 'nitterlgj3n5fgwesu3vxc5h67ruku33nqaoeoocae2mvlzhsu6k7fqd.onion',
  34. )
  35. HTTP_INSTANCES = (
  36. 'nitter.42l.fr',
  37. 'nitter.pussthecat.org',
  38. 'nitter.nixnet.services',
  39. 'nitter.fdn.fr',
  40. 'nitter.1d4.us',
  41. 'nitter.kavin.rocks',
  42. 'nitter.unixfox.eu',
  43. 'nitter.domain.glass',
  44. 'nitter.eu',
  45. 'nitter.namazso.eu',
  46. 'nitter.actionsack.com',
  47. 'birdsite.xanny.family',
  48. 'nitter.hu',
  49. 'twitr.gq',
  50. 'nitter.moomoo.me',
  51. 'nittereu.moomoo.me',
  52. 'bird.from.tf',
  53. 'nitter.it',
  54. 'twitter.censors.us',
  55. 'twitter.grimneko.de',
  56. 'nitter.alefvanoon.xyz',
  57. 'n.hyperborea.cloud',
  58. 'nitter.ca',
  59. 'twitter.076.ne.jp',
  60. 'twitter.mstdn.social',
  61. 'nitter.fly.dev',
  62. 'notabird.site',
  63. 'nitter.weiler.rocks',
  64. 'nitter.silkky.cloud',
  65. 'nitter.sethforprivacy.com',
  66. 'nttr.stream',
  67. 'nitter.cutelab.space',
  68. 'nitter.nl',
  69. 'nitter.mint.lgbt',
  70. 'nitter.bus-hit.me',
  71. 'fuckthesacklers.network',
  72. 'nitter.govt.land',
  73. 'nitter.datatunnel.xyz',
  74. 'nitter.esmailelbob.xyz',
  75. 'tw.artemislena.eu',
  76. 'de.nttr.stream',
  77. 'nitter.winscloud.net',
  78. 'nitter.tiekoetter.com',
  79. 'nitter.spaceint.fr',
  80. 'twtr.bch.bar',
  81. 'nitter.exonip.de',
  82. 'nitter.mastodon.pro',
  83. 'nitter.notraxx.ch',
  84. # not in the list anymore
  85. 'nitter.skrep.in',
  86. 'nitter.snopyta.org',
  87. )
  88. DEAD_INSTANCES = (
  89. # maintenance
  90. 'nitter.ethibox.fr',
  91. # official, rate limited
  92. 'nitter.net',
  93. # offline
  94. 'is-nitter.resolv.ee',
  95. 'lu-nitter.resolv.ee',
  96. 'nitter.13ad.de',
  97. 'nitter.40two.app',
  98. 'nitter.cattube.org',
  99. 'nitter.cc',
  100. 'nitter.dark.fail',
  101. 'nitter.himiko.cloud',
  102. 'nitter.koyu.space',
  103. 'nitter.mailstation.de',
  104. 'nitter.mastodont.cat',
  105. 'nitter.tedomum.net',
  106. 'nitter.tokhmi.xyz',
  107. 'nitter.weaponizedhumiliation.com',
  108. 'nitter.vxempire.xyz',
  109. 'tweet.lambda.dance',
  110. )
  111. INSTANCES = NON_HTTP_INSTANCES + HTTP_INSTANCES + DEAD_INSTANCES
  112. _INSTANCES_RE = f'(?:{"|".join(map(re.escape, INSTANCES))})'
  113. _VALID_URL = fr'https?://{_INSTANCES_RE}/(?P<uploader_id>.+)/status/(?P<id>[0-9]+)(#.)?'
  114. current_instance = random.choice(HTTP_INSTANCES)
  115. _TESTS = [
  116. {
  117. # GIF (wrapped in mp4)
  118. 'url': f'https://{current_instance}/firefox/status/1314279897502629888#m',
  119. 'info_dict': {
  120. 'id': '1314279897502629888',
  121. 'ext': 'mp4',
  122. 'title': 'md5:7890a9277da4639ab624dd899424c5d8',
  123. 'description': 'md5:5fea96a4d3716c350f8b95b21b3111fe',
  124. 'thumbnail': r're:^https?://.*\.jpg$',
  125. 'uploader': 'Firefox 🔥',
  126. 'uploader_id': 'firefox',
  127. 'uploader_url': f'https://{current_instance}/firefox',
  128. 'upload_date': '20201008',
  129. 'timestamp': 1602183720,
  130. 'like_count': int,
  131. 'repost_count': int,
  132. 'comment_count': int,
  133. },
  134. }, { # normal video
  135. 'url': f'https://{current_instance}/Le___Doc/status/1299715685392756737#m',
  136. 'info_dict': {
  137. 'id': '1299715685392756737',
  138. 'ext': 'mp4',
  139. 'title': 're:^.* - "Je ne prédis jamais rien"\nD Raoult, Août 2020...',
  140. 'description': '"Je ne prédis jamais rien"\nD Raoult, Août 2020...',
  141. 'thumbnail': r're:^https?://.*\.jpg$',
  142. 'uploader': 're:^Le *Doc',
  143. 'uploader_id': 'Le___Doc',
  144. 'uploader_url': f'https://{current_instance}/Le___Doc',
  145. 'upload_date': '20200829',
  146. 'timestamp': 1598711340,
  147. 'view_count': int,
  148. 'like_count': int,
  149. 'repost_count': int,
  150. 'comment_count': int,
  151. },
  152. }, { # video embed in a "Streaming Political Ads" box
  153. 'url': f'https://{current_instance}/mozilla/status/1321147074491092994#m',
  154. 'info_dict': {
  155. 'id': '1321147074491092994',
  156. 'ext': 'mp4',
  157. 'title': 'md5:8290664aabb43b9189145c008386bf12',
  158. 'description': 'md5:9cf2762d49674bc416a191a689fb2aaa',
  159. 'thumbnail': r're:^https?://.*\.jpg$',
  160. 'uploader': 'Mozilla',
  161. 'uploader_id': 'mozilla',
  162. 'uploader_url': f'https://{current_instance}/mozilla',
  163. 'upload_date': '20201027',
  164. 'timestamp': 1603820940,
  165. 'view_count': int,
  166. 'like_count': int,
  167. 'repost_count': int,
  168. 'comment_count': int,
  169. },
  170. 'expected_warnings': ['Ignoring subtitle tracks found in the HLS manifest'],
  171. }, { # not the first tweet but main-tweet
  172. 'url': f'https://{current_instance}/firefox/status/1354848277481414657#m',
  173. 'info_dict': {
  174. 'id': '1354848277481414657',
  175. 'ext': 'mp4',
  176. 'title': 'md5:bef647f03bd1c6b15b687ea70dfc9700',
  177. 'description': 'md5:5efba25e2f9dac85ebcd21160cb4341f',
  178. 'thumbnail': r're:^https?://.*\.jpg$',
  179. 'uploader': 'Firefox 🔥',
  180. 'uploader_id': 'firefox',
  181. 'uploader_url': f'https://{current_instance}/firefox',
  182. 'upload_date': '20210128',
  183. 'timestamp': 1611855960,
  184. 'view_count': int,
  185. 'like_count': int,
  186. 'repost_count': int,
  187. 'comment_count': int,
  188. }
  189. }
  190. ]
  191. def _real_extract(self, url):
  192. video_id, uploader_id = self._match_valid_url(url).group('id', 'uploader_id')
  193. parsed_url = compat_urlparse.urlparse(url)
  194. base_url = f'{parsed_url.scheme}://{parsed_url.netloc}'
  195. self._set_cookie(parsed_url.netloc, 'hlsPlayback', 'on')
  196. full_webpage = webpage = self._download_webpage(url, video_id)
  197. main_tweet_start = full_webpage.find('class="main-tweet"')
  198. if main_tweet_start > 0:
  199. webpage = full_webpage[main_tweet_start:]
  200. video_url = '%s%s' % (base_url, self._html_search_regex(
  201. r'(?:<video[^>]+data-url|<source[^>]+src)="([^"]+)"', webpage, 'video url'))
  202. ext = determine_ext(video_url)
  203. if ext == 'unknown_video':
  204. formats = self._extract_m3u8_formats(video_url, video_id, ext='mp4')
  205. else:
  206. formats = [{
  207. 'url': video_url,
  208. 'ext': ext
  209. }]
  210. title = description = self._og_search_description(full_webpage) or self._html_search_regex(
  211. r'<div class="tweet-content[^>]+>([^<]+)</div>', webpage, 'title', fatal=False)
  212. uploader_id = self._html_search_regex(
  213. r'<a class="username"[^>]+title="@([^"]+)"', webpage, 'uploader id', fatal=False) or uploader_id
  214. uploader = self._html_search_regex(
  215. r'<a class="fullname"[^>]+title="([^"]+)"', webpage, 'uploader name', fatal=False)
  216. if uploader:
  217. title = f'{uploader} - {title}'
  218. counts = {
  219. f'{x[0]}_count': self._html_search_regex(
  220. fr'<span[^>]+class="icon-{x[1]}[^>]*></span>([^<]*)</div>',
  221. webpage, f'{x[0]} count', fatal=False)
  222. for x in (('view', 'play'), ('like', 'heart'), ('repost', 'retweet'), ('comment', 'comment'))
  223. }
  224. counts = {field: 0 if count == '' else parse_count(count) for field, count in counts.items()}
  225. thumbnail = (
  226. self._html_search_meta('og:image', full_webpage, 'thumbnail url')
  227. or remove_end('%s%s' % (base_url, self._html_search_regex(
  228. r'<video[^>]+poster="([^"]+)"', webpage, 'thumbnail url', fatal=False)), '%3Asmall'))
  229. thumbnails = [
  230. {'id': id, 'url': f'{thumbnail}%3A{id}'}
  231. for id in ('thumb', 'small', 'large', 'medium', 'orig')
  232. ]
  233. date = self._html_search_regex(
  234. r'<span[^>]+class="tweet-date"[^>]*><a[^>]+title="([^"]+)"',
  235. webpage, 'upload date', default='').replace('·', '')
  236. return {
  237. 'id': video_id,
  238. 'title': title,
  239. 'description': description,
  240. 'uploader': uploader,
  241. 'timestamp': unified_timestamp(date),
  242. 'uploader_id': uploader_id,
  243. 'uploader_url': f'{base_url}/{uploader_id}',
  244. 'formats': formats,
  245. 'thumbnails': thumbnails,
  246. 'thumbnail': thumbnail,
  247. **counts,
  248. }