summaryrefslogtreecommitdiff
path: root/jamos
blob: 573dc1d4a5be487b8b8a24e41b4f4280c6749d19 (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
#!/usr/bin/env python3
import datetime
import glob
import json
import music_tag
import os
from os.path import isfile, join
from pathlib import Path
import readline
import youtube_dl


# MUSIC_DIRECTORY = "~/Music/"
MUSIC_DIRECTORY = "./music/"

COMMANDS = ['extra', 'extension', 'stuff', 'errors',
            'email', 'foobar', 'foo']
FILES = []
DIRECTORIES = []

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

ytdl = youtube_dl.YoutubeDL(AUDIO_OPTIONS)


# TODO: Pull via command line args
def get_playlist_url():
    # return "https://music.youtube.com/watch?v=Enm0XL7xx_E&feature=share" # Deer Tick
    # return "https://music.youtube.com/watch?v=hJLb0zPBzkE&feature=share" # You Worry Me
    # return "https://music.youtube.com/playlist?list=OLAK5uy_lHnMUbm8pKsyMTRQNCrjM2v4CPvIJUWq0&feature=share" # Button the busker
    # return "https://music.youtube.com/playlist?list=PLC-Ro2Hd9eWm1ZPIAsxFcfg2JoOlTO0Oj&feature=share" # broken hearts and dirty windows volume 2
    # return "https://music.youtube.com/playlist?list=PLC-Ro2Hd9eWkX4hDamDvyB5G-vxLTEBKl&feature=share" # dev playlist
    return "https://music.youtube.com/playlist?list=PLC-Ro2Hd9eWkoElrTZIcCVCcdxBQzojsZ&feature=share" # missing playlist


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


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

    f['name'] = metadata['title']
    f['artist'] = metadata['artist']
    f['album'] = metadata['album']
    f['year'] = format_youtube_date(metadata['release_date'])

    f.save()

def format_youtube_date(date):
    fmt = "%Y%m%d"
    d = datetime.datetime.strptime(date, fmt)
    return d.year

def sort_stuff():
    # read in all the files and directories to move
    get_all_files()

    # setup tab completion
    readline.parse_and_bind("tab: complete")
    readline.set_completer(complete)

    in_directory = False

    # loop over all the files left and
    print(len(FILES))
    for f in FILES:
        in_directory = False
        while not in_directory:
            print(f)
            directory = input('Enter directory: ')
            if directory in DIRECTORIES:
                move_file(directory, f)
                in_directory = True
            if directory == "refresh":
                get_directories()


def complete(text, state):
    for cmd in DIRECTORIES:
        if cmd.startswith(text):
            if not state:
                return cmd
            else:
                state -= 1


def get_all_files():
    files = os.listdir()
    global FILES
    global DIRECTORIES

    for f in files:
        if os.path.isfile(f):
            FILES.append(f)
        elif os.path.isdir(f):
            DIRECTORIES.append(f)
        else:
            raise Exception("File wasn't a file or directory! " + f)
    FILES.sort()


def get_directories():
    files = os.listdir()
    global DIRECTORIES

    for f in files:
        if os.path.isdir(f):
            DIRECTORIES.append(f)


def move_file(d, f):
    # print("directory " + d + " file: " + f)
    print(d + "/" + f)
    os.rename(f, (d + "/" + f))


if __name__ == "__main__":
    # Get the playlist url from youtube music
    playlist_url = get_playlist_url()

    download_song(playlist_url)