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

Load credentials file #55

Merged
merged 5 commits into from Aug 8, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -4,3 +4,4 @@ praw.ini
__pycache__
*.ini`
venv/*
credentials.json
@@ -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).
101 fresh.py
@@ -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(
@@ -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
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.