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

Implementing dotenv and Changing READ.ME Accordingly #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export SPOTIFY_USER_ID='edit me'
export SPOTIFY_CLIENT_ID='edit me'
export SPOTIFY_CLIENT_SECRET='edit me'
export SPOTIFY_REDIRECT_URI='edit me'
export YOUTUBE_API_KEY='edit me'
SPOTIFY_USER_ID='SPOTIFY_USERNAME'
SPOTIFY_CLIENT_ID='SPOTIFY_CLIENT_ID'
SPOTIFY_CLIENT_SECRET='SPOTIFY_CLIENT_SECRET'
SPOTIFY_REDIRECT_URI='http://localhost:8888/callback'
YOUTUBE_API_KEY='YOUTUBE_API_KEY'
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,15 @@ Reliable: Adds 85-95% of the songs from popular youtube playlists.
[CLICK HERE TO SEE HOW TO CREATE YOUTUBE API KEY](getkey.md)



### Setting Environment Variables Mac and Linux

1. Rename the file `.env.sample` to `.env`

2. Edit the file by adding your credentials

3. Run the following command to set your environment variable `source .env`


### Setting Environment Variables (Windows)
### Input Your Credentials
```
set SPOTIFY_USER_ID <your_user_id>
set SPOTIFY_CLIENT_ID <your_client_id>
set SPOTIFY_CLIENT_SECRET <your_client_secret>
set SPOTIFY_REDIRECT_URI 'http://localhost:8888/callback'
set YOUTUBE_API_KEY <your_youtube_api_key>
SPOTIFY_USER_ID <your_user_id>
SPOTIFY_CLIENT_ID <your_client_id>
SPOTIFY_CLIENT_SECRET <your_client_secret>
SPOTIFY_REDIRECT_URI 'http://localhost:8888/callback'
YOUTUBE_API_KEY <your_youtube_api_key>
```
Change `.env.sample` to `.env`.

## Usage
`$ python main.py`
Expand Down
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from spotify import Spotify
from youtube import Youtube
import os
from dotenv import load_dotenv

def main():
sp = Spotify()
yt = Youtube()
load_dotenv()
sp = Spotify(os.environ.get('SPOTIFY_USER_ID'), os.environ.get('SPOTIFY_CLIENT_ID'), os.environ.get('SPOTIFY_CLIENT_SECRET'), os.environ.get('SPOTIFY_REDIRECT_URI'))
yt = Youtube(os.environ.get('YOUTUBE_API_KEY'))

yt_playlist_id = input("Enter youtube playlist id: ")
spotify_playlist_name = input("Enter a name for your spotify playlist: ")
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ spotipy==2.16.1
requests==2.25.0
oauth2client==4.1.3
youtube-title-parse==1.0.0
google-api-python-client==1.12.8
google-api-python-client==1.12.8
python-dotenv==1.0.0
15 changes: 8 additions & 7 deletions spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import os
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be removed.




class SpotifyClientManager:
def __init__(self):
def __init__(self, SPOTIFY_USER_ID, SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REDIRECT_URI):
self.scope = 'playlist-modify-private'
self.user_id = os.getenv('SPOTIFY_USER_ID')
self.client_id = os.getenv('SPOTIFY_CLIENT_ID')
self.client_secret = os.getenv('SPOTIFY_CLIENT_SECRET')
self.redirect_uri = os.getenv('SPOTIFY_REDIRECT_URI')
self.user_id = SPOTIFY_USER_ID
self.client_id = SPOTIFY_CLIENT_ID
self.client_secret = SPOTIFY_CLIENT_SECRET
self.redirect_uri = SPOTIFY_REDIRECT_URI

@property
def token(self):
Expand All @@ -28,8 +29,8 @@ def token(self):


class Spotify:
def __init__(self):
self.spotify = SpotifyClientManager()
def __init__(self, SPOTIFY_USER_ID, SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REDIRECT_URI):
self.spotify = SpotifyClientManager(SPOTIFY_USER_ID, SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REDIRECT_URI)

def create_playlist(self, playlist_name: str) -> str:
request_body = {
Expand Down
8 changes: 4 additions & 4 deletions youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from dataclasses import dataclass
from pprint import pprint
import re
import os


@dataclass
Expand All @@ -26,16 +25,17 @@ def clean_song_info(song: Song) -> Song:


class Youtube:
DEVELOPER_KEY = os.getenv('YOUTUBE_API_KEY')
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def __init__(self):
def __init__(self, DEVELOPER_KEY):
print(DEVELOPER_KEY)
DEVELOPER_KEY = DEVELOPER_KEY
self.songs = []
self.youtube = build(
Youtube.YOUTUBE_API_SERVICE_NAME,
Youtube.YOUTUBE_API_VERSION,
developerKey=Youtube.DEVELOPER_KEY
developerKey=DEVELOPER_KEY
)

def __fetch_songs(self, youtube, playlist_id, page_token=None):
Expand Down