-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
85 lines (65 loc) · 3.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import json
import os
import re
import cv2
import requests.exceptions
import spotipy
from pytesseract import pytesseract, Output
from spotipy.oauth2 import SpotifyOAuth
def connectToSpotify():
# print("Get a client ID & client secret from: https://developer.spotify.com/dashboard/applications")
os.environ['SPOTIPY_REDIRECT_URI'] = 'http://127.0.0.1:8080/'
# os.environ['SPOTIPY_CLIENT_ID'] = input("Enter your client ID: ")
# os.environ['SPOTIPY_CLIENT_SECRET'] = input("Enter your client secret: ")
def usernameAuthenticator():
try:
print("Note that entering the wrong username will currently crash the application.")
return "lvrkhn" # input("Enter your username: ")
except requests.exceptions.HTTPError or spotipy.SpotifyException: # Does not work
print("You can not create a playlist for another user.")
usernameAuthenticator()
# Setting up the Spotify API
scope = "playlist-modify-public"
username = usernameAuthenticator()
connectToSpotify()
token = SpotifyOAuth(scope=scope, username=username)
spotifyObject = spotipy.Spotify(auth_manager=token)
# Creating the playlist
# playlistName = input("Enter a playlist name: ")
playlistName = "ss-test"
playlistDesc = "Created with ss-to-spotify: https://github.com/acariaC/ss-to-spotify"
# spotifyObject.user_playlist_create(user=username, name=playlistName, public=True, description=playlistDesc)
listOfSongs = []
# Getting the images from a directory
folder_dir = "/Users/oliverkuhn/Documents/iCloud Photos/Sample/" # input("Please choose your photo directory: ")
img = cv2.imread(folder_dir)
for images in os.listdir(folder_dir):
img = cv2.imread(folder_dir + images)
if images.endswith('.PNG') or (images.endswith('.jpeg')):
dimensions = img.shape
xyRatio = float(format(img.shape[1] / img.shape[0], ".2f")) # Might be optimized with round() later
if xyRatio == 0.46:
# cv2.rectangle(img, (603, 1304), (603, 1304), (255, 0, 0), -1)
cropped_image = img[int(img.shape[0] * 0.25): int(img.shape[0] * 0.75)]
# cv2.rectangle(cropped_image, (100, 500), (100, 500), (255, 0, 0), -1)
cv2.imshow("Cropped Image", cropped_image)
cv2.waitKey(0)
artistAndTitle = (pytesseract.image_to_string(cropped_image))
result = re.sub(r"[^a-zA-Z0-9]+", ' ', artistAndTitle).strip()
if result:
lengthCheck = len(result.split())
if 3 <= lengthCheck < 30:
song = spotifyObject.search(q=result)
try:
print('Track: ' + song['tracks']['items'][0]['name'])
print('Artist: ' + song['tracks']['items'][0]['album']['artists'][0]['name'] + '\n')
listOfSongs.append(song['tracks']['items'][0]['uri'])
except IndexError:
print("No song found. \n")
# Finding the new playlist
prePlaylist = spotifyObject.user_playlists(user=username)
playlist = prePlaylist['items'][0]['id']
# Adding songs
print(listOfSongs)
spotifyObject.user_playlist_add_tracks(user=username, playlist_id=playlist, tracks=listOfSongs)
print("Finished.")