├── README.md ├── myplextv.py └── webhookservice.py /README.md: -------------------------------------------------------------------------------- 1 | # customplextv -------------------------------------------------------------------------------- /myplextv.py: -------------------------------------------------------------------------------- 1 | from random import randint, shuffle 2 | import sys 3 | 4 | global plex 5 | global client 6 | 7 | 8 | def plexlogin(): 9 | global plex 10 | global client 11 | 12 | baseurl = "http://serverip:port" 13 | 14 | from plexapi.server import PlexServer 15 | 16 | plex = PlexServer(baseurl) 17 | 18 | client = plex.client("clientnamegoeshere") 19 | 20 | 21 | def getmovies(): 22 | plexlogin() 23 | movielist = plex.library.section("Movies") 24 | mvlist = movielist.search("") 25 | max = len(mvlist)-1 26 | plme = randint(0, max) 27 | playme = mvlist[plme].title 28 | print ("Found the following title: " + playme + "\nTrying to play that now.") 29 | client.playMedia(mvlist[plme]) 30 | return ("Done.") 31 | 32 | def stopvideo(): 33 | plexlogin() 34 | client.stop('video') 35 | 36 | def pausevideo(): 37 | plexlogin() 38 | client.pause('video') 39 | 40 | 41 | 42 | 43 | #say = getmovies() 44 | 45 | if ("stopvideo" in sys.argv[1]): 46 | stopvideo() 47 | say = "Done" 48 | elif ("pausevideo" in sys.argv[1]): 49 | pausevideo() 50 | say = "Done" 51 | elif ("startnextprogram" in sys.argv[1]): 52 | say = getmovies() 53 | 54 | print (say) 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /webhookservice.py: -------------------------------------------------------------------------------- 1 | import web 2 | import os 3 | 4 | global WSTATUS 5 | WSTATUS = "ON" 6 | 7 | urls = ('/.*', 'hooks') 8 | 9 | app = web.application(urls, globals()) 10 | 11 | def getclient(data): 12 | client = data 13 | client = client.split("\"Player\":") 14 | client = client[1] 15 | client = client.split("},") 16 | client = client[0] 17 | client = client.split("title\":\"") 18 | client = client[1] 19 | client = client.split("\"") 20 | client = client[0] 21 | return (client) 22 | 23 | def getaction(data): 24 | action = data 25 | action = action.split("event\":\"") 26 | action = action[1] 27 | action = action.split("\"") 28 | action = action[0] 29 | return action 30 | 31 | class hooks: 32 | def POST(self): 33 | global WSTATUS 34 | client = "Your Client Name Goes Here" 35 | 36 | data = web.data() 37 | clnt = getclient(data) 38 | action = getaction(data) 39 | print (clnt) 40 | print (action) 41 | 42 | if WSTATUS == "ON": 43 | 44 | if (("media.pause" in action) and (clnt == client)): 45 | print ("Paused") 46 | elif (("media.resume" in action) and (clnt == client)): 47 | print ("Resumed") 48 | elif (("media.stop" in action) and (clnt == client)): 49 | print ("Stopped") 50 | WSTATUS = "Pending" 51 | command = "python myplextv.py startnextprogram" 52 | print ("Starting Next Program") 53 | os.system(command) 54 | elif (("media.scrobble" in action) and (clnt == client)): 55 | print ("Current feature is almost over.") 56 | 57 | elif (WSTATUS == "Pending"): 58 | if (("media.stop" in action) and (clnt == client)): 59 | print ("Duplicate Request, Dropping.") 60 | elif (("media.play" in action) and (clnt == client)): 61 | print ("Media Playing, Resetting Status.") 62 | WSTATUS = "ON" 63 | 64 | 65 | 66 | return ("OK") 67 | 68 | if __name__ == '__main__': 69 | app.run() 70 | --------------------------------------------------------------------------------