From d292c17943bdd4556561e38e77eff84840b4c34d Mon Sep 17 00:00:00 2001 From: Ben Price Date: Tue, 17 Sep 2019 21:40:21 -0700 Subject: [PATCH 1/2] Use a unique user-agent when creating `praw.Reddit` instance. Requires reddit username to be included in `credentials.json` --- Pipfile | 1 + README.md | 1 + fresh.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Pipfile b/Pipfile index 6fd35de..172fa66 100644 --- a/Pipfile +++ b/Pipfile @@ -9,6 +9,7 @@ spotipy = "==2.4.4" configparser = "==3.5.0" cutie = "==0.2.2" crontab = "*" +gitpython = "*" [dev-packages] pylint = "*" diff --git a/README.md b/README.md index 676ce68..bb50ea3 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ To set up your credentials, create a new file called `credentials.json` in the r "redirect": "[redirect uri]" }, "reddit": { + "username": "[reddit username]", "client_id": "[praw client id]", "client_secret": "[praw client secret]" } diff --git a/fresh.py b/fresh.py index 024f6ed..ffef178 100644 --- a/fresh.py +++ b/fresh.py @@ -12,6 +12,10 @@ from constants import ft_set from models import User import cutie +import git + +class InvalidConfigFile(Exception): + """The configuration file is missing a certain section or key""" def createUserConfig(user, config_path='.config.ini'): """ @@ -62,6 +66,48 @@ def createPrawConfig(client_id, client_secret, with open(praw_path, 'w') as p: r_config.write(p) +def createUserAgentString(config_file='credentials.json'): + """ + Create a user-agent string which follows reddit API rules. + + Parameters + ---------- + config_file: str + File from which to read user's reddit username. + + Raises + ------ + InvalidConfigFile: ConfigurationError + if config_file is missing a certain section or key + """ + platform = "Python3" + app = "FreshScript" + github_url = "https://github.com/amcquade/fresh_script" + + # Check containing folder for git repository. This is used as an + # ad hoc versioning system. + try: + repo = git.Repo() + except git.exc.InvalidGitRepositoryError: + repo = None + hexsha = repo.head.object.hexsha[:7] if repo else "UNKNOWN" + # TODO: adopt semantic versioning (https://semver.org) + + # Fetch reddit username from file + with open(config_file, 'r') as f: + config = json.load(f) + try: + reddit_username = config['reddit']['username'] + except KeyError as e: + raise InvalidConfigFile(f"{config_file!r}") from e + + user_agent = ' '.join([ + f"{platform}:{app}:(commit {hexsha})", + f"(by /u/{reddit_username})", + f"({github_url})" + ]) + return user_agent + def createUser(): user = None # read config file @@ -374,7 +420,8 @@ def main(): args = argparser.parse_args() # connect to reddit bot - reddit = praw.Reddit('bot1') + user_agent = createUserAgentString() + reddit = praw.Reddit('bot1', user_agent=user_agent) subreddit = reddit.subreddit('hiphopheads') # create spotipy obj From af3af8e87413e5f5b656bb4eecabe688f1119488 Mon Sep 17 00:00:00 2001 From: Ben Price Date: Tue, 17 Sep 2019 22:17:18 -0700 Subject: [PATCH 2/2] Fix docstring (remove reference to non-existant parent class) --- fresh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fresh.py b/fresh.py index ffef178..cebbe6b 100644 --- a/fresh.py +++ b/fresh.py @@ -77,7 +77,7 @@ def createUserAgentString(config_file='credentials.json'): Raises ------ - InvalidConfigFile: ConfigurationError + InvalidConfigFile: if config_file is missing a certain section or key """ platform = "Python3"