From 778002f45565227013d6be294f2ea546eda51365 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Thu, 14 Dec 2017 17:22:16 -0800 Subject: [PATCH] Split out reviewing skins from tweeting them --- experiments/tweetBot/tweet.py | 117 +++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 31 deletions(-) diff --git a/experiments/tweetBot/tweet.py b/experiments/tweetBot/tweet.py index 45477b90..8384666a 100755 --- a/experiments/tweetBot/tweet.py +++ b/experiments/tweetBot/tweet.py @@ -2,7 +2,9 @@ """Tweet Winamp Skins Usage: - tweet.py [--dry] + tweet.py post [--dry] + tweet.py review [--dry] + tweet.py debug Options: -h --help Show this screen. @@ -20,6 +22,7 @@ import boto3 import twitter from docopt import docopt from sets import Set +from collections import defaultdict from config import CONFIG @@ -70,13 +73,18 @@ def md5_file(path): def find_skin_with_screenshot(): + skins = get_state() skin_path = random_skin() - md5 = md5_file(skin_path) + if(md5 in skins): + print "Already handled %s. Trying again..." % md5 + return find_skin_with_screenshot() screenshot_path = screenshot_by_md5(md5) if(not screenshot_path): print "Could not find the screenshot, trying again..." return find_skin_with_screenshot() + dispatch({"type": "FOUND_SCREENSHOT", "skin_path": skin_path, + "screenshot_path": screenshot_path, "md5": md5}) return skin_path, screenshot_path, md5 @@ -88,41 +96,80 @@ def dispatch(action): json.dump(action_log, f) -def past_skins(): +def get_state(): + def get_default_skin(): + return dict() + + skins = defaultdict(get_default_skin) + with open("./action_log.json", 'r') as f: action_log = json.load(f) - past = Set() for action in action_log: - if(action['type'] == "REJECTED_SKIN" or action['type'] == "TWEETED"): - past.add(str(action["md5"])) - return past + if(action['type'] == "FOUND_SCREENSHOT"): + skin = skins[action['md5']] + skin['md5'] = action['md5'] + skin['has_screenshot'] = True + skin['screenshot_path'] = action['screenshot_path'] + skin['skin_path'] = action['skin_path'] + if(action['type'] == "TWEETED"): + skin = skins[action['md5']] + skin['tweeted'] = True + if(action['type'] == "REJECTED_SKIN"): + skin = skins[action['md5']] + skin['rejected'] = True + if(action['type'] == "APPROVED_SKIN"): + skin = skins[action['md5']] + skin['approved'] = True + if(action['type'] == "UPLOADED_SKIN"): + skin = skins[action['md5']] + skin['skin_url'] = action['skin_url'] + + return skins + + +def review(): + while(True): + skin_path, screenshot_path, md5 = find_skin_with_screenshot() + skin_name = os.path.basename(skin_path) + print "Found %s" % skin_name + os.system("open \"%s\"" % screenshot_path) + res = raw_input("Approve? (y/n/q)") + if(res is "q"): + return + elif(res is "y"): + dispatch({"type": "APPROVED_SKIN", "md5": md5, "skin_path": skin_path}) + print "Approved %s" % skin_name + elif(res is "n"): + dispatch({"type": "REJECTED_SKIN", "md5": md5}) + print "Rejected %s" % skin_name + else: + print "Invalid input" def main(dry): - already_handled = past_skins() - skin_path, screenshot_path, md5 = find_skin_with_screenshot() - dispatch({"type": "FOUND_SCREENSHOT", "skin_path": skin_path, - "screenshot_path": screenshot_path, "md5": md5}) - if(md5 in already_handled): - print "Already handled %s. Trying again..." % md5 - return main(dry) + + state = get_state() + skin = next(skin for skin in state.itervalues() if skin.get( + 'approved') and not skin.get('tweeted')) + + skin_path = skin.get('skin_path') skin_name = os.path.basename(skin_path) - print "Found %s" % skin_name - os.system("open \"%s\"" % screenshot_path) - res = raw_input("continue?") - if(res is not "y"): - dispatch({"type": "REJECTED_SKIN", "md5": md5}) - return main(dry) - print "Screenshot: %s" % screenshot_path + skin_url = skin.get('skin_url') + md5 = skin.get('md5') + screenshot_path = skin.get('screenshot_path') - print "Uploading to S3..." - s3 = boto3.resource('s3') - s3.meta.client.upload_file( - skin_path, 'winamp2-js-skins', skin_name, {'ACL': 'public-read'}) + assert skin_name + assert md5 + assert screenshot_path - skin_url = "https://s3-us-west-2.amazonaws.com/winamp2-js-skins/%s" % skin_name - dispatch({"type": "UPLOADED_SKIN", "md5": md5, "skin_url": skin_url}) - print "Done: %s" % skin_url + if(not skin_url): + print "Uploading to S3..." + s3 = boto3.resource('s3') + s3.meta.client.upload_file(skin_path, 'winamp2-js-skins', skin_name, {'ACL': 'public-read'}) + + skin_url = "https://s3-us-west-2.amazonaws.com/winamp2-js-skins/%s" % skin_name + dispatch({"type": "UPLOADED_SKIN", "md5": md5, "skin_url": skin_url}) + print "Done: %s" % skin_url print "Going to check that URL..." if not url_is_good(skin_url): @@ -140,11 +187,19 @@ def main(dry): status_message = """%s Try: %s Download: %s""" % (skin_name, winamp2_js_url, skin_url) - tweet(status_message, screenshot_path) - dispatch({"type": "TWEETED", "md5": md5, "message": status_message}) + if not dry: + tweet(status_message, screenshot_path) + dispatch({"type": "TWEETED", "md5": md5, "message": status_message}) + else: + print "Would have tweeted: %" % status_message + print "With media file: %" % screenshot_path print "Done!" if __name__ == "__main__": arguments = docopt(__doc__, version='Tweet Winamp Skins 0.1') - main(dry=arguments.get("--dry")) + dry = arguments.get("--dry") + if(arguments.get("review")): + review() + elif(arguments.get("post")): + main(dry=dry)