Last active
March 31, 2025 00:54
-
-
Save jeffehobbs/f8edf87398d1eb6bac571a101c397fef to your computer and use it in GitHub Desktop.
AI Images That Never Trend https://bsky.app/profile/nevertrendbot.bsky.social
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# nevertrendbot.py | [email protected] | |
# | |
# Why don't pictures like this ever trend? | |
# | |
# [X] randomly choose premise for image | |
# [X] generate image | |
# [X] post image | |
import os, configparser, random, csv, tweepy | |
import pandas as pd | |
import requests | |
from openai import OpenAI | |
from PIL import Image | |
from atproto import Client, client_utils | |
from mastodon import Mastodon | |
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) | |
config = configparser.ConfigParser() | |
config.read(SCRIPT_PATH +'/secrets.txt') | |
OPENAI_APIKEY = config.get('openai', 'apikey') | |
BLUESKY_USERNAME = config.get('bluesky','username') | |
BLUESKY_PASSWORD = config.get('bluesky','password') | |
MASTODON_ACCESS_TOKEN = config.get('mastodon','access_token') | |
TWITTER_CONSUMER_KEY = config.get('twitter', 'consumer_key') | |
TWITTER_CONSUMER_SECRET = config.get('twitter', 'consumer_key_secret') | |
TWITTER_ACCESS_TOKEN = config.get('twitter', 'access_token') | |
TWITTER_ACCESS_TOKEN_SECRET = config.get('twitter', 'access_token_secret') | |
GOOGLE_SHEET = config.get('google', 'sheet') | |
def get_data_csv(): | |
print('getting data...') | |
path = GOOGLE_SHEET | |
response = requests.get(path) | |
if response.status_code == 200: | |
with open('/tmp/nevertrenddata.csv', 'wb') as f: | |
f.write(response.content) | |
else: | |
print('data file not found') | |
df = pd.read_csv('/tmp/nevertrenddata.csv') | |
subjects = df['SUBJECTS'] | |
subject = random.choice(subjects) | |
subject_two = random.choice(subjects) | |
if (subject == subject_two): | |
get_data_csv() | |
settings = df['SETTINGS'] | |
setting = random.choice(settings) | |
activities = df['ACTIVITIES'] | |
activity = random.choice(activities) | |
return(subject, subject_two, setting, activity) | |
def generate_image(prompt): | |
print('generating image...') | |
client = OpenAI(api_key=OPENAI_APIKEY) | |
response = client.images.generate( | |
model="dall-e-3", | |
prompt=prompt, | |
size="1024x1024") | |
return(response.data[0].url) | |
def compress_image(image_url): | |
print('compressing image...') | |
response = requests.get(image_url) | |
if response.status_code == 200: | |
with open('/tmp/nevertrend.jpg', 'wb') as f: | |
f.write(response.content) | |
image = Image.open('/tmp/nevertrend.jpg') | |
image.save('/tmp/nevertrend.jpg', optimize=True, quality=75) | |
def post_bluesky(status, alttext): | |
print('sending post to bluesky...') | |
client = Client() | |
client.login(BLUESKY_USERNAME, BLUESKY_PASSWORD) | |
with open('/tmp/nevertrend.jpg', 'rb') as f: | |
img_data = f.read() | |
post = client.send_image(text=status, image=img_data, image_alt=alttext) | |
post_id = post.uri.split('/') | |
print(f'https://bsky.app/profile/nevertrendbot.bsky.social/post/{post_id[-1]}') | |
return | |
def post_mastodon(status, alttext): | |
print('sending post to mastodon...') | |
mastodon = Mastodon( | |
access_token = MASTODON_ACCESS_TOKEN, | |
api_base_url = 'https://mastodon.social/' | |
) | |
media = mastodon.media_post('/tmp/nevertrend.jpg', description=alttext) | |
post = mastodon.status_post(status, media_ids=media) | |
print(post.uri) | |
return | |
# tweet that stuff | |
def post_tweet(status, alttext): | |
print('sending post to twitter...') | |
media_ids = [] | |
client = tweepy.Client(consumer_key=TWITTER_CONSUMER_KEY, | |
consumer_secret=TWITTER_CONSUMER_SECRET, | |
access_token=TWITTER_ACCESS_TOKEN, | |
access_token_secret=TWITTER_ACCESS_TOKEN_SECRET) | |
auth = tweepy.OAuth1UserHandler( | |
TWITTER_CONSUMER_KEY, | |
TWITTER_CONSUMER_SECRET, | |
TWITTER_ACCESS_TOKEN, | |
TWITTER_ACCESS_TOKEN_SECRET, | |
) | |
api = tweepy.API(auth) | |
media_upload_response = api.media_upload('/tmp/nevertrend.jpg') | |
media_ids.append(media_upload_response.media_id) | |
response = client.create_tweet(text=status, user_auth=True, media_ids=media_ids) | |
return | |
def main(): | |
subject, subject_two, setting, activity = get_data_csv() | |
prompt = f'single creature that is a combination of a {subject} and a {subject_two} {setting} {activity}' | |
#print(prompt) | |
image_url = generate_image(prompt) | |
compress_image(image_url) | |
status = "Why don't pictures like this ever trend" | |
post_bluesky(status, prompt) | |
post_mastodon(status, prompt) | |
post_tweet(status, prompt) | |
print('...done.') | |
if __name__ == '__main__': | |
main() | |
#fin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment