Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
automagically post my mpv screenshots to @slowpokeshots (see https://slowpokewell.wordpress.com/2014/12/11/automagic-screenshot-twitter-account/)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Author: slowpoke <mail+gist@slowpoke.io>
#
# I place this in the Public Domain.
# Do whatever you want. Fuck copywrong.
import os
import time
import plac # pip install plac
import twitter # pip install twitter
MPV_FIFO = "/tmp/mpv.fifo"
TMP_SHOT_FILENAME = "/tmp/slowpokeshot.png"
CONSUMER_KEY = "s1NfxCGgovyTGeg8MiuX8F9tZ"
CONSUMER_SECRET = "MEiB0paKNz0CBs2jUKn4YxKoanLW0CyDhYrR0LtLiJJjdCEzww"
CRED_FILE = os.path.expanduser("~/.slowpokeshots_twittercreds")
def main(subtitles: ("Include subtitles in screenshot", 'flag', 's')):
# take two screenshots, a temporary one for
# uploading and the other for the collection
screenshot(subtitles, TMP_SHOT_FILENAME)
screenshot(subtitles)
# wait for the shot to appear (avoids a race condition, this is an ugly
# hack, but it works)
wait_for_shot()
# upload the shot
upload_shot(TMP_SHOT_FILENAME)
# finally, remove the temporary shot again
os.remove(TMP_SHOT_FILENAME)
def screenshot(subtitles, filename=None):
subs_or_vid = "subtitles" if subtitles else "video"
if filename is None:
command = "screenshot {}\n".format(subs_or_vid)
else:
command = "screenshot_to_file {} {}\n".format(filename, subs_or_vid)
print(command)
with open(MPV_FIFO, "w") as fifo:
fifo.write(command)
def wait_for_shot():
while True:
if os.path.exists(TMP_SHOT_FILENAME):
break
time.sleep(1)
def upload_shot(filename, status: ("Optional tweet text", 'option', 't') = ""):
if not os.path.exists(CRED_FILE):
twitter.oauth_dance(
"slowpokeshots", CONSUMER_KEY, CONSUMER_SECRET, CRED_FILE)
oauth_token, oauth_secret = twitter.read_token_file(CRED_FILE)
t = twitter.Twitter(auth=twitter.OAuth(
oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
with open(filename, "rb") as image:
params = {"media[]": image.read(), "status": status}
t.statuses.update_with_media(**params)
if __name__ == "__main__":
plac.call(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment