Beispiel #1
0
def get_author(dat):
    if isinstance(dat, int):
        auth = Author.get_by_id(dat)
        if not auth is None:
            return auth

        q = Author.gql("WHERE name = :1", dat)
        if q.count(1) > 0:
            auth = q.fetch(1)
            return auth[0]

        raise KeyError("%s not found in authors" % dat)
Beispiel #2
0
def get_author(dat):
    if isinstance(dat,int):
        auth = Author.get_by_id(dat)
        if not auth is None:
            return auth

        q = Author.gql("WHERE name = :1",dat)
        if q.count(1) >0:
            auth = q.fetch(1)
            return auth[0]

        raise KeyError("%s not found in authors"%dat)
Beispiel #3
0
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()
Beispiel #4
0
    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
Beispiel #5
0
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"})
Beispiel #6
0
    def _list_to_publications(self, l):
        pubs = []
        for data in l:
            authors = []
            authors_incomplete = False
            if 'Author(s)' in data:
                for author_text in (unicode(x)
                                    for x in data['Author(s)'].split(';')):
                    if author_text.strip() == u'et al.':
                        authors_incomplete = True
                    else:
                        authors.append(
                            Author.parse_sn_first(author_text.strip()))
            else:
                authors_incomplete = True

            pub = Publication(unicode(data['Title']), authors,
                              int(data['Year']))
            if 'Source' in data:
                pub.published_in = unicode(data['Source'])
            if 'Book Series' in data:
                pub.series = unicode(data['Book Series'])
            if 'Volume' in data:
                pub.volume = unicode(data['Volume'])
            if 'Pages' in data:
                pub.pages = unicode(data['Pages'])
            if 'Issue' in data:
                pub.issue = unicode(data['Issue'])
            if 'Special Issue' in data:
                pub.special_issue = unicode(data['Special Issue'])
            if 'Supplement' in data:
                pub.supplement = unicode(data['Supplement'])
            pub.authors_incomplete = authors_incomplete
            pubs.append(pub)
        return pubs
Beispiel #7
0
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!'
Beispiel #8
0
 def get(self,id):
     a = Author.get_by_id(int(id))
     if not a is None:
         templatevars = { "author": a}
         templatevars["title"]= "PREOMR - %s"%a.name
         self.generate("authorread.html",templatevars)
     else:
         self.generate("error.html",{"traceback":"Author not found"})
Beispiel #9
0
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
Beispiel #10
0
 def get(self, id):
     a = Author.get_by_id(int(id))
     if not a is None:
         templatevars = {"author": a}
         templatevars["title"] = "PREOMR - %s" % a.name
         self.generate("authorread.html", templatevars)
     else:
         self.generate("error.html", {"traceback": "Author not found"})
Beispiel #11
0
    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]
Beispiel #12
0
def create_author(name=''):
    """Create an author."""

    author = Author(name=name)

    db.session.add(author)
    db.session.commit()

    return author
 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)))]
Beispiel #14
0
def check_parsed_author(source, expected):
    parsed = Author.parse_sn_first(source)
    try:
        assert parsed.surname == expected.surname
        assert len(parsed.names) == len(expected.names)
        for i in range(len(parsed.names)):
            assert parsed.names[i] == expected.names[i], i
    except AssertionError:
        print "{!r} != {!r}".format(parsed, expected)
        raise
Beispiel #15
0
 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()
Beispiel #16
0
    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
Beispiel #17
0
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)
Beispiel #18
0
    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)
Beispiel #19
0
 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)
Beispiel #20
0
 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)))
Beispiel #21
0
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
Beispiel #22
0
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"
Beispiel #23
0
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':'系统异常,请稍后再试或联系管理员~'})
Beispiel #24
0
    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
Beispiel #25
0
    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),
            )
Beispiel #26
0
    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),
                        )
Beispiel #27
0
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 _create_author(name, email, description):
     return Author(name, email, description)
Beispiel #29
0
#!/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.'),
Beispiel #30
0
import peewee
from model import Author
from model import Book

if __name__ == '__main__':
    try:
        Author.create_table()
    except peewee.OperationalError:
        print('Author table already exists')

    try:
        Book.create_table()
    except peewee.OperationalError:
        print('Book table already exists')

    author01 = Author.create(name='H. G. Wells')
    book01 = {'title': 'A Máquina do tempo', 'author': author01}
    book02 = {'title': 'Guerra dos Mundos', 'author': author01}

    author02 = Author.create(name='Julio Verne')
    book03 = {'title': 'Volta ao mundo em 80 dias', 'author': author02}
    book04 = {'title': 'Vinte Mil Leguas Submarinas', 'author': author01}

    books = [book01, book02, book03, book04]

    Book.insert_many(books).execute()

    book = Book.get(Book.title == 'Volta ao mundo em 80 dias')
    print(book.title)

    books = Book.select().join(Author).where(Author.name == 'H. G. Wells')
Beispiel #31
0
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
Beispiel #32
0
    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 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']))
Beispiel #34
0
 def convert_name_to_author(name):
     author = Author()
     author.name = name
     return author
Beispiel #35
0
 def get_current_user(self):
     authorurl = self.get_secure_cookie('user')
     if not authorurl: return None
     return Author().load(authorurl, db=self.db)