Skip to content

Commit

Permalink
Merge pull request #55 from dtcrout/load-credentials-file
Browse files Browse the repository at this point in the history
Load credentials file
  • Loading branch information
amcquade committed Aug 8, 2019
2 parents cae7509 + c67cd3e commit 29a482a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ praw.ini
__pycache__
*.ini`
venv/*
credentials.json
27 changes: 25 additions & 2 deletions README.md
Expand Up @@ -9,13 +9,36 @@ This program will search for spotify tracks posted in the HipHopHeads subreddit

### Prerequisites

This project uses Python3. You will need to setup a Spotify developer account and register your app. You will need the following things for this code:
This project uses Python3.

You will need to setup a Spotify developer account and register your app and obtain the following information:
* client id
* client secret
* your spotify username
* playlist id of the playlist you want to add the tracks to
* the url you want to redirect to for authentication, i.e. http://google.com/
You will also need to setup a reddit instance with praw. [Heres](https://pythonforengineers.com/build-a-reddit-bot-part-1/) a useful guide I used to do this.
* this must be added under your app settings > Redirect URIs

You will also need to setup a reddit instance with praw. [Here's](https://pythonforengineers.com/build-a-reddit-bot-part-1/) a useful guide I used to do this.

### Setup your Credentials

To set up your credentials, create a new file called `credentials.json` in the root of the project with the following contents:

```
{
"spotify": {
"username": "[Spotify username]",
"client_id": "[Spotify client id]",
"client_secret": "[Spotify client secret]",
"redirect": "[redirect uri]"
},
"reddit": {
"client_id": "[praw client id]",
"client_secret": "[praw client secret]"
}
}
```

### Installing dependencies
This project uses a dependency manager called [pipenv](https://pipenv.readthedocs.io). Follow the instructions to install it [here](https://pipenv.readthedocs.io/en/latest/install/#installing-pipenv).
Expand Down
101 changes: 77 additions & 24 deletions fresh.py
Expand Up @@ -13,12 +13,85 @@
from models import User
import cutie

def createUserConfig(user, config_path='.config.ini'):
"""
Create .config.ini file for Spotify credentials.
Parameters
----------
user: User object
Spotify user object.
config_path: str
Path to .config.ini.
"""
s_config = ConfigParser()
s_config['spotify'] = {
'client_id': user.client_id,
'client_secret': user.client_secret,
'username': user.username,
'playlist_id': user.getPlaylistsAsString(),
'redirect_uri': user.redirect
}

with open(config_path, 'w') as f:
s_config.write(f)

def createPrawConfig(client_id, client_secret,
praw_path='praw.ini'):
"""
Create praw.ini file for Reddit credentials.
Parameters
----------
client_id: str
Reddit app client id.
client_secret: str
Reddit app client secret.
praw_path: str
Path to praw.ini.
"""
r_config = ConfigParser()
r_config['bot1'] = {
'client_id': client_id,
'client_secret': client_secret,
'user_agent': 'FreshScript'
}

with open(praw_path, 'w') as p:
r_config.write(p)

def createUser():
user = None
# read config file
try:
if not os.path.isfile('.config.ini'):
if os.path.exists('credentials.json') and not os.path.isfile('.config.ini'):
# load credentials file
with open('credentials.json', 'r') as f:
credentials = json.load(f)

s_credentials = credentials['spotify']
p_credentials = credentials['reddit']

user = User(s_credentials['username'],
s_credentials['client_id'],
s_credentials['client_secret'],
s_credentials['redirect'],
[]
)

user.addPlaylists()

# write config files
createUserConfig(user)

createPrawConfig(p_credentials['client_id'],
p_credentials['client_secret'])

elif not os.path.isfile('.config.ini'):
print('Credentials file not found!')

# get credentials
s_client_id = input('Enter your Spotify Client ID: ').strip()
s_client_secret = input(
Expand All @@ -31,29 +104,9 @@ def createUser():
user = User(username, s_client_id, s_client_secret, redirect, [])
user.addPlaylists()

# write spotify config
s_config = ConfigParser()
s_config['spotify'] = {
'client_id': s_client_id,
'client_secret': s_client_secret,
'username': username,
'playlist_id': user.getPlaylistsAsString(),
'redirect_uri': redirect
}

# write praw config
r_config = ConfigParser()
r_config['bot1'] = {
'client_id': r_client_id,
'client_secret': r_client_secret,
'user_agent': 'FreshScript'

}
with open('.config.ini', 'w') as f:
s_config.write(f)

with open('praw.ini', 'w') as p:
r_config.write(p)
# write config files
createUserConfig(user)
createPrawConfig(r_client_id, r_client_secret)

else:
# parse config
Expand Down

0 comments on commit 29a482a

Please sign in to comment.