-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_to_playlist
More file actions
executable file
·113 lines (84 loc) · 3.13 KB
/
sql_to_playlist
File metadata and controls
executable file
·113 lines (84 loc) · 3.13 KB
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
#!/usr/bin/env python3
# Copyright 2020 Alex K (wtwf.com)
# PYTHON_ARGCOMPLETE_OK
"""Convert an SQL statement into an itunes playlist.
SQL must have a Persistent_ID column in it.
Database should be created with https://github.com/arkarkark/itdb
"""
__author__ = "wtwf.com (Alex K)"
import argparse
import atexit
import configparser
import logging
import os
import sys
import appscript
import argcomplete
import MySQLdb
TYPES_TO_PLAYLIST = {"Music": "Library", "Movies": "Movies", "TV Shows": "TV Shows"}
def usage(code, msg=""):
"""Show a usage message."""
file = sys.stderr if code else sys.stdout
PROGRAM = os.path.basename( # pylint: disable=invalid-name,possibly-unused-variable
sys.argv[0]
)
print(__doc__ % locals(), file=file)
if msg:
print(msg, file=file)
sys.exit(code)
def playlist_from_results(name, rows, source_name):
"Use appscript to make a playlist and add tracks to it."
i = appscript.app("iTunes")
library = i.playlists[appscript.its.name == source_name]()[0]
# TODO: maybe check to see if a playlist with this name already exists?
playlist = library.make(new=appscript.k.playlist)
playlist.name.set(name)
for row in rows:
print("name" in row and row["name"] or row["Persistent_ID"])
track = library.tracks[appscript.its.persistent_ID == row["Persistent_ID"]]()
if track:
x = track[0].duplicate(to=playlist)
def sql_to_playlist(config, sql, playlist, source_name):
"Connect and make a playlist from some SQL."
conn = MySQLdb.connect(
host=config.get("client", "host"),
db=config.get("client", "database"),
user=config.get("client", "user"),
passwd=config.get("client", "password"),
)
atexit.register(conn.close)
logging.debug("connected")
conn.autocommit(True)
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(sql)
rows = cursor.fetchall()
playlist_from_results(playlist, rows, source_name)
def main():
"""Parse args and do the thing."""
logging.basicConfig()
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--sql", help="SQL")
parser.add_argument("-p", "--playlist", help="playlist name")
parser.add_argument("-v", "--verbose", help="Log verbosely", action="store_true")
parser.add_argument("-d", "--debug", help="Log debug messages", action="store_true")
parser.add_argument(
"-t",
"--type",
help="What type of tracks are they",
default="Music",
choices=TYPES_TO_PLAYLIST.keys(),
)
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.info("Log")
if not args.sql or not args.playlist:
usage(code=1, msg="You must provide some SQL and a Playlist name")
config = configparser.ConfigParser()
config.read(["itdb.config", os.path.expanduser("~/.itdb.config")])
sql_to_playlist(config, args.sql, args.playlist, TYPES_TO_PLAYLIST[args.type])
if __name__ == "__main__":
main()