forked from ilyaz/PlexDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyplex.py
75 lines (71 loc) · 2.43 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
from xml.dom import minidom
import urllib
import os
import time
import hashlib
from ConfigParser import SafeConfigParser
import re
import socket
from urllib2 import Request, urlopen, quote
import base64
import uuid
import platform
from time import gmtime, strftime
import random
import string
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 myPlexSignin(username,password):
try:
if os.path.isfile('token.txt'):
with open('token.txt', 'r') as f:
authtoken = f.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"
r = Request("https://plex.tv/users/sign_in.xml", data="", headers=headers)
r = urlopen(r)
compiled = re.compile("<authentication-token>(.*)<\/authentication-token>", re.DOTALL)
authtoken = compiled.search(r.read()).group(1).strip()
if authtoken != None:
f = open('token.txt','w+')
f.write(authtoken)
f.close()
if myplexshared == "enable":
link = "https://plex.tv/pms/system/library/sections?X-Plex-Token="+authtoken
f = urllib.urlopen(link)
serverlist = f.read()
tokens = re.findall("accessToken\=\"(.*?)\"", serverlist)
tokens = list(set(tokens))
tokens.remove(authtoken)
authtoken = tokens[0]
f = open('token.txt','w+')
f.write(authtoken)
f.close()
return authtoken
print "Successfully grabbed shared myPlex Tokens!"
else:
return authtoken
print "Successfully authenticated with myPlex!"
else:
print "Failed to login to myPlex!"
return authtoken
else:
authtoken = ""
except Exception, e:
print "Failed to login to myPlex: %s" % str(e)