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

Issue #52 partially resolved:= UnboundLocalError #74

Closed
wants to merge 2 commits into from
Closed
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
52 changes: 35 additions & 17 deletions fresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import os
import json
import logging
import webbrowser
import textwrap
import spotipy
Expand All @@ -13,6 +14,22 @@
from models import User
import cutie

# Adding formatter for the logger
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Adding file handler to write logs to file
fresh_fh = logging.FileHandler('fresh.log')
fresh_fh.setLevel(logging.DEBUG)
fresh_fh.setFormatter(formatter)
# Adding console handler to write logs to console as well
fresh_ch = logging.StreamHandler()
fresh_ch.setLevel(logging.ERROR)
fresh_ch.setFormatter(formatter)
# Initiating a logger object and adding all handlers to it
logger = logging.getLogger('fresh_logs')
logger.setLevel(logging.DEBUG)
logger.addHandler(fresh_fh)
logger.addHandler(fresh_ch)

def createUserConfig(user, config_path='.config.ini'):
"""
Create .config.ini file for Spotify credentials.
Expand Down Expand Up @@ -90,7 +107,7 @@ def createUser():
p_credentials['client_secret'])

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

# get credentials
s_client_id = input('Enter your Spotify Client ID: ').strip()
Expand Down Expand Up @@ -128,7 +145,7 @@ def createUser():
config['soundcloud'] = {}
'''
except Exception as e:
print(f'config failure: {e}')
logger.error(f'config failure: {e}')

return user

Expand Down Expand Up @@ -313,20 +330,20 @@ def process_subreddit(subreddit, choice, l):
elif choice.lower() == 'top':
sub_choice = subreddit.top(limit=l)
else:
print("Unsupported sorting method")
logger.error("Unsupported sorting method")
sys.exit()
return sub_choice


def addSpotifyTrack(fresh, threshold, includeAlbums, verbose, sub, tracks):
def addSpotifyTrack(spotifyObj, fresh, threshold, includeAlbums, verbose, sub, tracks):
# check if post is a track or album
isMatch = re.search('(track|album)', sub.url)
if isMatch != None:
if verbose:
print("Post: ", sub.title)
print("URL: ", sub.url)
print("Score: ", sub.score)
print("------------------------\n")
logger.debug("Post: {}".format(sub.title))
logger.debug("URL: {}".format(sub.url))
logger.debug("Score: {}".format(sub.score))
logger.debug("------------------------\n")

# Discard post below threshold if given
if threshold and sub.score < threshold:
Expand Down Expand Up @@ -390,7 +407,7 @@ def main():
tracks_array = []
for sub in sub_choice:
if sub.domain == "open.spotify.com":
addSpotifyTrack(fresh, threshold, includeAlbums, verbose, sub, tracks)
addSpotifyTrack(spotifyObj, fresh, threshold, includeAlbums, verbose, sub, tracks)

else:
title, tags = filter_tags(sub.title)
Expand All @@ -407,10 +424,10 @@ def main():
otherDomainList = ['youtu.be', 'youtube.com', 'soundcloud.com']
# handle non-spotify posts
if sub.domain in otherDomainList and verbose:
print("Post: ", sub.title)
print("URL: ", sub.url)
print("Score: ", sub.score)
print("------------------------\n")
logger.debug("Post: {}".format(sub.title))
logger.debug("URL: {}".format(sub.url))
logger.debug("Score: {}".format(sub.score))
logger.debug("------------------------\n")

tracks.append(track_url)
# handle overflow
Expand All @@ -424,6 +441,7 @@ def main():
# handle remove duplicates of tracks before adding new tracks
if tracks != [] or tracks_array != []:
try:
results = []
if len(tracks_array) >= 1:
for tr in tracks_array:
for playlist in user.playlists:
Expand All @@ -435,14 +453,14 @@ def main():
results = spotifyObj.user_playlist_add_tracks(
user.username, playlist, tr)
if verbose:
print('New Tracks added to ', spotifyObj.user_playlist(user.username, playlist, 'name')['name'], ': ', abs(
existing_tracks['total'] - spotifyObj.user_playlist_tracks(user.username, playlist)['total']))
logger.info('New Tracks added to {}'.format((spotifyObj.user_playlist(user.username, playlist, 'name')['name'], ': ', abs(
existing_tracks['total'] - spotifyObj.user_playlist_tracks(user.username, playlist)['total']))))
print()
except:
if results == [] and verbose:
print("No new tracks have been added.")
logger.info("No new tracks have been added.")
else:
print("An error has occured removing or adding new tracks")
logger.error("An error has occured removing or adding new tracks")
# if verbose:
# print(tracks)

Expand Down