-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbestsellers.py
83 lines (70 loc) · 2.46 KB
/
bestsellers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
import os
import requests
import json
from dotenv import load_dotenv
load_dotenv() # running this will create environment variables
api_key = os.getenv('api_key')
def bestsellers_category():
url = f"https://api.nytimes.com/svc/books/v3/lists/names.json?&api-key={api_key}"
response = requests.get(url)
results = response.json()['results']
# for name_dict in results:
# print(name_dict['list_name'] + ': ' + name_dict['list_name_encoded'])
def select_category(user_input):
# user_input = input('Enter a book category: ')
url = 'https://api.nytimes.com/svc/books/v3/lists/current/' + user_input + '.json?&api-key=' + api_key
output = requests.get(url)
data = output.json()
print(data['results']['books'])
entire_book_dic= {}
for book in data['results']['books']:
rank = book['rank']
isbn10 = book['primary_isbn10']
isbn13 = book['primary_isbn13']
publisher = book['publisher']
title = book['title']
author = book['author']
book_image = book['book_image']
product_url = book['amazon_product_url']
buy_links = book['buy_links']
entire_book_dic[book_image] = [title, author, product_url, isbn13]
return entire_book_dic
# print(rank, isbn10, isbn13, publisher, title, author, book_image, buy_links)
def homepage_bestsellers():
homepage_url = 'https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?&api-key=' + api_key
response = requests.get(homepage_url)
book_list = response.json()
entire_book_dic = {}
book_dic = {}
count = 0
home_book_lst = []
for book in book_list['results']['books']:
# print(count)
if(count==3):
count=0
home_book_lst.append(book_dic)
book_dic={}
rank = book['rank']
isbn10 = book['primary_isbn10']
isbn13 = book['primary_isbn13']
publisher = book['publisher']
title = book['title']
author = book['author']
book_image = book['book_image']
product_url = book['amazon_product_url']
buy_links = book['buy_links']
book_dic[book_image] = [title, author, product_url]
entire_book_dic[book_image] = [title, author, product_url, isbn13]
count+=1
# print(book_list['results']['books'])
# print(book_dic)
return home_book_lst,entire_book_dic
# def main():
# homepage_bestsellers()
# data = homepage_bestsellers()
# for key,value in data[0].items():
# print(value[2][0]['url']) #gets url
# print(key)#gets cover
# print(data[0])
# if __name__ == "__main__":
# main()