67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
import httpx
|
|
import asyncio
|
|
import sqlite3
|
|
|
|
url = 'http://94.177.199.207:3000/client'
|
|
session = 'AssistenzaPubblicaBardiProd'
|
|
contactDB = 'contacts.db'
|
|
createContactsTable = [
|
|
'DROP TABLE IF EXISTS contacts;',
|
|
'''
|
|
CREATE TABLE contacts (
|
|
id INTEGER PRIMARY KEY,
|
|
name text NOT NULL,
|
|
number text NOT NULL,
|
|
pushname text NOT NULL,
|
|
shortName text NOT NULL,
|
|
serialized text NOT NULL
|
|
);
|
|
'''
|
|
]
|
|
|
|
def createContactsDB():
|
|
try:
|
|
with sqlite3.connect(contactDB) as conn:
|
|
cursor = conn.cursor()
|
|
for sql in createContactsTable:
|
|
cursor.execute(sql)
|
|
print("Tables created successfully.")
|
|
except sqlite3.OperationalError as e:
|
|
print("Failed to create tables:", e)
|
|
|
|
def getContactList():
|
|
api = f'{url}/getContacts/{session}'
|
|
with httpx.Client() as client:
|
|
response = client.get(api)
|
|
return response.json()
|
|
|
|
def init_db():
|
|
createContactsDB()
|
|
contactList = getContactList()
|
|
try:
|
|
with sqlite3.connect(contactDB) as conn:
|
|
for id, contact in enumerate([item for item in contactList["contacts"] if str(item['number']).startswith('39')]):
|
|
conn.execute(f'''INSERT INTO contacts (id, name, number, pushname, shortName, serialized) VALUES ({id},
|
|
"{contact.get("name", "N/A")}", {contact.get("number")}, "{contact.get("pushname", "N/A")}",
|
|
"{contact.get("shortName", "N/A")}", "{contact["id"]["_serialized"]}")''')
|
|
conn.commit()
|
|
print("Tables loaded successfully.")
|
|
except sqlite3.OperationalError as e:
|
|
print("Failed to load table:", e)
|
|
|
|
async def fetch(client, url):
|
|
response = await client.get(url)
|
|
return response.json()
|
|
|
|
async def main():
|
|
init_db()
|
|
'''
|
|
async with httpx.AsyncClient() as client:
|
|
tasks = fetch(client, url)
|
|
results = await asyncio.gather(*tasks)
|
|
for result in results:
|
|
print(result)
|
|
'''
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main()) |