Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

prompt = (
f"recommend the top {cate} "
f"in {cy}: (combined)."
f"in {cy}: {combined}."
"based on these, recommend the better one and why"
"two to three sentences"
)
Expand Down
46 changes: 7 additions & 39 deletions json_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class Parser:

def __init__(self, json):
load_dotenv()
if 'data' in json:
Expand All @@ -31,47 +31,15 @@ def write_to_database(self, tb_name):
GROUP BY Name
);"""
connection.execute(db.text(remove_dupes))
# self.get_ratings()

# def get_ratings(self):
# if not isinstance(self.json, type([])):
# return
# rows = []
# for location in self.json:
# location_id = int(location['location_id'])
# url = (f"https://api.content.tripadvisor.com/api/v1/location/"
# f"{location_id}/details")
# headers = {"accept": "application/json"}
# data = {
# 'key': self.key
# }

# r = requests.get(url, headers=headers, params=data)
# if r.status_code == 200:
# flattened = pd.json_normalize(r.json())
# rows.append(flattened)
# df = pd.concat(rows, ignore_index=True)
# for col in df.columns:
# if df[col].apply(lambda x: isinstance(x, (dict, list))).any():
# df[col] = df[col].apply(json.dumps)
# df = df.infer_objects()
# print(df)
# print(df.dtypes)
# # df.to_sql("temp", con=self.engine, if_exists='append', index=False)
def pull_list(self, table_name, city):
query = (f"SELECT * FROM {table_name} "
f"WHERE \"address_obj.city\" = '{city}' LIMIT 10;")
with self.engine.connect() as connection:
result = connection.execute(db.text(query)).fetchall()
print(pd.DataFrame(result))

# join_command = """CREATE TABLE recommendations AS
# SELECT * FROM locations
# JOIN temp ON locations.location_id = temp.location_id;"""
# with self.engine.connect() as connection:
# connection.execute(db.text(join_command))
# self.drop("temp")

def drop(self, table_name):
command = f"DROP TABLE IF EXISTS {table_name}"
with self.engine.connect() as connection:
connection.execute(db.text(command))

with open('sampleJSON.txt', 'r') as file:
location = file.read()
test = Parser(json.loads(location))
test.write_to_database("locations")
Loading