Newer
Older
import RegularExpressionParser.Parser as Parser
import Crawler.Main as Main
import API.Functions as API
import os
import json
from dotenv import load_dotenv
app = typer.Typer()
def main_page():
print("Welcome to use goodReads scraper!\n"
"This is main page\n"
"Usage:\n"
"scrape: scrape goodRead and load data to database\n"
"get: find a document for a book or an author from database\n"
"put: create or update a document in database\n"
"post: add one or multiple documents of book or author in database \n"
"delete: delete certain document from database\n"
"export: export a certain collection from database\n"
"exit: end the program")
order = input()
if order == "scrape":
scrape()
elif order == "get":
get()
elif order == "put":
put()
elif order == "post":
post()
elif order == "delete":
delete()
elif order == "export":
export()
elif order == "exit":
exit()
else:
print("Unknown command")
main_page()
def scrape():
"""
Function used to create scrape request to Server
Only for assignment 2.0, for GET scrape, find it in GET in SimplerServer.py
"""
print('please enter URL of the book')
url = input()
print('please enter the number of books you want to crawl')
book_num = input()
if not book_num.isdigit():
print("max book num must be an integer")
main_page()
book_num = int(book_num)
print('please enter the number of authors you want to crawl')
author_num = input()
if not author_num.isdigit():
print("max author num must be an integer")
main_page()
author_num = int(author_num)
print('Request received, start scraping')
if not Parser.check_if_address_valid(url):
print("Error: invalid URL")
main_page()
elif book_num >= 2000 or author_num >= 2000:
print("Error, the number of book and author should be lower than 2000")
main_page()
else:
if book_num > 200 or author_num > 50:
print("Warning, maximum of the number of books or authors is large")
Main.scrape_api(url, book_num, author_num)
main_page()
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
def get():
print("Please enter your query:")
query = input()
result = API.get(query)
typer.echo(result)
main_page()
def put():
print("Please enter your query:")
query = input()
print("Please select one way to load update info: input or read:")
load = input()
if load == "input":
print("Please enter json data:")
string = input()
try:
data = json.loads(string)
except ValueError as err:
print(err)
main_page()
elif load == "read":
load_dotenv()
file_root = os.getenv('FILE_ROOT')
print("Please enter the name of file which is in the file root directory:")
file_path = r"" + file_root + input()
if os.path.isfile(file_path):
with open(file_path, "r") as file:
try:
data = json.load(file)
except ValueError as err:
print(err)
main_page()
else:
print("Error: file not exist")
main_page()
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
print("Unknown command")
main_page()
result = API.put(query, data)
typer.echo(result)
main_page()
def post():
print("Please enter your query:")
query = input()
load_dotenv()
file_root = os.getenv('FILE_ROOT')
print("Please enter the name of file which is in the file root directory:")
file_path = r"" + file_root + input()
if os.path.isfile(file_path):
with open(file_path, "r") as file:
try:
data = json.load(file)
except ValueError as err:
print(err)
main_page()
else:
print("Error: file not exist")
main_page()
result = API.post(query, data)
typer.echo(result)
main_page()
def delete():
print("Please enter your query:")
query = input()
result = API.delete(query)
typer.echo(result)
main_page()
def export():
print("Which collection do you want to export? books or authors?")
collection = input()
if collection == "books":
json = API.get("book")
load_dotenv()
file_root = os.getenv('FILE_ROOT')
with open(file_root + "books" + ".json", "w") as output:
output.write(json)
print("books.json is stored at " + file_root)
main_page()
elif collection == "authors":
json = API.get("author")
load_dotenv()
file_root = os.getenv('FILE_ROOT')
with open(file_root + "authors" + ".json", "w") as output:
output.write(json)
print("authors.json is stored at " + file_root)
main_page()
elif collection == "back":
main_page()
else:
print("Unknown collection")
main_page()
# def goodbye(name: str, formal: bool = False):
# """ function2 """
# if formal:
# typer.echo(f"Goodbye Ms. {name}. Have a good day.")
# else:
# typer.echo(f"Bye {name}!")
if __name__ == "__main__":