From 0680da2e17b14cc6a4443808b09bfa3eb54a7fb4 Mon Sep 17 00:00:00 2001 From: Nahuel Sotelo Date: Sun, 26 Apr 2020 15:48:59 -0300 Subject: [PATCH 1/4] Ability to request auth link separately from the initial config. This way you to can add more twitter tokens without regenerating the config file every time --- diffengine/__init__.py | 51 +++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/diffengine/__init__.py b/diffengine/__init__.py index 69b4cae..5f9e3ce 100755 --- a/diffengine/__init__.py +++ b/diffengine/__init__.py @@ -25,6 +25,7 @@ import readability import unicodedata import yaml +import argparse from peewee import * from playhouse.migrate import SqliteMigrator, migrate @@ -385,20 +386,14 @@ def get_initial_config(): else: config["feeds"].append({"url": url, "name": feed.feed.title}) - answer = input("Would you like to set up tweeting edits? [Y/n] ") + answer = input("Would you like to set up tweeting edits? [Y/n] ") or "Y" if answer.lower() == "y": print("Go to https://apps.twitter.com and create an application.") consumer_key = input("What is the consumer key? ") consumer_secret = input("What is the consumer secret? ") - auth = tweepy.OAuthHandler(consumer_key, consumer_secret) - auth.secure = True - auth_url = auth.get_authorization_url() - input( - "Log in to https://twitter.com as the user you want to tweet as and hit enter." - ) - input("Visit %s in your browser and hit enter." % auth_url) - pin = input("What is your PIN: ") - token = auth.get_access_token(verifier=pin) + + token = request_pin_to_user_and_get_token(consumer_key, consumer_secret) + config["twitter"] = { "consumer_key": consumer_key, "consumer_secret": consumer_secret, @@ -414,6 +409,18 @@ def get_initial_config(): return config +def request_pin_to_user_and_get_token(consumer_key, consumer_secret): + auth = tweepy.OAuthHandler(consumer_key, consumer_secret) + auth.secure = True + auth_url = auth.get_authorization_url() + input( + "Log in to https://twitter.com as the user you want to tweet as and hit enter." + ) + input("Visit %s in your browser and hit enter." % auth_url) + pin = input("What is your PIN: ") + return auth.get_access_token(verifier=pin) + + def home_path(rel_path): return os.path.join(home, rel_path) @@ -587,5 +594,27 @@ def _get(url, allow_redirects=True): ) +def get_auth_link_and_show_token(): + global home + home = os.getcwd() + config = load_config(True) + twitter = config["twitter"] + token = request_pin_to_user_and_get_token( + twitter["consumer_key"], twitter["consumer_secret"] + ) + print("These are your access token and secret.\nDO NOT SHARE THEM WITH ANYONE!\n") + print("ACCESS_TOKEN\n%s\n" % token[0]) + print("ACCESS_TOKEN_SECRET\n%s\n" % token[1]) + + +# Cli options +parser = argparse.ArgumentParser() +parser.add_argument("--add", action="store_true") + if __name__ == "__main__": - main() + options = parser.parse_args() + if options.add: + get_auth_link_and_show_token() + else: + main() + sys.exit("Finishing diffengine") From 01b0b642c9fa55c3d3c05574fde6f793a03002fa Mon Sep 17 00:00:00 2001 From: Nahuel Sotelo Date: Sun, 26 Apr 2020 15:51:43 -0300 Subject: [PATCH 2/4] Function rearranged --- diffengine/__init__.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/diffengine/__init__.py b/diffengine/__init__.py index 5f9e3ce..b42bc88 100755 --- a/diffengine/__init__.py +++ b/diffengine/__init__.py @@ -375,6 +375,19 @@ def load_config(prompt=True): return config +def get_auth_link_and_show_token(): + global home + home = os.getcwd() + config = load_config(True) + twitter = config["twitter"] + token = request_pin_to_user_and_get_token( + twitter["consumer_key"], twitter["consumer_secret"] + ) + print("These are your access token and secret.\nDO NOT SHARE THEM WITH ANYONE!\n") + print("ACCESS_TOKEN\n%s\n" % token[0]) + print("ACCESS_TOKEN_SECRET\n%s\n" % token[1]) + + def get_initial_config(): config = {"feeds": []} @@ -594,25 +607,12 @@ def _get(url, allow_redirects=True): ) -def get_auth_link_and_show_token(): - global home - home = os.getcwd() - config = load_config(True) - twitter = config["twitter"] - token = request_pin_to_user_and_get_token( - twitter["consumer_key"], twitter["consumer_secret"] - ) - print("These are your access token and secret.\nDO NOT SHARE THEM WITH ANYONE!\n") - print("ACCESS_TOKEN\n%s\n" % token[0]) - print("ACCESS_TOKEN_SECRET\n%s\n" % token[1]) - - # Cli options parser = argparse.ArgumentParser() parser.add_argument("--add", action="store_true") +options = parser.parse_args() if __name__ == "__main__": - options = parser.parse_args() if options.add: get_auth_link_and_show_token() else: From 69bb8d738b2ea2478d6c33328674bbc172f8d926 Mon Sep 17 00:00:00 2001 From: Nahuel Sotelo Date: Sun, 26 Apr 2020 15:53:49 -0300 Subject: [PATCH 3/4] Cli options inside main execution for avoid tests break --- diffengine/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/diffengine/__init__.py b/diffengine/__init__.py index b42bc88..8fd7de0 100755 --- a/diffengine/__init__.py +++ b/diffengine/__init__.py @@ -607,12 +607,12 @@ def _get(url, allow_redirects=True): ) -# Cli options -parser = argparse.ArgumentParser() -parser.add_argument("--add", action="store_true") -options = parser.parse_args() - if __name__ == "__main__": + # Cli options + parser = argparse.ArgumentParser() + parser.add_argument("--add", action="store_true") + options = parser.parse_args() + if options.add: get_auth_link_and_show_token() else: From 80a4b8047c2bf309ab0aeb88b2494025c418f615 Mon Sep 17 00:00:00 2001 From: Nahuel Sotelo Date: Sun, 26 Apr 2020 15:58:36 -0300 Subject: [PATCH 4/4] New line before the advise --- diffengine/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diffengine/__init__.py b/diffengine/__init__.py index 8fd7de0..8a1a712 100755 --- a/diffengine/__init__.py +++ b/diffengine/__init__.py @@ -383,7 +383,7 @@ def get_auth_link_and_show_token(): token = request_pin_to_user_and_get_token( twitter["consumer_key"], twitter["consumer_secret"] ) - print("These are your access token and secret.\nDO NOT SHARE THEM WITH ANYONE!\n") + print("\nThese are your access token and secret.\nDO NOT SHARE THEM WITH ANYONE!\n") print("ACCESS_TOKEN\n%s\n" % token[0]) print("ACCESS_TOKEN_SECRET\n%s\n" % token[1])