def generateDataFromAPI(keyword, order_By, critics_Picks): my_key_dict = main_functions.read_from_file( "Project2_Flask/JSON_Documents/api_keys.json") my_key = my_key_dict["my_ny_key"] url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=" + keyword + "&order=" + order_By + "&critics-pick=" + critics_Picks + "&api-key=" + my_key response = requests.get(url).json() main_functions.save_to_file(response, "Project2_Flask/JSON_Documents/response.json") response_dict = main_functions.read_from_file( "Project2_Flask/JSON_Documents/response.json") data_requested = {} movieTitles = [] for i in response_dict["results"]: movieTitles.append(i["display_title"]) critics = [] for i in response_dict["results"]: critics.append(i["byline"]) publicationDates = [] for i in response_dict["results"]: publicationDates.append(i["publication_date"]) reviewUrls = [] for i in response_dict["results"]: reviewUrls.append(i["link"]["url"]) """From the response dictionary, you need to filter the data requested by the user""" data_requested["titles"] = movieTitles data_requested["critics"] = critics data_requested["publicationDates"] = publicationDates data_requested["reviewLinks"] = reviewUrls return data_requested
def generateDataFromAPI(): my_key_dict = main_functions.read_from_file( "Project2_Flask/JSON_Files/api_key.json") my_key = my_key_dict["my_key"] url = "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?api-key=" + my_key response = requests.get(url).json() main_functions.save_to_file(response, "Project2_Flask/JSON_Files/response.json") bestselling_books = main_functions.read_from_file( "Project2_Flask/JSON_Files/response.json") print(bestselling_books.get("status")) # find out if a book is best selling data_requested = bestselling_books return data_requested
def movie_search(title, order, critics_picks): api_key_dict = main_functions.read_from_file( "Project2_Flask/JSON_Documents/api_keys.json") api_key = api_key_dict["my_ny_key"] url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=" + title + "&" + "critics-pick=" + critics_picks + "&" + "order=" + order + ";" + "&api-key=" + api_key response = requests.get(url).json() "https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=godfather&api-key=yourkey" main_functions.save_to_file(response, "Project2_Flask/JSON_Documents/response.json") response_dict = main_functions.read_from_file( "Project2_Flask/JSON_Documents/response.json") print(url) return response_dict
def search(): my_form = forms.NewsForm(request.form) if request.method == 'POST': first_name = request.form["first_name"] date = request.form["date"] list_name = request.form["list_name"] # Couldn't figure out how to run this from forms :/ sorry url = "https://api.nytimes.com/svc/books/v3/lists/" + date + "/" + list_name + ".json?api-key=" final_url = url + api_key response_ = requests.get(final_url).json() main_functions.save_to_file(response_, "Project2_Flask/JSON_Files/response.json") data = main_functions.read_from_file( "Project2_Flask/JSON_Files/response.json") book1 = data['results']['books'][0]['title'] book2 = data['results']['books'][1]['title'] book3 = data['results']['books'][2]['title'] book4 = data['results']['books'][3]['title'] book5 = data['results']['books'][4]['title'] response = [first_name, book1, book2, book3, book4, book5] return render_template('results.html', response=response, form=my_form) return render_template('search.html', form=my_form)
def generateDataFromAPI(): url = "https://api.nytimes.com/svc/books/v3/lists/" + date + "/" + list_name + ".json" final_url = url + api_key response = requests.get(final_url).json() main_functions.save_to_file(response, "Project2_Flask/JSON_Files/response.json") response_dict = main_functions.read_from_file( "Project2_Flask/JSON_Files/response.json")
def generateDataFromAPI(self): url = "https://api.nytimes.com/svc/books/v3/reviews.json?author=" my_key_dict = main_functions.read_from_file( "Project2_Flask/JSON_Files/api_keys.json") my_key = my_key_dict["my_api_key"] if NewsForm.author == "Haruki Murakami": user_author = "Haruki+Murakami" elif NewsForm.author == "Yukio Mishima": user_author = "Yukio+Mishima" elif NewsForm.author == "Kazuo Ishiguro": user_author = "Kazuo+Ishiguro" else: user_author = "Junichirō+Tanizaki" # Final URL final_url = url + user_author + "&api-key=" + my_key """Maybe you need to provide extra information in the API url""" response = requests.get(final_url).json() # Save info to response.json main_functions.save_to_file(response, "Project2_Flask/JSON_Files/response.json") """From the response dictionary, you need to filter the data requested by the user""" my_books = main_functions.read_from_file( "Project2_Flask/JSON_Files/response.json") book_t = response.json()['results'] book_title = [] for b in book_t: title = b['book_title'] book_title.append(title) return book_title
def getList(): list = [] #list of format "display_name","encoded_name","oldest_date" url = "https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=" api_key = main_functions.read_from_file( "Project2_Flask/jsons/api_key.json")["NYT_API_Project2_Key"] list_url = url + api_key #main_functions.save_to_file(requests.get(list_url).json(),"jsons/bestselling_list.json") request_response = requests.get(list_url).json() for result in request_response["results"]: entry = [ result["display_name"], result["list_name_encoded"], result["oldest_published_date"] ] list.append(entry) #print(list) return list
def generate_data_from_books_api(data_user_wants): if data_user_wants[0] == 'list': url = "https://api.nytimes.com/svc/books/v3/lists.json?" if data_user_wants[4] == 'date': date = data_user_wants[5] if date != '' or date != ' ': url = "https://api.nytimes.com/svc/books/v3/lists/{}/hardcover-fiction.json?".format( date) else: url = "https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?" elif data_user_wants[4] == 'history': url = "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?" elif data_user_wants[4] == 'overview': url = "https://api.nytimes.com/svc/books/v3/lists/overview.json?" elif data_user_wants[0] == 'reviews': url = "https://api.nytimes.com/svc/books/v3/reviews.json?" if (data_user_wants[1] != "" or data_user_wants[1] != " ") and data_user_wants[0] == 'reviews': author_name = add_plus_to_string(data_user_wants[1]) url = url + "author={}&".format(author_name) if (data_user_wants[2] != "" or data_user_wants[2] != " ") and data_user_wants[0] == 'reviews': book_title = add_plus_to_string(data_user_wants[2]) url = url + "title={}&".format(book_title) if (data_user_wants[3] != "" or data_user_wants[3] != " ") and data_user_wants[0] == 'reviews': isbn = str(data_user_wants[3]) url = url + "isbn={}&".format(isbn) api_key = main_functions.read_from_file( "Project2_Flask/JSON_Files/api_key.json") my_key = api_key["my_api_key"] final_url = url + 'api-key=' + my_key response = requests.get(final_url).json() main_functions.save_into_file(response, "Project2_Flask/JSON_Files/response.json") response_dict = main_functions.read_from_file( "Project2_Flask/JSON_Files/response.json") return response_dict
from Project2_Flask import app, main_functions from flask_wtf import FlaskForm from wtforms import StringField, IntegerField, DateField, RadioField, SelectField import requests api_key_dict = main_functions.read_from_file( "Project2_Flask/JSON_Files/api_keys.json") api_key = api_key_dict["my_ny_key"] class NewsForm(FlaskForm): first_name = StringField("First Name") date = RadioField("Choose Date", choices=[('', 'Current'), ('2020-08-01', 'August'), ('2020-09-01', 'September'), ('2020-10-01', 'October'), ('2020-11-01', 'November')]) list_name = SelectField("List Name", choices=[('hardcover-fiction', 'Hardcover Fiction'), ('hardcover-nonfiction', 'Hardcover Nonfiction')]) def generateDataFromAPI(): url = "https://api.nytimes.com/svc/books/v3/lists/" + date + "/" + list_name + ".json" final_url = url + api_key response = requests.get(final_url).json()
def getBooks(list, limit): url = "https://api.nytimes.com/svc/books/v3/lists/current/" + list + ".json?api-key=" + main_functions.read_from_file( "Project2_Flask/jsons/api_key.json")["NYT_API_Project2_Key"] book_list = requests.get(url).json() curr_entry = [] if limit <= 0: for entry in book_list["results"]["books"]: tup = [ entry["book_image"], entry["title"], entry["author"], entry["publisher"], entry["description"], entry["amazon_product_url"], entry["book_review_link"] ] curr_entry.append(tup) return curr_entry counter = 0 for entry in book_list["results"]["books"]: if counter >= limit: break tup = [ entry["book_image"], entry["title"], entry["author"], entry["publisher"], entry["description"], entry["amazon_product_url"], entry["book_review_link"] ] curr_entry.append(tup) counter += 1 return curr_entry