suchbot/main.py

56 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-09-02 12:46:05 +01:00
import httpx
import time
import modules
2022-11-14 07:39:00 +00:00
from os import system, name, environ
2022-09-02 12:46:05 +01:00
from dotenv import load_dotenv
from supabase import create_client, Client
load_dotenv()
2022-11-14 07:39:00 +00:00
url: str = environ.get("SUPABASE_URL")
key: str = environ.get("SUPABASE_KEY")
2022-09-02 12:46:05 +01:00
supabase: Client = create_client(url, key)
2022-11-14 07:39:00 +00:00
def clear():
if name == 'nt':
_ = system('cls')
else:
_ = system('clear')
2022-09-02 12:46:05 +01:00
def scraper():
transport = httpx.HTTPTransport(retries=5)
client = httpx.Client(transport=transport)
api_list = client.get("https://suchwow.xyz/api/list", timeout=100).json()
api_list.reverse()
for item in api_list:
try:
supabase.table("suchmeme").insert(item).execute()
2022-11-08 19:02:58 +00:00
print(f'Inserted meme {item["id"]} to Supabase DB, posting it to social medias...')
2022-09-02 12:46:05 +01:00
post(item)
except Exception as e:
if 'duplicate key value violates unique constraint' in str(e):
print("Meme already exists")
continue
else:
print(f'Error upon inserting data in DB with meme {item["id"]} due to {e}')
continue
def post(item):
modules.masto(item)
modules.tumblr(item)
2022-11-08 19:02:58 +00:00
modules.vk(item)
2022-09-02 12:46:05 +01:00
while True:
try:
scraper()
2022-11-14 07:39:00 +00:00
clear()
2022-09-02 12:46:05 +01:00
except Exception as e:
print(e)
print("Database updated")
time.sleep(120)