Skip to content

Commit

Permalink
Merge pull request #64 from pricebenjamin/unique-user-agent
Browse files Browse the repository at this point in the history
Use a unique user-agent when creating `praw.Reddit` instance
  • Loading branch information
amcquade authored Oct 20, 2019
2 parents 29a482a + af3af8e commit 76fa3d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ spotipy = "==2.4.4"
configparser = "==3.5.0"
cutie = "==0.2.2"
crontab = "*"
gitpython = "*"

[dev-packages]
pylint = "*"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
}
Expand Down
49 changes: 48 additions & 1 deletion fresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
"""
Expand Down Expand Up @@ -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:
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 76fa3d6

Please sign in to comment.