vvvvget.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import os
  2. import json
  3. import requests
  4. import sqlite3
  5. ua = "Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.4.2) Gecko/20020502 CS 2000 7.0/7.0"
  6. def ds(h):
  7. g = "MNOPIJKL89+/4567UVWXQRSTEFGHABCDcdefYZabstuvopqr0123wxyzklmnghij"
  8. def f(m):
  9. l = []
  10. o = 0
  11. b = False
  12. m_len = len(m)
  13. while ((not b) and o < m_len):
  14. n = m[o] << 2
  15. o += 1
  16. k = -1
  17. j = -1
  18. if o < m_len:
  19. n += m[o] >> 4
  20. o += 1
  21. if o < m_len:
  22. k = (m[o - 1] << 4) & 255
  23. k += m[o] >> 2
  24. o += 1
  25. if o < m_len:
  26. j = (m[o - 1] << 6) & 255
  27. j += m[o]
  28. o += 1
  29. else:
  30. b = True
  31. else:
  32. b = True
  33. else:
  34. b = True
  35. l.append(n)
  36. if k != -1:
  37. l.append(k)
  38. if j != -1:
  39. l.append(j)
  40. return l
  41. c = []
  42. for e in h:
  43. c.append(g.index(e))
  44. c_len = len(c)
  45. for e in range(c_len * 2 - 1, -1, -1):
  46. a = c[e % c_len] ^ c[(e + 1) % c_len]
  47. c[e % c_len] = a
  48. c = f(c)
  49. d = ''
  50. for e in c:
  51. d += chr(e)
  52. return d
  53. def get_settings():
  54. settings = requests.get('https://www.vvvvid.it/vvvvid/settings', headers={'User-Agent': ua})
  55. if settings.status_code != 200:
  56. return None
  57. return settings.json()['data']['defaultStreamingServer']
  58. def login():
  59. login = requests.get('https://www.vvvvid.it/user/login', headers={'User-Agent': ua})
  60. return login.json()['data']['conn_id']
  61. def get_info(show_id, conn_id):
  62. info = requests.get('https://www.vvvvid.it/vvvvid/ondemand/' + str(show_id) + '/info/?conn_id=' + conn_id, headers={'User-Agent': ua})
  63. info.encoding = 'utf-8'
  64. if info.json()['result'] == 'ok':
  65. return info.json()['data']
  66. else:
  67. return False
  68. def get_seasons(show_id, conn_id):
  69. seasons = requests.get('https://www.vvvvid.it/vvvvid/ondemand/' + str(show_id) + '/seasons/?conn_id=' + conn_id, headers={'User-Agent': ua})
  70. if seasons.json()['result'] == 'ok' and seasons.json()['data'] and seasons.json()['data'][0]['episodes']:
  71. return seasons.json()['data']
  72. else:
  73. return False
  74. def get_episodes(season_id, show_id, conn_id):
  75. episodes = requests.get('https://www.vvvvid.it/vvvvid/ondemand/' + str(show_id) + '/season/' +str(season_id) + '?conn_id=' + conn_id, headers={'User-Agent': ua})
  76. if episodes.json()['result'] == 'ok' and episodes.json()['data'] and episodes.json()['data'][0]['embed_info']:
  77. return episodes.json()['data']
  78. else:
  79. return False
  80. vvvvid_stream_url = get_settings()
  81. if vvvvid_stream_url is None:
  82. print("VVVVID is not available at the moment")
  83. exit(1)
  84. vvvvidb = "vvvvidb.sqlite3"
  85. last = 0
  86. if not os.path.isfile(vvvvidb):
  87. con = sqlite3.connect(vvvvidb)
  88. cur = con.cursor()
  89. cur.execute("CREATE TABLE series (id INTEGER, name TEXT NOT NULL, season_id INTEGER, type TEXT, PRIMARY KEY (id, season_id));")
  90. cur.execute("CREATE TABLE episodes (serie_id INTEGER, season_id INTEGER, cdn_url TEXT NOT NULL, type TEXT NOT NULL);")
  91. con.commit()
  92. con.close()
  93. else:
  94. con = sqlite3.connect(vvvvidb)
  95. cur = con.cursor()
  96. cur.execute("SELECT id FROM series ORDER BY id DESC LIMIT 1;")
  97. rows = cur.fetchall()
  98. if len(rows) > 0:
  99. last = rows[0][0] + 1
  100. con.commit()
  101. con.close()
  102. print("Resuming from...{}".format(last))
  103. con = sqlite3.connect(vvvvidb)
  104. cur = con.cursor()
  105. stream_url = get_settings()
  106. conn_id = login()
  107. for i in range(last, min(last + 500, 1000)):
  108. print("Fetching...{}".format(i))
  109. info = get_info(i, conn_id)
  110. if info:
  111. seasons = get_seasons(i, conn_id)
  112. if seasons:
  113. for j in seasons:
  114. serie = (info['show_id'], info['title'], j['season_id'], j['name'])
  115. print("Found: {}".format(info['title']))
  116. try:
  117. cur.execute("INSERT INTO series (id, name, season_id, type) VALUES (?, ?, ?, ?);", serie)
  118. con.commit()
  119. except sqlite3.IntegrityError:
  120. # serie/stagione gia' presente, salta il fetch degli episodi
  121. continue
  122. eps = []
  123. episodes = get_episodes(j['season_id'], i, conn_id)
  124. if episodes:
  125. for k in episodes:
  126. if k['embed_info']:
  127. if k['video_type'] == 'video/rcs':
  128. embed_info = ds(k['embed_info'])
  129. embed_info = 'https' + embed_info[4:30] + 'i' + embed_info[31:-12] + 'master.m3u8'
  130. elif k['video_type'] == 'video/vvvvid':
  131. embed_info = 'https' + vvvvid_stream_url[4:] + ds(k['embed_info']) + '/playlist.m3u8'
  132. elif k['video_type'] == 'video/youtube':
  133. embed_info = ds(k['embed_info'])
  134. elif k['video_type'] == 'video/kenc':
  135. embed_info = ds(k['embed_info'])
  136. else:
  137. embed_info = ds(k['embed_info'])
  138. eps.append((info['show_id'], j['season_id'], k['video_type'], embed_info))
  139. print("Found {} episodes".format(len(eps)))
  140. try:
  141. cur.executemany("INSERT INTO episodes (serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?);", eps)
  142. con.commit()
  143. except sqlite3.IntegrityError:
  144. # episodi gia' presenti
  145. pass
  146. con.close()