-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbums.py
194 lines (153 loc) · 6.69 KB
/
albums.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from __future__ import print_function
#spotify imports
import sys
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
#GOOGLE IMPORTS AND TOKEN STUFF
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from pprint import pprint
from googleapiclient import discovery
google_cid = '709831756208-hc395jqnaeq4gmrhgdp27243v38pbe2f.apps.googleusercontent.com'
google_csec = '-W_7SzJWXQbGE1zzKhA5wsdm'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SPREADSHEET_ID = '1NbsNJd2C2bJMwI8mRA0UPQVq5PNhnh-qxDtJbfYFu3E'
def createSpreadsheet(sheet_title):
# TODO: Change placeholder below to generate authentication credentials. See
# https://developers.google.com/sheets/quickstart/python#step_3_set_up_the_sample
#
# Authorize using one of the following scopes:
# 'https://www.googleapis.com/auth/drive'
# 'https://www.googleapis.com/auth/drive.file'
# 'https://www.googleapis.com/auth/spreadsheets'
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = discovery.build('sheets', 'v4', credentials=creds)
# The spreadsheet to apply the updates to.
spreadsheet_id = SPREADSHEET_ID # TODO: Update placeholder value.
batch_update_spreadsheet_request_body = {
# A list of updates to apply to the spreadsheet.
# Requests will be applied in the order they are specified.
# If any request is not valid, no requests will be applied.
"requests": [
{
"addSheet": {
"properties": {
"title": sheet_title,
"gridProperties": {
"rowCount": 500,
"columnCount": 12
},
"tabColor": {
"red": 1.0,
"green": 0.3,
"blue": 0.4
}
}
}
}
] # TODO: Update placeholder value.
# TODO: Add desired entries to the request body.
}
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=batch_update_spreadsheet_request_body)
response = request.execute()
def appendSong():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = discovery.build('sheets', 'v4', credentials=creds)
spreadsheet_id = SPREADSHEET_ID # TODO: Update placeholder value.
# The A1 notation of a range to search for a logical table of data.
# Values will be appended after the last row of the table.
range_ = 'Sheet1!A1:B1' # TODO: Update placeholder value.
# How the input data should be interpreted.
value_input_option = 'RAW' # TODO: Update placeholder value.
# How the input data should be inserted.
insert_data_option = 'INSERT_ROWS' # TODO: Update placeholder value.
value_range_body = {
# TODO: Add desired entries to the request body.
'values' : [[3,4]]
}
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
response = request.execute()
util.prompt_for_user_token('trevwilliams107',
scope='playlist-modify-private,playlist-modify-public',
client_id='20624df21d7c41288ee2b990bcd14050',
client_secret='c3e9c3e71deb4d09ba2ef2441c305cc8',
redirect_uri='https://localhost:8000')
def show_tracks(tracks):
for i, item in enumerate(tracks['items']):
track = item['track']
print(" %d %32.32s %s" % (i, track['artists'][0]['name'],
track['name']))
if __name__ == '__main__':
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print("Whoops, need your username!")
print("usage: python user_playlists.py [username]")
sys.exit()
token = util.prompt_for_user_token(username,
scope='playlist-modify-private,playlist-modify-public',
client_id='20624df21d7c41288ee2b990bcd14050',
client_secret='c3e9c3e71deb4d09ba2ef2441c305cc8',
redirect_uri='https://localhost:8000')
if token:
sp = spotipy.Spotify(auth=token)
playlists = sp.user_playlists(username)
for playlist in playlists['items']:
if playlist['owner']['id'] == username:
print()
print(playlist['name'])
print (' total tracks', playlist['tracks']['total'])
results = sp.playlist(playlist['id'],
fields="tracks,next")
tracks = results['tracks']
show_tracks(tracks)
while tracks['next']:
tracks = sp.next(tracks)
show_tracks(tracks)
else:
print("Can't get token for", username)
# albums = results['items']
# while results['next']:
# results = spotify.next(results)
# albums.extend(results['items'])
#
# for album in albums:
# print(album['name'])