2019-05-22, 20:14
Hello
I posted my preliminary versions of a script which can be used to generate a next up episodes smart playlist. This was very much a work in progress but i've now refined it to a fairly final, reliable version which runs very quickly. So the original thread was here:
https://forum.kodi.tv/showthread.php?tid=344009
The new version only writes one playlist file, it gets all the episode id's for next up episodes dated before today using a single sql query (it may look fairly complex but i can break it down for anyone who is interested and wanted to make their own playlists), there is no issue with the "Special://" path which i had in my original script, all configuration is done at the top of the script. And it triggers widget refreshing by marking the oldest unwatched episode watched/unwatched.
So all you should really need to do is determine the path to the playlists folder and the database folder for your system and setup your kodi connection options.
I posted my preliminary versions of a script which can be used to generate a next up episodes smart playlist. This was very much a work in progress but i've now refined it to a fairly final, reliable version which runs very quickly. So the original thread was here:
https://forum.kodi.tv/showthread.php?tid=344009
The new version only writes one playlist file, it gets all the episode id's for next up episodes dated before today using a single sql query (it may look fairly complex but i can break it down for anyone who is interested and wanted to make their own playlists), there is no issue with the "Special://" path which i had in my original script, all configuration is done at the top of the script. And it triggers widget refreshing by marking the oldest unwatched episode watched/unwatched.
So all you should really need to do is determine the path to the playlists folder and the database folder for your system and setup your kodi connection options.
python:#!/usr/bin/env python
import requests
import json
import sys
import base64
import os, re, os.path
import sqlite3
con = sqlite3.connect('/home/path/.kodi/userdata/Database/MyVideos116.db')
cur = con.cursor()
kodi_credentials = b'user:password'
kodi_encoded_credentials = base64.b64encode(kodi_credentials)
kodi_authorization = b'Basic ' + kodi_encoded_credentials
kodi_header = { 'Content-Type': 'application/json', 'Authorization': kodi_authorization }
kodi_ip = '127.0.0.1'
kodi_port = '8080'
kodi_url = 'http://' + kodi_ip + ':' + kodi_port + '/jsonrpc'
playlist_path = '/home/path/.kodi/userdata/playlists/video/'
#in progress + next up episodes - episodeid + path
sql_result = (cur.execute(" select idepisode,c18,c13,show from (SELECT files.idfile, episode.c00, episode.c18, episode.c12, episode.c13, episode.c05, idepisode, tvshow.c00 as show, episode.idShow, playcount, lastplayed from episode, files, tvshow where episode.idfile=files.idfile and episode.idshow = tvshow.idshow and files.idfile in (select idfile from(SELECT min(files.idfile) as idfile, min(episode.c05) as firstaired, tvshow.c00 FROM episode, files, tvshow where episode.idfile=files.idfile and episode.idshow = tvshow.idshow and playcount is null GROUP BY tvshow.c00) ) ) where idshow in (select idshow from(SELECT files.idfile, episode.c00, episode.c05, idepisode, tvshow.c00, episode.idShow, playcount, lastplayed from episode, files, tvshow where episode.idfile=files.idfile and episode.idshow = tvshow.idshow and files.idfile in (select idfile from(SELECT max(files.idfile) as idfile, max(episode.c05) as firstaired, tvshow.c00 FROM episode, files, tvshow where episode.idfile=files.idfile and episode.idshow = tvshow.idshow and playcount>0 GROUP BY tvshow.c00) ) )) and c05 < date('now') order by c05 desc").fetchall())
big_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>' + "\n" + '<smartplaylist type="episodes">' + "\n"
big_xml = big_xml + ' <name>001_NEXT_EPISODES</name>' + "\n" + ' <match>one</match>'
for k in sql_result:
episode_id = k[0]
file_name = os.path.basename(k[1])
big_xml = big_xml + "\n" + ' <rule field=\"filename\" operator=\"is\"><value>'+ file_name.replace('&','&') +'</value></rule>'
big_xml = big_xml + "\n"+' <order direction=\"descending\">year</order>' + "\n" +'</smartplaylist>'
f = open(playlist_path + "NEXT_EPISODES_PLAYLIST.xsp", "w")
f.write(big_xml)
f.close()
kodi_params = ('{"jsonrpc":"2.0","method":"VideoLibrary.SetEpisodeDetails","params":{"episodeid":' + str(episode_id)+',"playcount": 1},"id":"1"}')
kodi_response = requests.post(kodi_url, headers=kodi_header, data=kodi_params)
json_data = json.dumps(kodi_response.json(), indent=4, sort_keys=True)
kodi_params = ('{"jsonrpc":"2.0","method":"VideoLibrary.SetEpisodeDetails","params":{"episodeid":' + str(episode_id)+',"playcount": 0},"id":"1"}')
kodi_response = requests.post(kodi_url, headers=kodi_header, data=kodi_params)
json_data = json.dumps(kodi_response.json(), indent=4, sort_keys=True)
print(json_data)
cur.close()
exit()