def load_authors(): print("Loading authors") """Loads sample authors into database""" db.session.add( Author(author_name="Brandon Sanderson", goodreads_id="38550")) db.session.add( Author(author_name="William Shakespeare", goodreads_id="947")) db.session.add(Author(author_name="Lois Lowry", goodreads_id="2493")) db.session.add(Author(author_name="Dr.Seuss", goodreads_id="61105")) db.session.add(Author(author_name="Stephen King")) db.session.commit()
def get_author_id(): """ Returns author id for author name inputted """ a_name = request.form.get("author") author = Author.query.filter_by(author_name=a_name).first() if author: # if the author is in the database return jsonify({"auth_status": "ok", "id": author.author_id}) else: # author is not in database under spelling provided goodreads_id, goodreads_name = get_author_goodreads_info(a_name) if goodreads_id: # if this author is in goodreads author2 = Author.query.filter_by(goodreads_id=goodreads_id).first() if author2: # if the author is already in the database, but under a different spelling return jsonify({"auth_status": "ok", "id": author2.author_id}) else: # if author is not in database db.session.add( Author(author_name=goodreads_name, goodreads_id=goodreads_id)) db.session.commit() new_auth = Author.query.filter_by( goodreads_id=goodreads_id).first() return jsonify({"auth_status": "ok", "id": new_auth.author_id}) else: # author is not in goodreads return jsonify({"auth_status": "error"})
def load_author(soup): """loads author from raw_poem file into db""" name = Parse.parse_name(soup) try: author = Author.query.filter_by(name = name).one() return author except NoResultFound: region = Parse.parse_region(soup) affiliation = Parse.parse_school(soup) try: region = Region.query.filter(Region.region_name == region).one().region_id except NoResultFound: log_err('region', name.encode('unicode-escape'), region) try: affiliation = Affiliation.query.filter(Affiliation.affiliation_name == affiliation).one().affiliation_id except NoResultFound: log_err('affiliation', name.encode('unicode-escape'), affiliation) author = Author(name=name, region_id=region, affiliation_id=affiliation) db.session.add(author) db.session.flush() return author except MultipleResultsFound: print 'multiple results found for author name. db corrupted. investigate!'
def create_author(name=''): """Create an author.""" author = Author(name=name) db.session.add(author) db.session.commit() return author
def authors_from_json(self, json): def none_to_emptystr(s): if s is None: return '' return s return [Author(surname=author['surname'], names=[none_to_emptystr(author['given-name'])]) for author in json]
def load(string): parsed = xml.dom.minidom.parseString(string) author = parsed.getElementsByTagName('author')[0] name = author.getElementsByTagName('name')[0].firstChild.data country = author.getElementsByTagName('country')[0].firstChild.data years = author.getElementsByTagName('years')[0] return [Author(name=name, country=country, years='-'.join((years.attributes['born'].value, years.attributes['died'].value)))]
def accept(self): birth_year = self.ui.birth_year.text() death_year = self.ui.death_year.text() self.author = Author( name=self.ui.name.text(), country=self.ui.country.text(), years='-'.join( (birth_year, death_year)) if len(death_year) > 0 else birth_year) self.parent().db.insert_author(self.author) self.close()
def scan_city(city: CitySearch, data_dir, delay): for results_page in api.get_ads(city, 2): res_parser = Parser(url, results_page) time.sleep(delay) for ad in res_parser.parse_search_results(): dir = data_dir + ad.id create_if_no_exists(dir) print("Reading ad: %s" % ad.url) ad_page = api.get_ad_page(ad.url) ad_parser = Parser(url, ad_page) if not ad_parser.details_page_has_pics(): time.sleep(delay) print( "Parser detected that page had no photos block. Try reload page..." ) ad_page = api.get_ad_page(ad.url) ad_parser = Parser(url, ad_page) save_to_file(ad_page, dir + '/ad.html') data_file = dir + '/ad.json' prev_record = read_record(data_file) ad.details = ad_parser.parse_property_details() user_ids = ad_parser.parse_user_ids() user_data = api.get_user_data(user_ids[0], user_ids[1], user_ids[2]) ad.author = Author(user_ids[0], user_data['public_name'], user_data['mobile'], user_data['verified_user'] == '1', user_data['_links']['self']['href']) if prev_record is not None: ad.created = prev_record.created ad.city = city.name save_to_file(to_json(ad), data_file) print("Saved ad %s" % ad.id) pic_dir = dir + '/img' create_if_no_exists(pic_dir) for p_url in ad.details.imgs: content = api.get_image(p_url) img_name = p_url.rsplit('/', 1)[-1] img_url = pic_dir + '/' + img_name if os.path.exists(img_url): continue with open(img_url, "wb") as img_file: img_file.write(content) time.sleep(delay)
def save_author(self, author_url, author_name, author_twitter, author_bio): author = Author.query.filter(Author.url == author_url).first() if (isinstance(author, Author) == False): author = Author() author.url = author_url author.name = author_name author.twitter = author_twitter author.bio = author_bio db.session.add(author) db.session.commit() db.session.refresh(author) return author
def get_record(self): try: author_idx = int( self.id_pattern.findall(self.fd.readline())[0].strip()) except IndexError: raise EOFError author_name = unicode( self.name_pattern.findall(self.fd.readline())[0].strip(), 'utf-8') affiliation = unicode( self.affiliation_pattern.findall(self.fd.readline())[0].strip(), 'utf-8') line = self.fd.readline() # it matches the PC pattern.. if self.published_count_pattern.match(line): # stuff it into PC published_count = int( self.published_count_pattern.findall(line)[0].strip()) else: # probably an errant continuation of affiliation affiliation = affiliation + unicode( line) # so concatenate it to existing affiliation line # and then read in the next line as published_count published_count = int( self.published_count_pattern.findall( self.fd.readline())[0].strip()) citation_count = int( self.citations_pattern.findall(self.fd.readline())[0].strip()) hindex = self.hindex_pattern.findall(self.fd.readline())[0].strip() pindex = self.pindex_pattern.findall(self.fd.readline())[0].strip() upindex = self.upindex_pattern.findall(self.fd.readline())[0].strip() terms = unicode( self.terms_pattern.findall(self.fd.readline())[0].strip(), 'utf-8') line = self.fd.readline() # there's an error in like 8181450 where a term list spans 2 lines if line.strip(): terms = terms + line self.fd.readline() # skip next line else: pass # already skipped terms = [a for a in terms.split(';') if a] return Author(author_idx=author_idx, name=author_name, affiliation=affiliation, pc=published_count, cc=citation_count, hindex=hindex, pindex=pindex, upindex=upindex, terms=terms)
def export_authors(self): indexes = self.ui.authors.selectedIndexes() if len(indexes) < 1: return dialog = QFileDialog(self) dialog.setNameFilters(self.nameFilters.keys()) dialog.setAcceptMode(dialog.AcceptSave) if dialog.exec(): loader = self.nameFilters[dialog.selectedNameFilter()] with open(dialog.selectedFiles()[0], 'wt') as file: model = self.ui.authors.model() name, country, years = (model.item(indexes[0].row(), i).text() for i in range(model.columnCount())) file.write(loader.dump(Author(name=name, country=country, years=years)))
def add_author(author_name): """ Creates Author object, check if it already exists in db & adds it.""" # Creates an instance of the Author class author = Author(name=author_name) try: # Check if author exists & do not add them if so. check_author = Author.query.filter(Author.name == author_name).one() except NoResultFound: # If we get this error, then author doesn't exist yet. Add/commit to db db.session.add(author) db.session.commit() check_author = Author.query.filter(Author.name == author_name).one() return check_author
def _load_authors_from_worldcat_details_page(html_content): """ :param pq_html_content: :return: """ pq_page = pq(html_content) authors_string = pq_page('#bib-author-cell').text() author_list = [author.strip() for author in authors_string.split(';')] final_author_list = [] for author in author_list: current_author = Author() current_author.author_id = None current_author.author_name = author.strip() final_author_list.append(current_author) return final_author_list
def addAuthor(author_name): first_name, _, last_name = author_name.partition(" ") author = (session.query(Author).filter( and_(Author.first_name == first_name, Author.last_name == last_name)).one_or_none()) if author is None: author = Author(first_name=first_name, last_name=last_name) session.add(author) d = collections.OrderedDict() d["author_firstname"] = first_name d["author_lastname"] = last_name j = json.dumps(d) return j if author is not None: return "Author already exist"
def regist(): # try: data = request.get_json() print(data) username = data['newUsername'] password = data['newPassword'] user = Author.query.filter(Author.user_name == username).first() if user: return jsonify({'status':'1','msg':'该用户名已存在,请重新输入~'}) else: try: id = myutils.createRandomString(10) user = Author(user_name=username, password=password,id=id) db.session.add(user) db.session.commit() return jsonify({'status': '0', 'msg': '注册成功~'}) except: db.session.rollback() return jsonify({'status':'2','msg':'系统异常,请稍后再试或联系管理员~'})
def get(self): siteurl = self.request.get("site-url").strip() name = self.request.get("name").strip() site = self.request.get("site").strip() try: exist = Author.gql("WHERE name = :1 and site = :2 LIMIT 1", name, site) if exist.count(1) > 0: a = exist.get() if not a.siteurl is siteurl: a.siteurl = siteurl a.put() self.jsonout(status="dup", msg="%s existed with id %d", format=(a.name, a.key().id()), id=a.key().id(), key=str(a.key())) return auth = Author( name=name, site=site, siteurl=siteurl, ) auth.put() increment("Author") increment("Author-%s" % site) self.jsonout(status="ok", msg="%s added as author with id: %d", format=(auth.name, auth.key().id()), id=auth.key().id(), key=str(auth.key())) except BadValueError, e: self.jsonout( status="error", msg="BadValue: %s. (%s)", format=(e, self.request.GET), )
def fromFullName(self, fullname): author = Author() if fullname == "": return author first_names = [] last_names = [] # Split full name by space for word in fullname.split(" "): # If name is uppercased, it's a lastname if (word.isupper()): last_names.append(word) else: first_names.append(word) author.first_name = " ".join(first_names) author.last_name = " ".join(last_names) return author
def parse_author(author_xml_element): """Parse an author from an author xml element. The author xml element has been extracted from an xml file exported from Pubmed. Parameters ---------- xml.etree.ElementTree.Element corresponding to an author. Returns ------- Author object. None if no author could be parsed from the xml element. """ last_name = author_xml_element.findtext('LastName') fore_name = author_xml_element.findtext('ForeName') # Return None is an author element has no last name if last_name is None: return None # Accept authors that have only last name, but no fore names/initials if fore_name is None: first_name = None first_initial = None middle_initials = None else: names = fore_name.split() first_name = names[0] initials = extract_initials(fore_name) first_initial, middle_initials = initials[0], initials[1:] # Initialize an Author # print(first_initial, first_name, middle_initials, last_name) # DEBUG author = Author(first_initial=first_initial, first_name=first_name, middle_initials=middle_initials, last_name=last_name ) return author
def test_articles_to_xml(self): articles = [] per = Periodique() per.id = 1 per.name = "Le Seigneur des anneaux" bull = Bulletin() bull.title = "La communaute de l anneau" bull.number = "1" bull.period = "1960" bull.periodique = per article = Article() article.title = "Concerning hobbit" article.pagination = "p. 1-100" article.language = 'fre' author = Author() author.last_name = 'TOLKIEN' author.first_name = 'J.R.R' article.authors.append(author) article.bulletin = bull article.periodique = per articles.append(article) article = Article() article.title = "La comte" article.pagination = "p. 101-200" article.language = 'fre' article.authors.append(author) article.bulletin = bull article.periodique = per articles.append(article) conv = XmlConverter() flow = conv._toXml(articles) xml = len(etree.tostring(flow)) xml_proof = len( '<unimarc><notice><rs>n</rs><dt>a</dt><bl>a</bl><hl>2</hl><el>1</el><rs>i</rs><f c="200"><s c="a">Concerning hobbit</s></f><f c="101"><s c="a">fre</s></f><f c="215"><s c="a">p. 1-100</s></f><f c="700"><s c="a">TOLKIEN</s><s c="b">J.R.R</s><s c="4">070</s></f><f c="461"><s c="t">Le Seigneur des anneaux</s><s c="9">id:1</s><s c="9">lnk:perio</s></f><f c="463"><s c="t">La communaute de l anneau</s><s c="e">1960</s><s c="v">1</s><s c="9">lnk:bull</s></f></notice><notice><rs>n</rs><dt>a</dt><bl>a</bl><hl>2</hl><el>1</el><rs>i</rs><f c="200"><s c="a">La comte</s></f><f c="101"><s c="a">fre</s></f><f c="215"><s c="a">p. 101-200</s></f><f c="700"><s c="a">TOLKIEN</s><s c="b">J.R.R</s><s c="4">070</s></f><f c="461"><s c="t">Le Seigneur des anneaux</s><s c="9">id:1</s><s c="9">lnk:perio</s></f><f c="463"><s c="t">La communaute de l anneau</s><s c="e">1960</s><s c="v">1</s><s c="9">lnk:bull</s></f></notice></unimarc>' ) self.assertEqual(xml, xml_proof)
def _create_author(name, email, description): return Author(name, email, description)
def create_author(first_name,last_name): author = Author(first_name= first_name,last_name=last_name) db.session.add(author) db.session.commit() return author
def search_by_author(): """ Returns list of series associated with author""" author_name = request.form.get("author") author = Author.query.filter_by(author_name=author_name).first() if author: # author is in database title = "Series by {}".format(author_name) if author.goodreads_id: # if author has goodreads id series_info = get_series_list_by_author(author.goodreads_id) if series_info is not None: # if we get results return render_template("series_results.html", series=series_info, title=title, tfs=timeframes) else: # did not get results/returned None flash("An error occured. Please try again", "danger") return redirect("/adv-search") else: # author does not have a goodreads id actual_id = get_author_goodreads_info(author_name)[0] if actual_id: # if you get a goodreads id author.goodreads_id = actual_id db.session.commit() series_info = get_series_list_by_author(actual_id) if series_info is not None: # if everything worked return render_template("series_results.html", series=series_info, title=title, tfs=timeframes) else: # error occured/returned None flash("An error occured. Please try again", "danger") return redirect("/adv-search") else: flash( "I'm sorry, we can't find info on this author in our external resources. Sorry about that!", "warning") return redirect("/adv-search") else: # author is not in database possible_id, possible_name = get_author_goodreads_info(author_name) if possible_id: possible_author = Author.query.filter_by( goodreads_id=possible_id).first() if possible_author: # if there was a typo that goodreads handled series_info = get_series_list_by_author( possible_author.goodreads_id) if series_info is not None: # if we get results flash( "Showing results for {} instead of {}".format( possible_author.author_name, author_name), "info") title = "Series by {}".format(possible_author.author_name) return render_template("series_results.html", series=series_info, title=title, tfs=timeframes) else: # did not get results/returned None flash("An error occured. Please try again", "danger") return redirect("/adv-search") else: # author is not in database db.session.add( Author(author_name=possible_name, goodreads_id=possible_id)) db.session.commit() series_info = get_series_list_by_author(possible_id) if series_info is not None: # if we get results if author_name != possible_name: flash( "Showing results for {} instead of {}".format( possible_name, author_name), "info") title = "Series by {}".format(possible_name) return render_template("series_results.html", series=series_info, title=title, tfs=timeframes) else: # did not get results/returned None flash("An error occured. Please try again", "danger") return redirect("/adv-search") else: # if no author id is returned flash("Could not find an author with that name. Please try again.", "warning") return redirect("/adv-search")
def update_authors(): """ Updates who is the user's favorites authors """ user_id = session.get("user_id") unfav_authors = request.form.getlist("author-remove") new_authors = request.form.get("new-author").strip().splitlines() # might change how I flash messages if unfav_authors: auth_names = "Successfully removed:" for author_id in unfav_authors: author = Fav_Author.query.filter_by(author_id=author_id, user_id=user_id).first() if author: db.session.delete(author) auth_names += " {},".format(author.author.author_name) flash(auth_names.rstrip(","), "success") if new_authors: auth_fav = "" auth_add = "" for author_name in new_authors: author = Author.query.filter_by(author_name=author_name).first() if author: # if author in database if Fav_Author.query.filter_by(author_id=author.author_id, user_id=user_id).first(): auth_fav += " {},".format(author.author_name) else: db.session.add( Fav_Author(author_id=author.author_id, user_id=user_id)) auth_add += " {},".format(author_name) else: # if author is NOT in database gr_id, gr_name = get_author_goodreads_info(author_name) if gr_id: if author_name != gr_name: # name entered and name returned from goodreads do not match (indicates typo) flash( "Could not find {}. Did you mean {}?".format( author_name, gr_name), "info") else: # author is definitely NOT in database db.session.add( Author(author_name=gr_name, goodreads_id=gr_id)) db.session.commit() author_id = Author.query.filter_by( author_name=author_name).first().author_id db.session.add( Fav_Author(author_id=author_id, user_id=user_id)) auth_add += " {},".format(author_name) else: # author is not in Goodreaeds flash( "{} is not in Goodreads, so cannot add to favorites. Sorry!" .format(author_name), "warning") if auth_fav: flash("You already liked:{}".format(auth_fav.rstrip(",")), "warning") if auth_add: flash("Successfully added:{}".format(auth_add.rstrip(",")), "success") db.session.commit() return redirect("/user/{}".format(user_id))
#!/usr/bin/env python # -*- coding: utf-8 -*- from model import Author author_parse_test_cases = [ (u'Surname, First', Author(u'Surname', [u'First'])), (u'Surname, First Second', Author(u'Surname', [u'First', u'Second'])), (u'Smith JD', Author(u'Smith', [u'J.', u'D.'])), (u'Smith J.D.', Author(u'Smith', [u'J.', u'D.'])), (u'Smith, J.D.', Author(u'Smith', [u'J.', u'D.'])), (u'Smith J. D.', Author(u'Smith', [u'J.', u'D.'])), (u'Smith, J. D.', Author(u'Smith', [u'J.', u'D.'])), (u'Cheng Kai-Tei', Author(u'Cheng', [u'Kai', u'Tei'])), (u'Cheng K-T', Author(u'Cheng', [u'K.', u'T.'])), (u'Cheng K.-T.', Author(u'Cheng', [u'K.', u'T.'])), (u'Cheng K-T', Author(u'Cheng', [u'K.', u'T.'])), (u'Cheng K-T', Author(u'Cheng', [u'K.', u'T.'])), (u'Nobrega de Sousa, V.', Author(u'Nobrega de Sousa', [u'V.'])), (u'Nobrega de Sousa V.', Author(u'Nobrega de Sousa', [u'V.'])), (u'Nobrega de Sousa V', Author(u'Nobrega de Sousa', [u'V.'])), (u'Nobrega de Sousa V', Author(u'Nobrega de Sousa', [u'V.'])), (u'de Sousa V.', Author(u'de Sousa', [u'V.'])), (u'de Sousa Vito', Author(u'de Sousa', [u'Vito'])), (u'de Menezes Neto, R.', Author(u'de Menezes Neto', [u'R.'])), (u'de Menezes Neto R.', Author(u'de Menezes Neto', [u'R.'])), (u'McDonald R.', Author(u'McDonald', [u'R.'])), ] author_short_name_test_cases = [ (Author(u'MCDONALD', [u'R.']), u'McDonald, R.'),
def login(self, login: Login) -> bool: user = self.user_repo.find_user_by_email(login.email) if user != None and user.check_password(login.password): self.logged_user = user return True else: self.logged_user = None return False def get_logged_user(self) -> Optional[User]: return self.logged_user if __name__ == '__main__': users = [ Author('Ivan Petrov', '*****@*****.**', 'ivanp123'), Admin('Admin Admin', '*****@*****.**', 'admin123', '35928976564'), Admin('Nadezda Hristova', '*****@*****.**', 'nadia123', '3592754632'), ] user_repo = UserRepository(users) print(f'Repository has {len(user_repo)} users.') user_repo.add_user( Author('Hrisitna Dimitrova', '*****@*****.**', 'hristina', 'expert')) user_repo.add_user( Author('Ivan Pavlov', '*****@*****.**', 'ivan123', 'professional')) for user in user_repo: print('+', user) print(f'Repository has {len(user_repo)} users.')
def get_current_user(self): authorurl = self.get_secure_cookie('user') if not authorurl: return None return Author().load(authorurl, db=self.db)
def parse_loaded_dict(loaded_dict): return Author(name=loaded_dict['name'], country=loaded_dict['country'], years='-'.join(map(str, loaded_dict['years'])) if len(loaded_dict['years']) > 1 else ''.join(loaded_dict['years']))