utils.py 2.93 KB
Newer Older
1
2
3
# standard libary imports
from datetime import datetime
# third party imports
4
import requests
5
# imports from your apps
6
7
8
from .models import Flag, Rating, BidFlag


9
10
# previously pulled worldcat metadata from ISBNs, worldcat is now a paid service
# thankfully, the internet archive has stepped in with a free api replacement
11
def ISBNMetadata(standardISBN):
12
13
14
15
16
    # supports both ISBN 10 and ISBN 13
    key_format = 'ISBN:%s' % str(standardISBN)
    url = "https://openlibrary.org/api/books?bibkeys=" +\
          key_format +\
          "&format=json&jscmd=data"
17
18
19
20
21
22
23
24

    # In case the API fails to return, simply return None.
    try:
        metadata = requests.get(url, timeout=3)
    except requests.ConnectionError:
        return None

    # format into a dictionary
25
26
    json_response = metadata.json()
    isbn_data = json_response.get(key_format)
27
    try:
28
29
30
31
32
33
        title = isbn_data.get('title', '')
        subtitle = isbn_data.get('subtitle', '')
        if subtitle:
            full_title = '%s: %s' % (title, subtitle)
        else:
            full_title = title
34
        metadataDict = {'title': full_title.title()}
35
36
37
38
39
40
41
42
43
44
45
46

        date = isbn_data.get('publish_date' ,'')
        # unfortunately, dates are formatted in a variety of different ways
        # but year should be the final four digits regardless of, say,  month formatting
        metadataDict['year'] = date[-4:]

        authors = isbn_data.get('authors', 'wat')
        metadataDict['authors'] = ' and '.join([author.get('name', '')
                                                for author in authors])

        # unlike worldcat, openlibrary does not provide edition information
        return metadataDict
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    except:
        return None


# flagging
# you can only flag a listing once...
def can_flag(flagger, listing):
    user_flag_num = Flag.objects.filter(flagger=flagger,
                                        listing=listing).count()
    # we're assuming that this isn't going to go over 1
    if user_flag_num:
        return False
    else:
        return True


def can_flag_bid(flagger, bid):
    user_flag_num = BidFlag.objects.filter(flagger=flagger,
                                           bid=bid).count()
    if user_flag_num:
        return False
    else:
        return True


# get the listing's slug to pass to the create flag page
def flag_slug(flagger, listing):
    if not can_flag(flagger, listing):
        return Flag.objects.get(flagger=flagger, listing=listing).slug
    else:
        return None


def bid_flag_slug(flagger, bid):
    if not can_flag_bid(flagger, bid):
        return BidFlag.objects.get(flagger=flagger, bid=bid).slug
    else:
        return None


# rating
# (basically) duplicated code!!!
def can_rate(rater, listing):
    user_rate_num = Rating.objects.filter(rater=rater,
                                          listing=listing).count()
    # we're assuming that this isn't going to go over 1
    if user_rate_num:
        return False
    else:
        return True