Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a unique user-agent when creating praw.Reddit instance #64

Merged
merged 2 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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