-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (75 loc) · 3.22 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
import json
import psycopg2, sql_queries, os, hashlib, requests
from datetime import datetime
NETFLIX_BASE_URL = 'https://gateway.marvel.com:443'
PRIVATE_KEY = os.getenv('PRIVATE_KEY')
PUBLIC_KEY = os.getenv('PUBLIC_KEY')
TS = '1234'
HASH = hashlib.md5(str(TS+PRIVATE_KEY+PUBLIC_KEY).encode('utf').strip()).hexdigest()
def get_connection():
conn = psycopg2.connect(user='admin', host='127.0.0.1', port='5432', database='marvel_db')
return conn
conn = get_connection()
def _create_tables(conn):
try:
cursor = conn.cursor()
cursor.execute(sql_queries.CREATE_TABLE_CHARACTERS)
cursor.execute(sql_queries.CREATE_TABLE_COMICS)
conn.commit()
print("Table create successfully")
except Exception as e:
print(f"Error: {e}")
def _get_comics(character_id):
params = dict(ts=TS, apikey=PUBLIC_KEY, hash=HASH)
response = requests.get(url=f"{NETFLIX_BASE_URL}/v1/public/characters/{character_id}/comics", params=params)
if response.status_code == 200:
response_body = json.loads(response.text)
if response_body.get("data", {}).get("results"):
for comics in response_body.get("data", {}).get("results"):
def insert_comics_into_db(id,
digital_id,
character_id,
title,
issue_number,
variantDescription,
description,
modified,
isbn,
upc,
diamondCode,
ean,
issn,
format,
pageCount):
try:
cursor = conn.cursor()
cursor.execute(sql_queries.INSERT_INTO_COMICS,
(id,digital_id, character_id, title, issue_number, variantDescription, description, modified,
isbn, upc, diamondCode, ean, issn, format, pageCount))
except Exception as e:
print(f"Can not insert data into table: {e}")
def _get_characters():
params = dict(ts=TS, apikey=PUBLIC_KEY, hash=HASH)
response = requests.get(url=f"{NETFLIX_BASE_URL}/v1/public/characters", params=params)
if response.status_code == 200:
response_body = json.loads(response.text)
if response_body.get("data", {}).get("results"):
for character in response_body.get("data", {}).get("results"):
insert_character_into_db(
id=character.get('id', 0),
name=character.get('name', ''),
description=character.get('description', ''),
modified=datetime.strptime(character.get('modified'), '%Y-%m-%dT%H:%M:%S%z'))
_get_comics()
def insert_character_into_db(id, name, description, modified):
try:
cursor = conn.cursor()
cursor.execute(sql_queries.INSERT_INTO_CHARACTERS, (id, name, description, modified))
conn.commit()
cursor.close()
print(f"The record was added to DB")
except Exception as e:
print(f"Can not insert data into table: {e}")
if __name__ == '__main__':
_create_tables(conn=conn)
_get_characters()