Skip to content

Instantly share code, notes, and snippets.

@proxypoke
Created December 11, 2014 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proxypoke/57565ebce1e60001094a to your computer and use it in GitHub Desktop.
Save proxypoke/57565ebce1e60001094a to your computer and use it in GitHub Desktop.
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