commit f03cc75f2bbda546643a74427ac2f0a935d700cc Author: houndvoyager Date: Fri Sep 2 13:46:05 2022 +0200 resetting master branch diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aab923d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +test.py +.env +venv/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7891df5 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# Suchbot, a bot for suchmemes ._. + +# Supported platforms: + - [x] Mastodon (Come check it out at [@wownero](https://mastodon.social/@wownero)) + - [x] Tumblr (only posts image due to the package limitations. Check out the feed here: [wownero](https://wownero.tumblr.com/) + - [ ] XMPP + - [ ] IRC + +## It uses a [Supabase](https://supabase.com/) database to store the json from https://suchwow.xyz/api/list. + +> Posts image data directly to services without storing it. + +# Reproducing: +To reproduce, set up a .env with the following and install the necessary modules found in `requirements.txt`: + +``` +SUPABASE_URL= +SUPABASE_KEY= + +Mastodon: +MAST_TOKEN= + +Tumblr: +consumer_key= +consumer_secret= +oauth_token= +oauth_secret= +``` \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..836b7a4 --- /dev/null +++ b/main.py @@ -0,0 +1,45 @@ +import httpx +import time +import modules +import os +from dotenv import load_dotenv +from supabase import create_client, Client +load_dotenv() +url: str = os.environ.get("SUPABASE_URL") +key: str = os.environ.get("SUPABASE_KEY") +supabase: Client = create_client(url, key) + + +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() + print(f'Inserted meme {item["id"]} to Supabase DB') + 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) + + +while True: + try: + scraper() + except Exception as e: + print(e) + print("Database updated") + time.sleep(120) \ No newline at end of file diff --git a/modules.py b/modules.py new file mode 100644 index 0000000..06e9118 --- /dev/null +++ b/modules.py @@ -0,0 +1,38 @@ +from mastodon import Mastodon +import pytumblr2 +import mimetypes +import httpx +import os +from dotenv import load_dotenv + +load_dotenv() + + +def masto(post): + mastodon = Mastodon( + access_token=os.environ.get("MAST_TOKEN"), + api_base_url='https://mastodon.social' + ) + try: + i = httpx.get(post["image"]).content + mime = mimetypes.guess_type(post["image"], strict=True)[0] + media_ids = mastodon.media_post(i, mime, description=f'{post["title"]}')["id"] + media_ids = [media_ids] + content = f'New meme by {post["submitter"]}!\n{post["title"]} - {post["text"]}\n{post["href"]}' + mastodon.status_post(content, media_ids=media_ids) + print(f'Posted {post["id"]} to Mastodon @wownero') + except Exception as e: + print(e) + + + +def tumblr(post): + client = pytumblr2.TumblrRestClient( + os.environ.get("consumer_key"), + os.environ.get("consumer_secret"), + os.environ.get("oauth_token"), + os.environ.get("oauth_secret"), + ) + + client.legacy_create_photo("wownero", state="published", tags=["wownero", "such", "meme", "wow", "memes"], caption=f'New meme by {post["submitter"]}!\n{post["title"]} - {post["text"]}', link=f'{post["href"]}', + source=post["image"]) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..74718dd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,37 @@ +anyio==3.5.0 +blurhash==1.1.4 +certifi==2021.10.8 +charset-normalizer==2.0.12 +decorator==5.1.1 +deprecation==2.1.0 +dnspython==2.2.1 +future==0.18.2 +gotrue==0.5.0 +h11==0.12.0 +httpcore==0.14.7 +httpx==0.21.3 +idna==3.3 +Mastodon.py==1.5.1 +oauthlib==3.2.0 +packaging==21.3 +Pillow==9.0.1 +postgrest-py==0.9.2 +psycopg2==2.9.3 +pydantic==1.9.0 +pymongo==4.0.2 +pyparsing==3.0.7 +python-dateutil==2.8.2 +python-dotenv==0.19.2 +python-magic==0.4.25 +PyTumblr2==0.2.2 +pytz==2022.1 +realtime==0.0.4 +requests==2.27.1 +requests-oauthlib==1.3.1 +rfc3986==1.5.0 +six==1.16.0 +sniffio==1.2.0 +supabase==0.5.3 +typing-extensions==4.1.1 +urllib3==1.26.9 +websockets==9.1