def api_create_profile(self, generated_profile=None): print("Création du profile...") profile = self.fetch_identity() if generated_profile is None else generated_profile url = self.base_url + "/profile" DATA = { 'firstName': profile[0], 'lastName': profile[1], 'hobbies': [ profile[2][0], profile[2][1] ], 'email': self.email, 'website': profile[3], 'summary': profile[4], } HEADER = { 'custom_auth': "authorization=Token " + self.token } request = requests.post(url=url, data=DATA, headers=HEADER) res = request.json() if request.status_code == 200: print_bold_success("Profile créée") print(Color.CYAN + json.dumps(DATA, indent=4) + Color.END) return True else: print_bold_error("Impossible de créer le profile") print(Color.CYAN + json.dumps(res, indent=4) + Color.END) return False
def _send_notify(appkey,userkey,message,priority): """ Sends a pushover notification to the address provided userKey: The pushover user key to send the message to (or to subscribe with) msg: The message to send (unicode) returns: True if the message succeeded, False otherwise """ params = { 'token': appkey, 'user': userkey, 'title': 'Auto-Sub', 'message': message, 'retry': 30, 'expire': 180, 'priority': priority, } try: msg = requests.post('https://api.pushover.net/1/messages.json', data=params).json() except: log.error('Notify Pushover: Problem sending Pushover message.') return False if msg['status'] == 0 : log.error('Notify Pushover: Pushover error is: %s' % msg['errors'][0]) return False return True
def api_get_token(self): url = self.base_url + "/authUser" data = { 'email': self.email, 'password': self.password, } request = requests.post(url=url, data=data) res = request.json() if request.status_code == 200: try: self.token = res['authUser']['token'] except KeyError: return False else: print_bold_error("Erreur :") print_api(str(res)) print_success("Token acquis")
def __sopa_post(self, method, body): head = {'Username': self.username, 'Password': self.password} self.__encode_object(head) headers = { 'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': 'http://www.open.com.cn/' + method } payload = self.__xml(method, head, body) r = requests.post('http://' + self.path + '/WebService/ServiceV3.asmx', headers=headers, data=payload) root = ET.fromstring(r.text) result = {} body = root[0][0] for child in body: if child.text != None: result[child.tag[24:]] = child.text return result
def api_create_user(self) -> bool: url = self.base_url + "/subscribeUser" data = { 'username': self.username, 'email': self.email, 'password': self.password, } request = requests.post(url=url, data=data) res = request.json() if request.status_code == 200 and res['authUser']['status'] is True: print_bold_success("Nouvel utilisateur créée") self.token = res['authUser']['token'] elif request.status_code == 200 and res['authUser']['status'] is False: print_bold_warning("Cet utilisateur existe déjà. User par défaut remplacé") self.token = res['authUser']['token'] self.store_user() else: print_bold_error("Erreur") print_api(str(res)) return False return True
def api_create_ad(self, body: json) -> bool: global prod_url, preprod_url data = json.dumps(body) url = self.base_url + "/create" # print(url) header = { 'custom_auth': "authorization=Token " + self.token_user, 'Content-Type': 'application/json' } request = requests.post(url=url, data=data, headers=header) res = request.json() print(res) if request.status_code == 200 and res['res'].find("Added") != -1: if body['bearing'] == 1: self.ad_id = res['_id'] return True else: print(Color.BOLD + Color.RED + "Erreur :\n" + Color.END + Color.CYAN + str(res) + Color.END) return False
def _update_library(plexserverhost, plexserverport, plexserverusername, plexserverpassword, plexservertoken): if not plexserverhost: plexserverhost = autosub.PLEXSERVERHOST if not plexserverport: plexserverport = int(autosub.PLEXSERVERPORT) if not plexserverusername: plexserverusername = autosub.PLEXSERVERUSERNAME if not plexserverpassword: plexserverpassword = autosub.PLEXSERVERPASSWORD if not plexservertoken: plexservertoken = autosub.PLEXSERVERTOKEN #Maintain support for older Plex installations without myPlex if not plexservertoken and not plexserverusername and not plexserverpassword: url = "http://%s:%s/library/sections" % (plexserverhost, plexserverport) try: response = requests.get(url, timeout=10) except: log.error("Error while trying to contact plexmedia server") return False #SubLines = re.findall('<tr class="epeven completed">(.*?)</tr>', SubOverviewPage, re.S) else: #Fetch X-Plex-Token if it doesn't exist but a username/password do if not plexservertoken and plexserverusername and plexserverpassword: log.info("Fetching a new X-Plex-Token from plex.tv") authheader = "Basic %s" % base64.encodestring( '%s:%s' % (plexserverusername, plexserverpassword))[:-1] headers = { "Authorization": authheader, "X-Plex-Product": "AutoSub Notifier", "X-Plex-Client-Identifier": "b3a6b24dcab2224bdb101fc6aa08ea5e2f3147d6", "X-Plex-Version": "1.0" } try: response = requests.post("https://plex.tv/users/sign_in.xml", headers=headers, verify=autosub.CERT, timeout=10) except: log.error("Error while trying to contact plexmediaserver") return False auth_tree = ET.fromstring(response.text) plexservertoken = auth_tree.findall( ".//authentication-token")[0].text autosub.PLEXSERVERTOKEN = plexservertoken if plexservertoken: #Add X-Plex-Token header for myPlex support workaround try: response = requests.get('%s/%s?X-Plex-Token=%s' % ("%s:%s" % (plexserverhost, plexserverport), 'library/sections', plexservertoken), timeout=10) except Exception as error: log.error("Error from get header. Error is: %s" % error) return False xml_sections = ET.fromstring(response.text) try: sections = xml_sections.findall('Directory') except: pass if not sections: log.info("Server not running on: %s:%s" % (plexserverhost, plexserverport)) return False for s in sections: if s.get('type') == "title": if plexservertoken: try: requests.get('%s/%s?X-Plex-Token=%s' % ("%s:%s" % (plexserverhost, plexserverport), "library/sections/%s/refresh" % (s.get('key')), plexservertoken), timeout=10) log.info("TV Shows library (%s) is currently updating." % s.get('title')) return True except Exception, e: log.error("Error updating library section: %s" % e) return False else: url = "http://%s:%s/library/sections/%s/refresh" % ( plexserverhost, plexserverport, s.getAttribute('key')) try: requests.get(url, timeout=10) log.info("TV Shows library is currently updating.") return True except Exception, e: log.error("Error updating library section: %s" % e) return False
#Fetch X-Plex-Token if it doesn't exist but a username/password do if not plexservertoken and plexserverusername and plexserverpassword: log.info( "plexmediaserver: Fetching a new X-Plex-Token from plex.tv") authheader = "Basic %s" % base64.encodestring( '%s:%s' % (plexserverusername, plexserverpassword))[:-1] headers = { "Authorization": authheader, "X-Plex-Product": "AutoSub Notifier", "X-Plex-Client-Identifier": "b3a6b24dcab2224bdb101fc6aa08ea5e2f3147d6", "X-Plex-Version": "1.0" } response = requests.post("https://plex.tv/users/sign_in.xml", headers=headers) auth_tree = ET.fromstring(response.text) plexservertoken = auth_tree.findall( ".//authentication-token")[0].text autosub.PLEXSERVERTOKEN = plexservertoken if plexservertoken: #Add X-Plex-Token header for myPlex support workaround response = requests.get('%s/%s?X-Plex-Token=%s' % ("%s:%s" % (plexserverhost, plexserverport), 'library/sections', plexservertoken)) xml_sections = ET.fromstring(response.text)
def _update_library(plexserverhost, plexserverport, plexserverusername, plexserverpassword, plexservertoken): if not plexserverhost: plexserverhost = autosub.PLEXSERVERHOST if not plexserverport: plexserverport = int(autosub.PLEXSERVERPORT) if not plexserverusername: plexserverusername = autosub.PLEXSERVERUSERNAME if not plexserverpassword: plexserverpassword = autosub.PLEXSERVERPASSWORD if not plexservertoken: plexservertoken = autosub.PLEXSERVERTOKEN #Maintain support for older Plex installations without myPlex if not plexservertoken and not plexserverusername and not plexserverpassword: url = "http://%s:%s/library/sections" % (plexserverhost, plexserverport) try: response = requests.get(url) except: log.error("plexmediaserver: Error while trying to contact plexmedia server") return False #SubLines = re.findall('<tr class="epeven completed">(.*?)</tr>', SubOverviewPage, re.S) else: #Fetch X-Plex-Token if it doesn't exist but a username/password do if not plexservertoken and plexserverusername and plexserverpassword: log.info("plexmediaserver: Fetching a new X-Plex-Token from plex.tv") authheader = "Basic %s" % base64.encodestring('%s:%s' % (plexserverusername, plexserverpassword))[:-1] headers = { "Authorization": authheader, "X-Plex-Product": "AutoSub Notifier", "X-Plex-Client-Identifier": "b3a6b24dcab2224bdb101fc6aa08ea5e2f3147d6", "X-Plex-Version": "1.0" } try: response = requests.post("https://plex.tv/users/sign_in.xml", headers=headers); except: log.error("Plex Media Server: Error while trying to contact plexmediaserver") return False auth_tree = ET.fromstring(response.text) plexservertoken = auth_tree.findall(".//authentication-token")[0].text autosub.PLEXSERVERTOKEN = plexservertoken if plexservertoken: #Add X-Plex-Token header for myPlex support workaround response = requests.get('%s/%s?X-Plex-Token=%s' % ( "%s:%s" % (plexserverhost, plexserverport), 'library/sections', plexservertoken )) xml_sections = ET.fromstring(response.text) try: sections = xml_sections.findall('Directory') except: pass if not sections: log.info("plexmediaserver: Server not running on: %s:%s" % (plexserverhost, plexserverport)) return False for s in sections: if s.get('type') == "show": if plexservertoken: try: requests.get('%s/%s?X-Plex-Token=%s' % ( "%s:%s" % (plexserverhost, plexserverport), "library/sections/%s/refresh" % (s.get('key')), plexservertoken )) log.info("plexmediaserver: TV Shows library (%s) is currently updating." % s.get('title')) return True except Exception, e: log.error("plexmediaserver: Error updating library section: %s" % e) return False else: url = "http://%s:%s/library/sections/%s/refresh" % (plexserverhost, plexserverport, s.getAttribute('key')) try: requests.get(url) log.info("Plex Media Server: TV Shows library is currently updating.") return True except Exception, e: log.error("Plex Media Server: Error updating library section: %s" % e) return False
log.error("plexmediaserver: Error while trying to contact: %s" % e) return False else: #Fetch X-Plex-Token if it doesn't exist but a username/password do if not plexservertoken and plexserverusername and plexserverpassword: log.info("plexmediaserver: Fetching a new X-Plex-Token from plex.tv") authheader = "Basic %s" % base64.encodestring('%s:%s' % (plexserverusername, plexserverpassword))[:-1] headers = { "Authorization": authheader, "X-Plex-Product": "AutoSub Notifier", "X-Plex-Client-Identifier": "b3a6b24dcab2224bdb101fc6aa08ea5e2f3147d6", "X-Plex-Version": "1.0" } response = requests.post("https://plex.tv/users/sign_in.xml", headers=headers); auth_tree = ET.fromstring(response.text) plexservertoken = auth_tree.findall(".//authentication-token")[0].text autosub.PLEXSERVERTOKEN = plexservertoken if plexservertoken: #Add X-Plex-Token header for myPlex support workaround response = requests.get('%s/%s?X-Plex-Token=%s' % ( "%s:%s" % (plexserverhost, plexserverport), 'library/sections', plexservertoken )) xml_sections = ET.fromstring(response.text)