forked from danstis/PlexDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myplex.py
77 lines (71 loc) · 3.28 KB
/
myplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
""" Contains functions used for interacting with MyPlex"""
import base64
import os
import platform
import re
import socket
import uuid
from ConfigParser import SafeConfigParser
from urllib2 import Request, quote, urlopen
PARSER = SafeConfigParser()
PARSER.read('user.ini')
MYPLEXSTATUS = PARSER.get('myplex', 'status')
MYPLEXUSERNAME = PARSER.get('myplex', 'username')
MYPLEXPASSWORD = PARSER.get('myplex', 'password')
MYPLEXSHARED = PARSER.get('myplex', 'shared')
def myplex_signin(username, password):
"""Attempts to signin to MyPlex and retrieve a token"""
try:
if os.path.isfile('token.txt'):
with open('token.txt', 'r') as tokenfile:
authtoken = tokenfile.readline()
print "Using cached myPlex token."
return authtoken
elif username != '' and password != '':
print "Fetching myPlex authentication token."
headers = {}
headers["Authorization"] = "Basic %s" % base64.encodestring(
'%s:%s' % (username, password)).replace('\n', '')
headers["X-Plex-Client-Identifier"] = quote(
base64.encodestring(str(uuid.getnode())).replace('\n', ''))
headers["X-Plex-Product"] = "Plex-Downloader"
headers["X-Plex-Device"] = "Plex-Downloader"
headers["X-Plex-Device-Name"] = socket.gethostname()
headers["X-Plex-Platform"] = platform.system()
headers["X-Plex-Client-Platform"] = platform.system()
headers["X-Plex-Platform-Version"] = platform.version()
headers["X-Plex-Provides"] = "controller"
request = Request("https://plex.tv/users/sign_in.xml",
data="", headers=headers)
response = urlopen(request)
compiled = re.compile(
r"<authentication-token>(.*)</authentication-token>", re.DOTALL)
authtoken = compiled.search(response.read()).group(1).strip()
if authtoken != None:
with open('token.txt', 'w+') as tokenfile:
tokenfile.write(authtoken)
if MYPLEXSHARED == "enable":
link = "https://plex.tv/api/resources.xml?includeHttps=1&includeRelay=1&X-Plex-Device-Name=iPhone&X-Plex-Token=" + authtoken
response = urlopen(link)
serverlist = response.read()
tokens = re.findall(r"accessToken=\"(.*?)\"", serverlist)
tokenlist = list(set(tokens))
for item in tokenlist:
print(item)
print("-------------------------------")
response = raw_input("Please enter your token: ")
authtoken = response
with open('token.txt', 'w+') as tokenfile:
tokenfile.write(authtoken)
print "Successfully grabbed shared myPlex Tokens!"
return authtoken
else:
print "Successfully authenticated with myPlex!"
return authtoken
else:
print "Failed to login to myPlex!"
return authtoken
else:
authtoken = ""
except Exception, e:
print "Failed to login to myPlex: %s" % str(e)