Skip to content

Instantly share code, notes, and snippets.

@bostelk
Last active January 2, 2020 23:13
Show Gist options
  • Save bostelk/95b39a09e50c88eecf2926064e765e66 to your computer and use it in GitHub Desktop.
Save bostelk/95b39a09e50c88eecf2926064e765e66 to your computer and use it in GitHub Desktop.
Selects a random image to use as a twitter icon
import twitter
import glob
import fnmatch
import random
import os
TWITTER_ICON_GLOB = '*.png'
BLACKLIST_FILENAME = 'blacklist.txt'
CONSUMER_KEY=''
CONSUMER_SECRET=''
ACCESS_TOKEN_KEY=''
ACCESS_TOKEN_SECRET=''
def select_icon_image():
icon_images = glob.glob(TWITTER_ICON_GLOB)
blacklist = []
if os.path.isfile(BLACKLIST_FILENAME):
with open(BLACKLIST_FILENAME, 'r') as f:
blacklist = list(f)
blacklist = list(map(lambda filename: filename.rstrip('\n'), blacklist))
match_blacklist = lambda filename: not any(filter(lambda pattern: fnmatch.fnmatch(filename, pattern), blacklist))
icon_images = list(filter(match_blacklist, icon_images))
return random.choice(icon_images)
def append_to_blacklist(icon_image):
with open(BLACKLIST_FILENAME, 'a') as f:
f.write(icon_image + '\n')
def upload_twitter_icon(icon_image):
api = twitter.Api(consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN_KEY,
access_token_secret=ACCESS_TOKEN_SECRET)
if api.VerifyCredentials():
api.UpdateImage(icon_image)
return True
return False
icon_image = select_icon_image()
print("selected icon: %s" % icon_image)
if upload_twitter_icon(icon_image):
append_to_blacklist(icon_image)
print("uploaded icon: %s" % icon_image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment