Esempio n. 1
0
    def get_authors_list(self, author_names):
        """
        Process the list of authors names, returns a
        list of corresponding objects, creates new objects
        if don't exist

        Args:
            author_names (List of String): Author names

        Returns:
            List of Athor: Author objects
        """

        authors = []

        for author_name in author_names:
            try:
                author = Author.objects.get(name=author_name)
                authors.append(author)
            except Author.DoesNotExist:
                author = Author()
                author.name = author_name
                author.save()
                authors.append(author)

        return authors
Esempio n. 2
0
    def get_authors(self, authors_string):
        """ Get authors object from string names
        sepparated by commas

        Args:
            authors_string (String): names separated by commas

        Returns:
            List of Author: List of author objects
        """

        names = self.separate(authors_string)

        authors = []

        for name in names:
            try:
                author = Author.objects.get(name=name)
                authors.append(author)
            except Author.DoesNotExist:
                author = Author()
                author.name = name
                author.save()
                authors.append(author)

        return authors
Esempio n. 3
0
def regFromDouban(request):
    '''利用豆瓣API获取书籍信息加入数据库'''
    try:
        DOUBAN = "https://api.douban.com/v2/book/search?q="
        q = request.REQUEST.get('q', '')
        q = urllib.quote(q.decode('utf8').encode('utf8'))  
        DOUBAN += q
    
        req = urllib.urlopen(DOUBAN)
        resp = req.read()
        result = simplejson.loads(resp)
        books = result['books']
        
        
        for i in range(len(books)):
            author = Author()
            author.name = books[i]['author'][0]
            author.desc = books[i]['author_intro']
            author.save()
            
            try:
                book = Book.objects.get(isbn=books[i]['isbn13'])
            except Book.DoesNotExist:
                book = Book()
                book.name = books[i]['title']
                book.author = author
                book.price = float(''.join([ item for item in books[i]['price'] if item in '1234567890.' ]))
                if not books[i]['isbn13']:
                    book.isbn = books[i]['isbn10']
                else:
                    book.isbn = books[i]['isbn13']
                book.press = books[i]['publisher']
                book.desc = books[i]['summary']
                book.binding = books[i]['binding']
                book.pages = books[i]['pages']
                book.spic = _downloadImg(books[i]['images']['small'], 'small')   
                book.mpic = _downloadImg(books[i]['images']['medium'], 'medium')   
                book.lpic = _downloadImg(books[i]['images']['large'], 'large')   
                book.stock = 140
                book.publish_date = books[i]['pubdate']
                book.category = _getBookCate('letter')
                book.save()
                
        return HttpResponse('success')
    except Exception:
        return HttpResponse('error')
Esempio n. 4
0
def import_authors(all_data, options):
    """"Import authors from JSON data."""

    for k, val in all_data["authors"].items():
        # If already there, overwrite:
        try:
            author = Author.objects.get(pk=k)
        except ObjectDoesNotExist:
            author = Author(pk=k)

        author.name = val["name"]

        msg = f"Imported [AUTHOR] {author}"
        print(msg)

        if not options.dry_run:
            author.save()