|
9 | 9 | load_dotenv() |
10 | 10 |
|
11 | 11 | print(datetime.now(), " [LOG]:") |
12 | | -emby_ip = os.getenv('EMBY_IP') |
| 12 | +emby_ip = os.getenv("EMBY_IP") |
13 | 13 |
|
14 | 14 | auth_url = f"{emby_ip}/Users/AuthenticateByName" |
15 | | -payload = json.dumps({ |
16 | | - "Username": os.getenv('EMBY_USER_NAME'), |
17 | | - "pw": os.getenv('EMBY_PASSWORD') |
18 | | -}) |
| 15 | +payload = json.dumps({"Username": os.getenv("EMBY_USER_NAME"), "pw": os.getenv("EMBY_PASSWORD")}) |
19 | 16 | headers = { |
20 | | - 'Authorization': f'''Emby UserId="{os.getenv('EMBY_USER_ID')}",Client="Python", Device="Raspberry Pi 4B", DeviceId="xxx", Version="3.9"''', |
21 | | - 'Content-Type': 'application/json' |
| 17 | + "Authorization": f'''Emby UserId="{os.getenv('EMBY_USER_ID')}",Client="Python", Device="Raspberry Pi 4B", DeviceId="xxx", Version="3.9"''', |
| 18 | + "Content-Type": "application/json", |
22 | 19 | } |
23 | 20 | auth_response = requests.request("POST", auth_url, headers=headers, data=payload) |
24 | 21 |
|
25 | | -token = auth_response.json()['AccessToken'] |
| 22 | +token = auth_response.json()["AccessToken"] |
26 | 23 | query_string = "SELECT * FROM PlaybackActivity" |
27 | 24 | query_url = f"{emby_ip}/emby/user_usage_stats/submit_custom_query?X-Emby-Token={token}&CustomQueryString={query_string}&ReplaceUserId=True" |
28 | 25 |
|
29 | 26 | query_response = requests.request("POST", query_url) |
30 | 27 | resp = query_response.json() |
31 | | -cols = resp['colums'] |
32 | | -qry_result = resp['results'] |
| 28 | +cols = resp["colums"] |
| 29 | +qry_result = resp["results"] |
33 | 30 |
|
34 | 31 | cols = [item.lower() for item in cols] |
35 | 32 | joined_cols = '","'.join(cols) |
36 | 33 |
|
37 | 34 | try: |
38 | | - if len(qry_result) == 0: |
39 | | - raise Exception('No records present') |
40 | | - |
41 | | - conn = psycopg2.connect( |
42 | | - user=os.getenv('PG_USER'), |
43 | | - password=os.getenv('PG_PASSWORD'), |
44 | | - host=os.getenv('PG_HOST'), |
45 | | - port=os.getenv('PG_PORT'), |
46 | | - dbname=os.getenv('EMBY_DB_NAME')) |
| 35 | + if len(qry_result) == 0: |
| 36 | + raise Exception("No records present") |
47 | 37 |
|
48 | | - print('\tConnected to DB EmbyReporting') |
| 38 | + conn = psycopg2.connect( |
| 39 | + user=os.getenv("PG_USER"), |
| 40 | + password=os.getenv("PG_PASSWORD"), |
| 41 | + host=os.getenv("PG_HOST"), |
| 42 | + port=os.getenv("PG_PORT"), |
| 43 | + dbname=os.getenv("EMBY_DB_NAME"), |
| 44 | + ) |
49 | 45 |
|
50 | | - cur = conn.cursor() |
51 | | - truncate_query = 'TRUNCATE TABLE playbackactivity;' |
52 | | - cur.execute(truncate_query) |
53 | | - conn.commit() |
54 | | - print('\tCleared Table PlaybackActivity') |
| 46 | + print("\tConnected to DB EmbyReporting") |
55 | 47 |
|
56 | | - for rec in qry_result: |
57 | | - if len(rec) != 11: |
58 | | - raise Exception('Data missing in record') |
59 | | - |
60 | | - insert_query = f''' |
61 | | - INSERT INTO playbackactivity ("{joined_cols}") |
62 | | - VALUES |
63 | | - ('{rec[0]}', '{rec[1]}', {rec[2]}, '{rec[3]}', '{rec[4].replace("'", "''")}', '{rec[5]}', '{rec[6]}', '{rec[7].replace("'", "''")}', {rec[8]}, {rec[9]}, '{rec[10]}') |
64 | | - ''' |
65 | | - cur.execute(insert_query) |
66 | | - conn.commit() |
67 | | - print(f'\t{len(qry_result)} records inserted into PlaybackActivity.') |
| 48 | + cur = conn.cursor() |
| 49 | + truncate_query = "TRUNCATE TABLE playbackactivity;" |
| 50 | + cur.execute(truncate_query) |
| 51 | + conn.commit() |
| 52 | + print("\tCleared Table PlaybackActivity") |
| 53 | + |
| 54 | + for rec in qry_result: |
| 55 | + if len(rec) != 11: |
| 56 | + raise Exception("Data missing in record") |
| 57 | + |
| 58 | + insert_query = f""" |
| 59 | + INSERT INTO playbackactivity ("{joined_cols}") |
| 60 | + VALUES |
| 61 | + ('{rec[0]}', '{rec[1]}', {rec[2]}, '{rec[3]}', '{rec[4].replace("'", "''")}', '{rec[5]}', |
| 62 | + '{rec[6]}', '{rec[7].replace("'", "''")}', {rec[8]}, {rec[9]}, '{rec[10]}') |
| 63 | + """ |
| 64 | + cur.execute(insert_query) |
| 65 | + conn.commit() |
| 66 | + print(f"\t{len(qry_result)} records inserted into PlaybackActivity.") |
68 | 67 |
|
69 | 68 | except Exception as e: |
70 | | - print("\t",str(e)) |
| 69 | + print("\t", str(e)) |
71 | 70 |
|
72 | 71 | finally: |
73 | | - cur.close() |
74 | | - conn.close() |
75 | | - |
| 72 | + cur.close() |
| 73 | + conn.close() |
0 commit comments