def _convert_result_data(self, ID): evp = self.ev_db[ID] feats = self.feat_db[ID]['features'] for cf_pair, cf_dict in feats.iteritems(): if cf_pair == "general": continue row_data = {} row_data["ID"] = "%s_%s" % (ID, cf_pair) contributors = evp['feature_contributors'][cf_pair] for feat_type in ["cfsim", "cArg", "cCase"]: feat_col_name = feat_type if feat_type in [ "cfsim" ] else feat_type.replace('c', 'context') if feat_type in cf_dict: row_data[feat_col_name] = utils.encode_dict({ k: v for k, v in cf_dict[feat_type].iteritems() if '-' in k }) else: row_data[feat_col_name] = "" if feat_type in contributors: row_data["%sText" % feat_col_name] = utils.encode_dict( contributors[feat_type]) yield {k: v.decode('utf-8') for k, v in row_data.iteritems()}
def login(self, root_url, url, data, headers, fails_with): """ Login wrapper around ``open`` Args: url (str): The URL to open data (dict): POST login data fails_with (str): String that must **not** be included in the response's content Returns: bool: Whether or not login was successful """ if not url.startswith('http'): url = root_url + url if self.open(url.encode('utf-8'), post_data=encode_dict(data, self.request_charset), headers=headers): try: if fails_with in self.content: self.status = 'Wrong username or password' return False except Exception as e: log.debug("Login failed with: %s" % e) try: if fails_with in self.content.decode('utf-8'): self.status = 'Wrong username or password' return False except: return False return True return False
def login(self, url, data, fails_with, charset='utf8'): """ Login wrapper around ``open`` Args: url (str): The URL to open data (dict): POST login data fails_with (str): String that must **not** be included in the response's content Returns: bool: Whether or not login was successful """ result = False if self.open(url.encode('utf-8'), post_data=encode_dict(data, charset)): result = True try: if fails_with in self.content: self.status = 'Wrong username or password' result = False except Exception as e: log.debug("Login failed with: %s" % e) try: if fails_with in self.content.decode('utf-8'): self.status = 'Wrong username or password' result = False except: result = False return result
def _connect_ticket(self, url, lt): data = utils.encode_dict({ "username": self.config["user"], "password": self.config["password"], "lt": lt, "submit": "SE+CONNECTER", "_eventId": "submit" }) nurl = self.config["url"] + url res = requests.post( nurl, headers={ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'content-type': 'application/x-www-form-urlencoded', "Connection": "close", 'Upgrade-Insecure-Requests': '1', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'DNT': '1', 'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', "Cookie": PyDAbsSession.encode_cookies( {"JSESSIONID": self.cookies["JSESSIONID"]}) }, data=data, allow_redirects=False) if res.status_code == 302: location = "" if (not "location" in res.headers) and (not "Location" in res.headers): raise PyDException(PyDAbsSession.ERROR_DATA, res) if "location" in res.headers: location = res.headers["location"] elif "Location" in res.headers: location = res.headers["Location"] self.auth_url = location return res else: raise PyDException( PyDAbsSession.ERROR_STATUS, "Status: %d (doit normalement être 302)" % res.status_code)
def _convert_result_data(self, ID): contributors = self.ev_db[ID]['feature_contributors']['general'] feats = self.feat_db[ID]['features']['general'] row_data = {} row_data['ID'] = ID for feat_type in ["conflict", "embed", "embed_s", "cArg", "cCase"]: # will be modify in the future feat_col_name = feat_type if feat_type not in [ 'cArg', 'cCase' ] else feat_type.replace('c', 'context') # will be modify in the future if feat_type not in feats: row_data[feat_col_name] = "" else: row_data[feat_col_name] = utils.encode_dict(feats[feat_type]) if feat_type in contributors: row_data["%sText" % feat_col_name] = utils.encode_dict( contributors[feat_type]) return {k: v.decode('utf-8') for k, v in row_data.iteritems()}
def login(self, url, data, fails_with): """ Login wrapper around ``open`` Args: url (str): The URL to open data (dict): POST login data fails_with (str): String that must **not** be included in the response's content Returns: bool: Whether or not login was successful """ result = False if self.open(url.encode('utf-8'), post_data=encode_dict(data)): result = True if fails_with in self.content: self.status = 'Wrong username or password' result = False return result
def search(self, code): url = self.url("/index.php", {"_action_": "backend"}) rawdata = { "path": "/networks/" + self.config["network"] + "/libraries/" + self.config["library"] + "/catalog/item/search/fulltext", "criteria": { "code": code, "networkId": self.config["network"], "sort": "code", "dir": "ASC", "start": 0, "limit": 20 }, "jsonParams": "criteria" } data = utils.encode_dict(rawdata) res = requests.post(url, headers={ "content-type": 'application/x-www-form-urlencoded', 'Cookie': 'eppk=' + self.cookies["eppk"] }, data=data) if res.status_code != 200: raise PyDException(PyDAbsSession.ERROR_STATUS, "Erreur, status : %d" % res.status_code) res = json.loads(res.text) id = res["res"]["executionId"] while True: res = self._progress("/catalog/item/search/%d/progress" % id) if res["res"]["execution"]["status"] == "COMPLETED": break res = self._progress("/catalog/item/search/%d/result/data" % id) return res["res"]["list"][0]
def modify(self, js): data = {} fields = [ "id", "code", "depositaryLibraryId", "cote", "price", "discount", "discountedPrice", "creationDate", "publicAvailableDate", "note", "isNew", "isCreatedAsNew", "itemClassifications" ] for key in fields: data[key] = js[key] if not key in js: raise Exception( "Le champ '%s' n'est pas dans le json du document" % key) fieldsId = [ "localization", "subLocalization", "support", "targetAudience", "loanCategory", "status", "owner", "collectionPeriodical", "targetOwner", "budgetLine", "provider" ] for key in fieldsId: data[key + "Id"] = js[key]["id"] if js[key] else None data["computeIsNewPeriodAgain"] = None data["checkPeriodicalCoherency"] = True data["trap"] = js["trap"] if "trap" in js else None if data["trap"]: toremove = [] for key in data["trap"]: if not key in [ "content", "blockingLoan", "visibleInLoan", "visibleInReturn" ]: toremove.append(key) for key in toremove: del data["trap"][key] with open("sources/%s.sent.json" % data["code"], "w") as f: f.write(json.dumps(data, indent=2)) url = self.url("/index.php", {"_action_": "backend"}) data = utils.encode_dict({ "path": "/networks/" + self.config["network"] + "/libraries/" + self.config["library"] + "/catalog/bibRecords/items/job/addOrUpdate", "operation": "UPDATE", "libraryId": self.config["library"], "bibrecordId": js["bibRecord"]["id"], "addOrUpdateForm": json.dumps(data), "jsonParams": "addOrUpdateForm" }) res = requests.post(url, headers={ "content-type": 'application/x-www-form-urlencoded', 'Cookie': 'eppk=' + self.cookies["eppk"] }, data=data) if res.status_code != 200: raise PyDException(PyDAbsSession.ERROR_STATUS, "Erreur, status : %d" % res.status_code) res = json.loads(res.text) id = res["res"]["executionId"] while True: res = self._progress( "/catalog/bibRecords/items/job/addOrUpdate/%d/progress" % id) if res["res"]["execution"]["status"] == "COMPLETED": break res = self._progress( "/catalog/bibRecords/items/job/addOrUpdate/%d/result" % id) return res