Last active
August 29, 2015 14:17
-
-
Save alekratz/c9824e7e5e5643608a2c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python2 | |
# Specifically, using python 2 | |
# Make a pics/ directory and put your pictures in there | |
# Make a text/ directory and put your text files in there to train the markov model | |
# requires markovify and pytumblr | |
import markovify | |
import pytumblr | |
import time, os, os.path, random | |
client = pytumblr.TumblrRestClient( | |
'', # consumer key | |
'', # consumer secret | |
'', # oauth token | |
'' # oauth secret | |
) | |
MINUTE = 60 | |
HOUR = MINUTE * 60 | |
blog_name = "your-blog.tumblr.com" | |
post_tags = [] | |
post_every = 2 * HOUR | |
# Number of glitch passes to make | |
# These numbers seem to work well. Glitch too much, and tumblr doesn't like the image. Glitch too little and nothing really changes. | |
glitch_min = 10 | |
glitch_max = 15 | |
def glitch(old_path, new_path): | |
with open(old_path, 'rb') as f: data = f.read() | |
data = list(data) | |
# replace a few random spots | |
pass_count = random.randint(glitch_min, glitch_max) | |
for i in range(pass_count): | |
data[random.randint(128, len(data)-1)] = chr(random.randint(0, 255)) | |
newdata = ''.join(data) | |
with open(new_path, 'wb') as f: f.write(newdata) | |
# get available photos | |
try: | |
os.mkdir("used") | |
except(Exception): | |
pass | |
try: | |
os.mkdir("gen") | |
except(Exception): | |
pass | |
available = [fname for fname in os.listdir("pics")] | |
used = [fname for fname in os.listdir("used")] | |
print "Starting bot using Tumblr account", blog_name | |
print "posting every", post_every / HOUR, "hours" | |
print len(available), "unglitched photos available" | |
print len(used), "used photos already glitched" | |
#print client.blog_info(blog_name) | |
print "loading markov chain text" | |
chain_text = '' | |
for fname in os.listdir("text"): | |
with open("text/" + fname) as fp: chain_text += fp.read() | |
text_model = markovify.Text(chain_text) | |
last_post = client.blog_info(blog_name)["blog"]["updated"] | |
while 1: | |
next_post = last_post + post_every | |
current_time = int(time.time()) | |
print "current time:",time.ctime(current_time), "(", (next_post - current_time)/60, "minutes till next post", ")" | |
if next_post <= current_time: | |
# get a random available image | |
image = random.choice(available) | |
pics_path = os.path.join("pics", image) | |
used_path = os.path.join("used", image) | |
gen_path = os.path.join("gen", image) | |
# glitch it! | |
glitch(pics_path, gen_path) | |
# create a markov chain string | |
gen_str = text_model.make_sentence() | |
# upload new glitched image | |
print "uploading", gen_path | |
result = client.create_photo(blog_name, tags=post_tags, state="published", data=gen_path, caption=gen_str) | |
if 'meta' in result and result['meta']['status'] == 400: | |
print "Error with photo, trying again" | |
else: | |
# move it to the used directory | |
available.remove(image) | |
used += [image] | |
os.rename(pics_path, used_path) | |
last_post += post_every | |
else: | |
sleep_time = 300 | |
if next_post - current_time < 300: | |
print (next_post-current_time),"seconds until next post" | |
time.sleep(next_post-current_time) | |
else: | |
time.sleep(300) # sleep till it's time | |
#client.create_text(blog_name, state="published", title="Test", body="Testing 1 2 3 4", tags=['test', 'test tags']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment