Skip to content

Instantly share code, notes, and snippets.

@henryjfry
Created November 13, 2020 19:31
Show Gist options
  • Save henryjfry/e7d096686c5a8ad1fde3affaf568f612 to your computer and use it in GitHub Desktop.
Save henryjfry/e7d096686c5a8ad1fde3affaf568f612 to your computer and use it in GitHub Desktop.
kodi_rd_prescrape_playlist.py
#!/usr/bin/python
import requests
import json
import time
import sys
import PTN
import re
regex = re.compile('[^a-zA-Z]')
try:
import xbmc, xbmcgui, xbmcplugin
xbmc_plugin = 'True'
xbmcgui.Window(10000).clearProperty('Next_EP.ResolvedUrl')
xbmcgui.Window(10000).clearProperty('Next_EP.Url')
xbmcgui.Window(10000).clearProperty('Next_EP.poster')
xbmcgui.Window(10000).clearProperty('Next_EP.fanart')
xbmcgui.Window(10000).clearProperty('Next_EP.clearlogo')
xbmcgui.Window(10000).clearProperty('Next_EP.landscape')
xbmcgui.Window(10000).clearProperty('Next_EP.banner')
xbmcgui.Window(10000).clearProperty('Next_EP.thumb')
xbmcgui.Window(10000).clearProperty('Next_EP.duration')
xbmcgui.Window(10000).clearProperty('Next_EP.dbid')
xbmcgui.Window(10000).clearProperty('Next_EP.dbtype')
xbmcgui.Window(10000).clearProperty('Next_EP.genre')
xbmcgui.Window(10000).clearProperty('Next_EP.imdb')
xbmcgui.Window(10000).clearProperty('Next_EP.icon')
xbmcgui.Window(10000).clearProperty('Next_EP.label')
xbmcgui.Window(10000).clearProperty('Next_EP.label2')
xbmcgui.Window(10000).clearProperty('Next_EP.originaltitle')
xbmcgui.Window(10000).clearProperty('Next_EP.plot')
xbmcgui.Window(10000).clearProperty('Next_EP.plotoutline')
xbmcgui.Window(10000).clearProperty('Next_EP.premiered')
xbmcgui.Window(10000).clearProperty('Next_EP.rating')
xbmcgui.Window(10000).clearProperty('Next_EP.tv_show_title')
xbmcgui.Window(10000).clearProperty('Next_EP.title')
xbmcgui.Window(10000).clearProperty('Next_EP.year')
xbmcgui.Window(10000).clearProperty('Next_EP.resumetime')
except:
xbmc_plugin = 'False'
tmdb_api = 'tmdb_api_key'
fanart_api = 'fanart_api_key'
RD_api = 'RD_api_key'
#show_title = 'Star Trek: Deep Space Nine'
#show_season = 1
#show_episode = 16
for i in sys.argv[1:]:
if 'show' in i:
show_title = str(i.replace('show=','').replace('"',''))
if 'season' in i:
show_season = int(i.replace('season=',''))
if 'episode' in i:
show_episode= int(i.replace('episode=',''))
show_title_clean = regex.sub(' ', show_title).replace(' ',' ').lower()
header = {'Authorization': 'Bearer ' + RD_api}
downloads_fail = 'False'
torrents_fail = 'False'
download_found = 0
torrent_found = 0
for x in range(999):
params = {'page': x+1}
try:
if downloads_fail == 'False' and download_found == 0:
response = requests.get('https://api.real-debrid.com/rest/1.0/downloads', headers=header, params=params)
data = response.json()
except:
downloads_fail = 'True'
pass
if downloads_fail == 'False' and download_found == 0:
for i in data:
ptn_data = PTN.parse(i['filename'])
PTN_title = regex.sub(' ', ptn_data['title']).replace(' ',' ').lower()
try: PTN_season = ptn_data['season']
except: break
PTN_episode = ptn_data['episode']
try: PTN_res = ptn_data['resolution']
except: PTN_res = ''
PTN_link = i['link']
PTN_download = i['download']
PTN_size = str(int(i['filesize'])/(1024*1024))+'Mb'
if PTN_title == show_title_clean and int(PTN_season) == show_season and int(PTN_episode)== show_episode:
# print PTN_title
# print PTN_season
# print PTN_episode
# print PTN_res
# print PTN_link
# print PTN_size
# print PTN_download
download_found = 1
break
if torrent_found == 1 or download_found == 1:
break
try:
if torrents_fail == 'False' and torrent_found == 0:
response2 = requests.get('https://api.real-debrid.com/rest/1.0/torrents', headers=header, params=params)
data2 = response2.json()
except:
torrents_fail = 'True'
pass
if torrents_fail == 'False' and torrent_found == 0:
for k in data2:
if torrent_found == 0:
torr_response = requests.get('https://api.real-debrid.com/rest/1.0/torrents/instantAvailability/' + k['hash'], headers=header)
torr_data = torr_response.json()
if torrent_found == 1:
break
try:
list1 = sorted(torr_data[k['hash']]['rd'][0])
list1 = sorted(list1, key = lambda z: int(z))
y = 0
for j in list1:
PTN_size = str(torr_data[k['hash']]['rd'][0][j]['filesize']/(1024*1024)) + 'Mb'
ptn_data = PTN.parse(torr_data[k['hash']]['rd'][0][j]['filename'])
PTN_title = regex.sub(' ', ptn_data['title']).replace(' ',' ').lower()
try:
PTN_season = ptn_data['season']
except :
if 'txt' in PTN_title:
pass
else:
break
PTN_episode = ptn_data['episode']
PTN_res = ptn_data['resolution']
if PTN_title == show_title_clean and int(PTN_season) == show_season and int(PTN_episode)== show_episode:
# print PTN_size
# print PTN_title
# print PTN_season
# print PTN_episode
# print PTN_res
# print k['filename']
PTN_link = k['links'][y]
# print PTN_link
params2 = {'link': PTN_link}
torr_link = requests.post('https://api.real-debrid.com/rest/1.0/unrestrict/link', headers=header, data=params2)
torr_unrestricted = torr_link.json()
PTN_download = torr_unrestricted['download']
# print PTN_download
torrent_found = 1
break
y = y +1
if PTN_title == show_title_clean and int(PTN_season) == show_season and int(PTN_episode)== show_episode:
break
except:
pass
if torrents_fail == 'True' and downloads_fail == 'True':
break
if torrent_found == 1 or download_found == 1:
break
#print 'END'
if torrent_found == 0 and download_found == 0:
print 'Not found1'
exit()
# return
response = requests.get('https://api.themoviedb.org/3/search/tv?api_key='+str(tmdb_api)+'&language=en-US&page=1&query='+str(show_title)+'&include_adult=false').json()
tmdb_id = response['results'][0]['id']
response = requests.get('https://api.themoviedb.org/3/tv/'+str(tmdb_id)+'/external_ids?api_key='+str(tmdb_api)+'&language=en-US').json()
tvdb_id = response['tvdb_id']
imdb_id = response['imdb_id']
response = requests.get('http://api.tvmaze.com/lookup/shows?thetvdb='+str(tvdb_id)).json()
show_id = response['id']
response = requests.get('http://api.tvmaze.com/shows/'+str(show_id)+'/episodes').json()
for i in response:
if i['season'] == show_season and i['number'] == show_episode:
episode_id = i['id']
try: response = requests.get('http://api.tvmaze.com/episodes/'+str(episode_id)+'?embed=show').json()
except: response = ''
try: episode_name = response['name']
except: episode_name = ''
try: air_date = response['airdate']
except: air_date = ''
try: year = response['airdate'][0:4]
except: year = ''
try: plot = response['summary'].replace('<p>','').replace('</p>','').replace('\n','').replace('\r','').encode("utf8")
except: plot = ''
try: genre = response['_embedded']['show']['genres']
except: genre = ''
try: rating = response['_embedded']['show']['rating']['average']
except: rating = ''
try: runtime = response['_embedded']['show']['runtime']
except: runtime = ''
try:
tvmaze_thumb_medium = response['image']['medium']
tvmaze_thumb_large = response['image']['medium'].replace('medium','large')
tvmaze_thumb_original = response['image']['original'].replace('medium','large')
episode_thumb = tvmaze_thumb_medium
except:
episode_thumb = ''
#print plot
#print air_date
#print tvmaze_thumb_medium
#print tvmaze_thumb_large
#print tvmaze_thumb_original
hdclearart, seasonposter, seasonthumb, seasonbanner, tvthumb, tvbanner, showbackground, clearlogo, characterart, tvposter, clearart, hdtvlogo = '', '', '', '', '', '', '', '', '', '', '', '';
try: response = requests.get('http://webservice.fanart.tv/v3/tv/'+str(tvdb_id)+'?api_key='+str(fanart_api)).json()
except: response = ''
for i in response:
# print i
for j in response[i]:
try:
lang = j['lang']
if j['lang'] == 'en' or (i == 'showbackground' and j['lang'] == ''):
if i == 'hdclearart':
hdclearart = response[i][0]['url']
break
if i == 'seasonposter':
for k in response[i]:
if int(k['season']) == show_season:
# print k['season']
seasonposter = k['url']
break
if i == 'seasonthumb':
for k in response[i]:
if int(k['season']) == show_season:
# print k['season']
seasonthumb = k['url']
break
if i == 'seasonbanner':
for k in response[i]:
if int(k['season']) == show_season:
# print k['season']
seasonbanner = k['url']
break
if i == 'tvthumb':
tvthumb = response[i][0]['url']
break
if i == 'tvbanner':
tvbanner = response[i][0]['url']
break
if i == 'showbackground':
showbackground = response[i][0]['url']
break
if i == 'clearlogo':
clearlogo = response[i][0]['url']
break
if i == 'characterart':
characterart = response[i][0]['url']
break
if i == 'tvposter':
tvposter = response[i][0]['url']
break
if i == 'clearart':
clearart = response[i][0]['url']
break
if i == 'hdtvlogo':
hdtvlogo = response[i][0]['url']
break
except:
pass
import sqlite3
con = sqlite3.connect('/home/osmc/.kodi/userdata/Database/MyVideos116.db')
cur = con.cursor()
sql_result1 = cur.execute("SELECT idepisode, * from files,episode,tvshow where episode.idfile = files.idfile and episode.idshow = tvshow.idshow and tvshow.c00 = '"+str(show_title)+"' and episode.c12 = '"+str(show_season)+"' and episode.c13 = '"+str(show_episode)+"' order by dateadded asc").fetchall()
#print sql_result
try: dbid = sql_result1[0][0]
except: dbid = 0
sql_result = cur.execute("SELECT resumeTimeInSeconds,C11 from episode_view where idepisode = " + str(dbid)).fetchall()
try: resumeTimeInSeconds = sql_result[0][0]
except: resumeTimeInSeconds = ''
try: duration = sql_result[0][1]
except: duration = runtime
if duration == '' or duration == None:
duration = runtime
if resumeTimeInSeconds == None:
resumeTimeInSeconds = 0
cur.close()
if xbmc_plugin == 'False' and PTN_title == show_title_clean and int(PTN_season) == show_season and int(PTN_episode)== show_episode:
print show_title
print PTN_title
print PTN_season
print PTN_episode
print PTN_res
print PTN_link
print PTN_size
print PTN_download
print dbid
print episode_name
print plot
print air_date
print episode_thumb
print genre
print year
print rating
print hdclearart
print seasonposter
print seasonthumb
print seasonbanner
print tvthumb
print tvbanner
print showbackground
print clearlogo
print characterart
print tvposter
print clearart
print hdtvlogo
print resumeTimeInSeconds
print duration
#print sql_result1
else:
print 'Not found'
if xbmc_plugin == 'True' and PTN_title == show_title_clean and int(PTN_season) == show_season and int(PTN_episode)== show_episode:
if seasonposter <> '':
poster = seasonposter
else:
poster = tvposter
xbmcgui.Window(10000).setProperty('Next_EP.poster', poster)
fanart = showbackground
xbmcgui.Window(10000).setProperty('Next_EP.fanart', fanart)
if hdtvlogo <> '':
clearlogo = hdtvlogo
else:
clearlogo = clearlogo
xbmcgui.Window(10000).setProperty('Next_EP.clearlogo', clearlogo)
landscape = showbackground
xbmcgui.Window(10000).setProperty('Next_EP.landscape', landscape)
banner = tvbanner
xbmcgui.Window(10000).setProperty('Next_EP.banner', banner)
thumb = episode_thumb
xbmcgui.Window(10000).setProperty('Next_EP.thumb', thumb)
#director = xbmc.getInfoLabel('listitem.Director')
#cast = xbmc.getInfoLabel('listitem.Cast')
#cast_role = xbmc.getInfoLabel('listitem.CastAndRole')
duration = str(duration)
xbmcgui.Window(10000).setProperty('Next_EP.duration', duration)
dbid = str(dbid)
xbmcgui.Window(10000).setProperty('Next_EP.dbid', dbid)
dbtype = 'episode'
xbmcgui.Window(10000).setProperty('Next_EP.dbtype', dbtype)
genre = genre
xbmcgui.Window(10000).setProperty('Next_EP.genre', str(genre))
imdb = imdb_id
xbmcgui.Window(10000).setProperty('Next_EP.imdb', imdb)
icon = episode_thumb
xbmcgui.Window(10000).setProperty('Next_EP.icon', icon)
label = episode_name
xbmcgui.Window(10000).setProperty('Next_EP.label', label)
label2 = episode_name
xbmcgui.Window(10000).setProperty('Next_EP.label2', label2)
#MPAA = xbmc.getInfoLabel('listitem.MPAA')
originaltitle = show_title
xbmcgui.Window(10000).setProperty('Next_EP.originaltitle', originaltitle)
plot = plot
xbmcgui.Window(10000).setProperty('Next_EP.plot', plot )
plotoutline = plot
xbmcgui.Window(10000).setProperty('Next_EP.plotoutline', plotoutline)
premiered = air_date
xbmcgui.Window(10000).setProperty('Next_EP.premiered', premiered)
rating = rating
xbmcgui.Window(10000).setProperty('Next_EP.rating', str(rating))
tv_show_title = show_title
xbmcgui.Window(10000).setProperty('Next_EP.tv_show_title', tv_show_title)
#rating_votes = xbmc.getInfoLabel('listitem.RatingAndVotes')
#set = xbmc.getInfoLabel('listitem.Set')
#setid = xbmc.getInfoLabel('listitem.SetID')
#studio = xbmc.getInfoLabel('listitem.Studio')
#tagline = xbmc.getInfoLabel('listitem.Tagline')
title = episode_name
xbmcgui.Window(10000).setProperty('Next_EP.title', title)
#votes = xbmc.getInfoLabel('listitem.Votes')
#writer = xbmc.getInfoLabel('listitem.Writer')
year = str(year)
xbmcgui.Window(10000).setProperty('Next_EP.year', year)
#percent_played = xbmc.getInfoLabel('ListItem.PercentPlayed')
resumetime = resumeTimeInSeconds
xbmcgui.Window(10000).setProperty('Next_EP.resumetime', str(resumetime))
xbmcgui.Window(10000).setProperty('Next_EP.Episode', str(show_episode))
xbmcgui.Window(10000).setProperty('Next_EP.Season', str(show_season))
xbmcplugin.setContent(-1, 'episodes')
li = xbmcgui.ListItem(label, iconImage=thumb)
li.setProperty('fanart_image', fanart)
li.setProperty('startoffset', str(resumeTimeInSeconds))
li.setProperty('DBID', dbid)
li.setProperty('TVShowTitle', show_title)
li.setProperty('Episode', str(show_episode))
li.setProperty('Season', str(show_season))
#li.setProperty('Cast', cast)
#li.setProperty('CastAndRole', cast_role)
li.setProperty('Duration', duration)
li.setArt({ 'poster': poster, 'fanart': fanart, 'banner': banner, 'clearlogo': clearlogo, 'landscape': landscape, 'thumb': thumb})
li.setProperty('IsPlayable', 'true')
li.setProperty('IsFolder', 'false')
#li.setInfo('video', {'title': title,'genre': genre, 'plotoutline': plotoutline, 'plot': plot, 'path': PTN_download,'premiered': premiered, 'dbid': dbid, 'mediatype': dbtype, 'writer': writer, 'director': director, 'duration': duration, 'IMDBNumber': imdb, 'MPAA': MPAA, 'Rating': rating, 'Studio': studio, 'Year': year, 'Tagline': tagline, 'Set': set, 'SetID': setid})
li.setInfo('video', {'title': title, 'TVShowTitle': show_title, 'Episode': str(show_episode), 'Season': show_season,'genre': genre, 'plotoutline': plotoutline, 'plot': plot, 'path': PTN_download,'premiered': premiered, 'dbid': dbid, 'mediatype': dbtype, 'duration': duration, 'IMDBNumber': imdb, 'Rating': rating, 'Year': year})
li.setPath(PTN_download)
xbmcplugin.addDirectoryItem(handle=-1, url=PTN_download , listitem=li, isFolder=True)
xbmcplugin.setResolvedUrl(-1, True, li)
xbmcgui.Window(10000).setProperty('Next_EP.ResolvedUrl', 'true')
xbmcgui.Window(10000).setProperty('Next_EP.Url', PTN_download)
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
playlist.add(PTN_download, li)
# xbmc.Player().play(item=PTN_download, listitem=li)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment