Skip to content

Commit

Permalink
more reconfig
Browse files Browse the repository at this point in the history
porting to AWS DynamoDB
  • Loading branch information
ATawzer committed Apr 13, 2021
1 parent 7cdcc57 commit 8a09269
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .cache-1241528689
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"access_token": "BQDwTmEmcwJN3c09yeD9hddeMnsXwRTx-AeFUuA_oKfB9HuShfQ2EA6w09ql0jZuVH-BLJth5if4xTceQscfZ3o-o950b8WDhVvuFhCxEZPDWfVcdfUgr9vvbgOFdlM7RmiBcMc8X9b53TjjSDLyQkLb8luH5p7H-7-MClK8TSwvvs__SB5C-zaIHPd24an59fYd2sec5vsBUoJq5xycJqk", "token_type": "Bearer", "expires_in": 3600, "scope": "playlist-modify-private playlist-modify-public user-follow-modify user-follow-read", "expires_at": 1617915446, "refresh_token": "AQAsxkWjXR0Iw8q65vbKmXUR0cOGEM8liRshm9vhsJbDenCcjijwBgyKF91oCqQ8NjdD8fwk3uO-NKGUVWYtWRF0E2f5ydGSyFlJRi29TR1Zyw71OKdaIs89XzUBfCOOO0M"}
{"access_token": "BQCXOeVx3tqRXMnJYZvyArG-IeXrr2w9JYbTNffOmHY0n1Sw2xsAse8KUJH193GwRDJnpAlylbrNwJzqfBQMvFOqHF1dgOBrovrPe2Udp3wWHCvs6k55gplLE4BlTjDH7IQdfpM-DSeHqfCnhndyqfIH80uTNWseiFgRsfjTMjUV9vysotl3np0uefImINLMLdhDrebuiOrjO_OvRxWl9lQ", "token_type": "Bearer", "expires_in": 3600, "scope": "playlist-modify-private playlist-modify-public user-follow-modify user-follow-read", "expires_at": 1618268185, "refresh_token": "AQAsxkWjXR0Iw8q65vbKmXUR0cOGEM8liRshm9vhsJbDenCcjijwBgyKF91oCqQ8NjdD8fwk3uO-NKGUVWYtWRF0E2f5ydGSyFlJRi29TR1Zyw71OKdaIs89XzUBfCOOO0M"}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ storm/config/config_secret.json
.vscode
.ipynb_checkpoints

.token
token.json
11 changes: 0 additions & 11 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,3 @@
from dotenv import load_dotenv
load_dotenv()

def get_storm_client():
"""
Based on config_secret.json will connect into the storm MongoDB database
"""

# Connect to DB using .env
client = MongoClient(os.getenv('mongodb_uri'),
tls=True,
tlsCertificateKeyFile=os.getenv('ssl_path'))

return client
54 changes: 52 additions & 2 deletions src/storm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,64 @@
import numpy as np
import pandas as pd
from tqdm import tqdm
from os import path
import os
import datetime as dt
import time
import json

# Creds
# DB
from pymongo import MongoClient
from dotenv import load_dotenv
load_dotenv()


class StormClient:

def __init__(self, user_id):

self.scope = 'user-follow-read playlist-modify-private playlist-modify-public user-follow-modify' # scope for permissions
self.user_id = user_id
self.client_id = os.getenv('storm_client_id') # API app id
self.client_secret = os.getenv('storm_client_secret') # API app secret

# DB connection
self.mc = MongoClient(os.getenv('mongodb_uri'))
self.db = self.mc['storm']

# Spotify API connection
self.sp = None
self.token_end = None
self.get_token()

# Authentication
def get_token(self):

if os.path.exists('token.json'):
with json.load(open('token.json', "r")) as f:
if dt.datetime.fromtimestamp(f['expires']) < dt.datetime.now():
self.token = f['token']
self.token_end = f['expires']

else:
self.get_new_token()

self.sp = spotipy.Spotify(auth=self.token)

def get_new_token(self):

self.token = util.prompt_for_user_token(self.user_id,
scope=self.scope,
client_id=self.client_id,
client_secret=self.client_secret,
redirect_uri='http://localhost/')

self.token_end = dt.datetime.timestamp(dt.datetime.now() + dt.timedelta(minutes=59))
json.dump({'token':self.token, 'expires':str(self.token_end)}, open('token.json', 'w'))


storm = StormClient('1241528689')


# A class to manage all of the storm functions and authentication
class Storm:
"""
Expand Down

0 comments on commit 8a09269

Please sign in to comment.