1111from plexapi .myplex import MyPlexAccount
1212from plexapi .myplex import MyPlexDevice
1313from uuid import uuid3 , NAMESPACE_URL
14+ from config import ppTagConfig
1415
1516class userData (object ):
1617 """a simple class that holds some user data"""
@@ -30,36 +31,17 @@ def setToken(self, token):
3031 self .token = token
3132
3233class plexUsers ():
34+ """A class to get user data (token, library id) for plex"""
3335
34- ####################### Change HERE #######################
35- #if you do not have a token you have to supply your credentials
36- PLEX_LOGIN = ''
37- PLEX_PASS = ''
38- # if you already have a token pass it here
39- PLEX_TOKEN = ''
40- # the plex server url
41- PLEX_URL = 'http://192.168.0.200:32400'
42- # provide the usernames for which the rating should be updated
43- # when users have a pin we need this, otherwise set it to ''
44- USERDATA = { 'user' : '1234' }
45-
46- # for the access tokens we need the exact server name
47- SERVERNAME = 'plexserver'
48-
49- # path of the photo library in plex
50- PHOTOS_LIBRARY_PATH = '/share/Photos/'
51-
52- ###########################################################
53-
54- def fetchPlexApi (self , path = '' , method = 'GET' , getFormPlextv = False , token = PLEX_TOKEN , params = None ):
36+ def fetchPlexApi (self , path = '' , method = 'GET' , getFormPlextv = False , token = ppTagConfig .PLEX_TOKEN , params = None ):
5537 """a helper function that fetches data from and put data to the plex server"""
5638 headers = {'X-Plex-Token' : token ,
5739 'Accept' : 'application/json' }
5840 if getFormPlextv :
5941 url = 'plex.tv'
6042 connection = http .client .HTTPSConnection (url )
6143 else :
62- url = self .PLEX_URL .rstrip ('/' ).replace ('http://' ,'' )
44+ url = ppTagConfig .PLEX_URL .rstrip ('/' ).replace ('http://' ,'' )
6345 connection = http .client .HTTPConnection (url )
6446
6547 try :
@@ -101,29 +83,29 @@ def fetchPlexApi(self, path='', method='GET', getFormPlextv=False, token=PLEX_TO
10183 def getAccessTokenForUser (self ):
10284 for user in self .users :
10385 params = urllib .parse .urlencode ({'pin' : user .pin , 'X-Plex-Client-Identifier' : self .clientId })
104- data = self .fetchPlexApi ('/api/v2/home/users/{uuid}/switch' .format (uuid = user .uuid ), 'POST' , True , self .PLEX_TOKEN , params )
86+ data = self .fetchPlexApi ('/api/v2/home/users/{uuid}/switch' .format (uuid = user .uuid ), 'POST' , True , ppTagConfig .PLEX_TOKEN , params )
10587 if 'authToken' in data :
10688 authToken = data ['authToken' ]
10789 data = self .fetchPlexApi ('/api/resources?includeHttps=1&includeRelay=1&X-Plex-Client-Identifier={clientid}' .format (clientid = self .clientId ), 'GET' , True , authToken )
10890 for device in data ['MediaContainer' ]['Device' ]:
10991 if isinstance (device , dict ):
110- if device .get ('@provides' ) == 'server' and device .get ('@name' ) == self .SERVERNAME :
92+ if device .get ('@provides' ) == 'server' and device .get ('@name' ) == ppTagConfig .SERVERNAME :
11193 self .serverId = device .get ('@clientIdentifier' )
11294 user .setToken (device .get ('@accessToken' ))
11395 break
11496
11597 def __init__ (self ):
11698 ## some initial tests
117- if len (self .PLEX_TOKEN ) == 0 :
99+ if len (ppTagConfig .PLEX_TOKEN ) == 0 :
118100 # get a token
119101 try :
120- account = MyPlexAccount (self .PLEX_LOGIN , self .PLEX_PASS )
102+ account = MyPlexAccount (ppTagConfig .PLEX_LOGIN , ppTagConfig .PLEX_PASS )
121103 except Exception as e :
122104 raise ("Error fetching from Plex API: {err}" .format (err = e ))
123105 # print the Token and message to enter it here
124106 print ('use this token' )
125107 print (account .authenticationToken )
126- print ('and put it into this file PLEX_TOKEN: plexUsers.py ' )
108+ print ('and put it into the file config.py after PLEX_TOKEN: ' )
127109 raise
128110
129111 # some lists for data
@@ -134,7 +116,7 @@ def __init__(self):
134116 # creating the client id
135117 self .clientId = uuid3 (NAMESPACE_URL , "pptag" ).hex
136118
137- self .plex = PlexServer (self .PLEX_URL , self .PLEX_TOKEN )
119+ self .plex = PlexServer (ppTagConfig .PLEX_URL , ppTagConfig .PLEX_TOKEN )
138120
139121
140122 apiUsers = self .fetchPlexApi ("/api/home/users" ,"GET" ,True )
@@ -149,8 +131,8 @@ def __init__(self):
149131
150132 for user in users :
151133 if isinstance (user , dict ):
152- if user ['@title' ] in self .USERDATA .keys ():
153- pin = self .USERDATA .get (user ['@title' ])
134+ if user ['@title' ] in ppTagConfig .USERDATA .keys ():
135+ pin = ppTagConfig .USERDATA .get (user ['@title' ])
154136 u = userData (user ['@id' ],user ['@uuid' ],user ['@title' ], pin )
155137 self .users .append (u )
156138
0 commit comments