-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_fct.py
executable file
·91 lines (81 loc) · 3.69 KB
/
class_fct.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
""" Fonctions which are related to class"""
import spotipy
import classes
import json
from urllib.parse import unquote
from flask import flash
from youtube_dl import YoutubeDL
from decouple import config
from os.path import join
def create_track_obj(playlist_id, tokenSpotify):
sp = spotipy.Spotify(auth=tokenSpotify)
try:
playlist_name = sp.playlist(playlist_id, fields="name")["name"]
except:
return False
list_track = [
playlist_name
] # list of the tracks return the name of the list as first element (pas propre, à changer !)
results = sp.playlist_tracks(playlist_id)
for info in results["items"]:
list_track.append(classes.Track(info["track"]))
print("------------------------------------------------------------------")
return list_track
def create_jsonFile(results, playlist_id):
"""make a json file from the results of the research
return the json file name """
data = {}
data['playlist'] = {"name": results[0], "id": playlist_id}
data['tracks'] = []
for track in results[1:]:
current_track = {}
spotifyInfo = {}
spotifyInfo['title'] = track.SpotifyTrack.title
spotifyInfo['artist'] = track.SpotifyTrack.artist
spotifyInfo['time'] = track.SpotifyTrack.time
spotifyInfo['min'] = track.SpotifyTrack.min
spotifyInfo['sec'] = track.SpotifyTrack.sec
youtubeInfo = {}
youtubeInfo['id'] = track.YoutubePage.id
youtubeInfo['URL'] = track.YoutubePage.URL
youtubeInfo['title'] = track.YoutubePage.title
youtubeInfo['time'] = track.YoutubePage.time
youtubeInfo['min'] = track.YoutubePage.min
youtubeInfo['sec'] = track.YoutubePage.sec
current_track['spotifyInfo'] = spotifyInfo
current_track['youtubeInfo'] = youtubeInfo
data['tracks'].append(current_track)
path = "playlist_informations/{}.json".format(playlist_id)
with open(path, "w") as outJsonFile: # TODO: Automatisation of directory playlist_informations creation
json.dump(data, outJsonFile)
def openPlaylistJson(json_id):
json_file_path = "playlist_informations/{}.json".format(json_id)
with open(json_file_path, 'r') as json_file:
playlist_data = json.load(json_file)
return playlist_data
def downloadYt(json_id, ids):
playlist_data = openPlaylistJson(json_id)
for id in ids:
id = int(id) - 1 # Jinja iteration start to 1 and no 0
path = join(config('EXPORT_DIR'), playlist_data['playlist']['name'],
f"{playlist_data['tracks'][id]['spotifyInfo']['title']} - {playlist_data['tracks'][id]['spotifyInfo']['artist']}")
print(path)
ydl_opts = {
'outtmpl': unquote(path),
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3' }]
}
ydl = YoutubeDL(ydl_opts)
try:
with ydl:
ydl.download(
[playlist_data['tracks'][id]['youtubeInfo']['URL']])
if len(ids) == 1:
flash("The file {} - {}.mp3 has been successfully downloaded in file /{} !".format(playlist_data['tracks']
[id]['spotifyInfo']['title'],
playlist_data['tracks'][id]['spotifyInfo']['artist'],
playlist_data['playlist']['name']), 'success')
except:
flash("An error occured during download !", 'error')