From e6ea77190242e8988c2ee84337e5193494650afe Mon Sep 17 00:00:00 2001 From: Jake Runyan Date: Mon, 10 Feb 2025 12:10:29 -0800 Subject: [PATCH] Initial commit, use of api done. --- .gitignore | 3 +++ README.md | 24 ++++++++++++++++++++ main.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c16328c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +env/ +output/ +config.json \ No newline at end of file diff --git a/README.md b/README.md index 994ac3e..cfec2cd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ # memechain So the idea here is to make an llm agent with tools that use https://imgflip.com/api to turn natural language into a meme. + +## Setup + +```bash +python -m venv env +source env/bin/activate +``` + +```bash +pip install -r requirements.txt +``` + +Update config.json with your imgflip username and password. +Example: +```json +{ + "username": "your_username", + "password": "your_password" +} +``` + +```bash +python main.py +``` diff --git a/main.py b/main.py new file mode 100644 index 0000000..f5a0c59 --- /dev/null +++ b/main.py @@ -0,0 +1,59 @@ +import requests +import os +import json + +def load_config(): + with open('config.json') as config_file: + return json.load(config_file) + +def get_memes(): + url = "https://api.imgflip.com/get_memes" + response = requests.get(url) + result = response.json() + + if result['success']: + memes = result['data']['memes'] + for meme in memes: + print(f"ID: {meme['id']}, Name: {meme['name']}") + else: + print("Failed to retrieve memes.") + +def create_meme(template_id, text0, text1): + config = load_config() + username = config['username'] + password = config['password'] + + url = "https://api.imgflip.com/caption_image" + payload = { + "template_id": template_id, + "username": username, + "password": password, + "text0": text0, + "text1": text1 + } + + response = requests.post(url, data=payload) + result = response.json() + + if result['success']: + meme_url = result['data']['url'] + print(f"Meme created! URL: {meme_url}") + download_image(meme_url) + else: + print(f"Error: {result['error_message']}") + +def download_image(url): + response = requests.get(url) + if response.status_code == 200: + output_dir = 'output' + os.makedirs(output_dir, exist_ok=True) + image_path = os.path.join(output_dir, url.split("/")[-1]) + with open(image_path, 'wb') as f: + f.write(response.content) + print(f"Image saved to {image_path}") + else: + print("Failed to download image.") + + +create_meme(20007896, "Top text", "Bottom text") +# get_memes() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f229360 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests