summaryrefslogtreecommitdiff
path: root/jamos
blob: dc8615880225acd8a18f8568d85464531ba538e7 (plain)
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
195
196
197
198
#!/usr/bin/env python3
import argparse
import datetime
import glob
import json
import music_tag
import os
from pathlib import Path
import sys
import youtube_dl

def cleanup_metadata_files(music_directory):
    files = glob.glob(os.path.join(music_directory, '*.json'))

    for file in files:
        os.remove(file)


def download_song(song_url, ytdl):
    """
    Download a song using youtube url and song title
    """
    return ytdl.extract_info(song_url, download=True)


def format_youtube_date(date):
    default = "Unknown Year"
    try:
        fmt = "%Y%m%d"
        d = datetime.datetime.strptime(date, fmt)
        return d.year
    except Exception as ex:
        print(ex)

    return default


def get_all_files(directory):
    things = glob.glob(os.path.join(directory, '*.mp3'))

    files = []

    for thing in things:
        if os.path.isfile(thing):
            files.append(thing)

    return files


def get_command_line_options():
    parser = argparse.ArgumentParser(description="Download songs from YouTube Music")
    parser.add_argument("url", metavar="string", type=str, help="Playlist or Song URL to download")
    parser.add_argument("-c", "--cookies", metavar="string", type=str, help="Cookie file to use.")
    parser.add_argument("-o", "--output", metavar="string", type=str, help="Output directory to use")

    return parser.parse_args()


# TODO: switch command line args to argparse
def get_playlist_url():
    return sys.argv[1]


def get_video_urls_in_playlist(playlist_url, ytdl):
    videos = ytdl.extract_info(playlist_url, download=False)

    urls = []
    for vid in videos['entries']:
        if 'webpage_url' in vid.keys() and vid['webpage_url'] is not None:
            urls.append(vid['webpage_url'])

    return urls


def move_file(file, metadata, output_directory):
    # TODO: Pass a jamos specific metadata object/dict
    # Then we can do this validation all at once

    artist = 'unknownartist'
    album = 'unknownalbum'
    title = 'unknownsong'

    if ('artist' in metadata.keys()) and (metadata['artist'] is not None):
        if len(metadata['artist'].split(',')) > 1:
            # If there are multiple artists, pick the first one
            # NOTE: This may break if the artist has a comma in their name
            artist = metadata['artist'].split(',')[0].replace(' ', '_').lower()
        else:
            artist = metadata['artist'].replace(' ', '_').lower()

    if ('album' in metadata.keys()) and (metadata['album'] is not None):
        album = metadata['album'].replace(' ', '_').replace(',', '').replace("'", "").replace('"', "").lower()

    if ('title' in metadata.keys()) and (metadata['title'] is not None):
        title = metadata['title'].replace(' ', '_').replace('/', '').lower()

    final_directory = os.path.join(
        output_directory,
        artist,
        album)

    Path(final_directory).mkdir(parents=True, exist_ok=True)

    # TODO: Include album title in filename
    # TODO: Research converting to mp3 instead of just naming it such.
    # TODO: Research better file formats over mp3?
    os.rename(
        file,
        os.path.join(final_directory, artist + '_' + title + '.mp3'))


def write_metadata_to_song_file(file, metadata):
    f = music_tag.load_file(file)

    artist = 'unknownartist'
    album = 'unknownalbum'
    title = 'unknownsong'
    year = 9999

    if ('artist' in metadata.keys()) and (metadata['artist'] is not None):
        if len(metadata['artist'].split(',')) > 1:
            # If there are multiple artists, pick the first one
            # NOTE: This may break if the artist has a comma in their name
            artist = metadata['artist'].split(',')[0].replace(' ', '_').lower()
        else:
            artist = metadata['artist'].replace(' ', '_').lower()

        artist = artist.replace('&', 'and')

    if ('album' in metadata.keys()) and (metadata['album'] is not None):
        album = metadata['album'].replace(' ', '_').lower()

    if ('title' in metadata.keys()) and (metadata['title'] is not None):
        title = metadata['title'].replace(' ', '_').lower()

    if ('release_date' in metadata.keys()):
        year = format_youtube_date(metadata['release_date'])

    f['name'] = title
    f['artist'] = artist
    f['album'] = album
    f['year'] = year

    f.save()


def create_downloader(music_directory, cookies):
    audio_options = {
            'format': 'mp3/bestaudio/best',
            'cookiefile': cookies,
            'outtmpl': music_directory + '%(title)s.%(ext)s',
            'postprocessors': [
                {
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',
                },
                {'key': 'FFmpegMetadata'},
            ],
            'writeinfojson': True
        }

    return youtube_dl.YoutubeDL(audio_options)


if __name__ == "__main__":
    args = get_command_line_options()

    # Get the playlist url from the command line
    playlist_url = args.url

    music_directory = args.output or "~/Music"
    cookies = args.cookies or "~/cookies.txt"

    ytdl = create_downloader(music_directory, cookies)

    # TODO: Save urls to file so we can start in the
    # middle of the playlist if needed
    urls = get_video_urls_in_playlist(playlist_url, ytdl)

    for url in urls:
        try:
            download_song(url, ytdl)
        except Exception as ex:
            # TODO: Handle this better
            print(ex)

    files = get_all_files(music_directory)

    for f in files:
        json_data = None
        with open(f.replace('.mp3', '.info.json')) as json_file:
            json_data = json.load(json_file)
            write_metadata_to_song_file(f, json_data)
        move_file(f, json_data, music_directory)

    cleanup_metadata_files(music_directory)