whx

.py

School

University of Southern California *

*We aren’t endorsed by this school

Course

551

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

3

Uploaded by PresidentIce13809

Report
# IMPORT LIBRARIES import json import sys import requests # Firebase Database URLs (Replace these URLs with your actual Firebase URLs) DATABASE_URLS = { 0: "https://dsci551-hw1-1-d0416-default-rtdb.firebaseio.com/", 1: "https://dsci551-hw1-2-8d859-default-rtdb.firebaseio.com/" } ## Define any global methods # HERE # Give hash value to the book according to the author name firebase1_dic = {} firebase2_dic = {} def hash(author_name): hash_code = 0 if author_name[0] <= "M": hash_code = 0 else: hash_code = 1 return hash_code #def add_firebase(book_hash, book_id, book_json): # if book_id in firebase1_dic.keys(): # print("Error! The book already exists in firebase1") # if book_id in firebase2_dic.keys(): # print("Error! The book already exists in firebase2") # book_json_python = json.loads(book_json) # if book_hash == 0: # firebase1_dic[book_id] = book_json_python # if book_hash == 1: # firebase2_dic[book_id] = book_json_python # firebase1 = json.dumps(firebase1_dic) # firebase2 = json.dumps(firebase2_dic) # return firebase1, firebase2 def add_book(book_id, book_json): # INPUT : book id and book json from command line # RETURN : status code after pyhton REST call to add book [response.status_code] # EXPECTED RETURN : 200 #method lose pt #author_name = json.loads(book_json)["author"] #book_hash = hash(author_name) #add_firebase(book_hash, book_id, book_json) #return book_author = json.loads(book_json)['author'] hash_code = hash(book_author) which_url = DATABASE_URLS[hash_code] result_in_firebase = requests.put(which_url + "books/" + book_id + ".json", data=book_json)
return result_in_firebase.status_code def search_by_author(author_name): # INPUT: Name of the author # RETURN: JSON object having book_ids as keys and book information as value [book_json] published by that author # EXPECTED RETURN TYPE: {'102': {'author': ... , 'price': ..., 'title': ..., 'year': ...}, '104': {'author': , 'price': , 'title': , 'year': }} #get which firebase the book belongs to author_result = {} hashcode = hash(author_name) database_url = DATABASE_URLS[hashcode] response = requests.get(database_url + "/books.json") if response.status_code == 200: decoded_data = response.content.decode('utf-8') books_data = json.loads(decoded_data) for key, value in books_data.items(): if value["author"] == author_name: author_result[key] = value else: print("Error! Can't get the firebase") author_result = json.dumps(author_result) return author_result def search_by_year(year): # INPUT: Year when the book published # RETURN: JSON object having book_ids as key and book information as value [book_json] published in that year # EXPECTED RETURN TYPE: {'102': {'author': ... , 'price': ..., 'title': ..., 'year': ...}, '104': {'author': , 'price': , 'title': , 'year': }} #year_result = {} #for key, value in firebase1_dic.items(): # if value["year"] == year: # year_result[key] = value #for key, value in firebase2_dic.items(): # if value["year"] == year: # year_result[key] = value #year_result = json.dumps(year_result) #return year_result # result_year = {} # for hashcode in DATABASE_URLS: # database_url = DATABASE_URLS[hashcode] # response = requests.get(database_url + "/books.json") # if response.status_code == 200: # decoded_data = response.content.decode('utf-8') # books_data = json.loads(decoded_data) # for book_id, book_json in books_data.items(): # if book_json['year'] == year: # result_year[book_id] = book_json
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help