-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusical_Tracksdb.py
More file actions
120 lines (99 loc) · 3.63 KB
/
Musical_Tracksdb.py
File metadata and controls
120 lines (99 loc) · 3.63 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
114
115
116
117
118
119
120
#import libraries (xml & sqlite database)
import xml.etree.ElementTree as ET
import sqlite3
#makes a databse connection
conn = sqlite3.connect('musical_tracksdb.sqlite')
cur = conn.cursor()
# create tables using sql command executescript()
cur.executescript('''
DROP TABLE IF EXISTS Artist;
DROP TABLE IF EXISTS Genre;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Track;
CREATE TABLE Artist (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT UNIQUE
);
CREATE TABLE Genre (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT UNIQUE
);
CREATE TABLE Album (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
artist_id INTEGER,
title TEXT UNIQUE
);
CREATE TABLE Track (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
title TEXT UNIQUE,
album_id INTEGER,
genre_id INTEGER,
len INTEGER, rating INTEGER, count INTEGER
);
''')
file_name = input('Enter file name: - ')
if (len(file_name) < 1): file_name = 'Library.xml'
# structure of Library xml file
# <key>Track ID</key><integer>369</integer>
# <key>Name</key><string>Another One Bites The Dust</string>
# <key>Artist</key><string>Queen</string>
#lookup function to browse the xml file
def lookup(d,key):
found = False
for child in d:
if found: return child.text
if child.tag == 'key' and child.text == key:
found = True
return None
#parse xml file
stuff = ET.parse(file_name)
all = stuff.findall('dict/dict/dict')
print('Dict count:', len(all))
#print(clean_data)
#iteract through clean_data
for entry in all:
if (lookup(entry,'Track ID') is None): continue
#find specific data entry
title = lookup(entry, 'Name')
artist = lookup(entry, 'Artist')
album = lookup(entry,'Album')
genre = lookup(entry,'Genre')
count = lookup(entry,'Play Count')
rating = lookup(entry,'Rating')
length = lookup(entry,'Total Time')
#checks for nontype data and skip further computition if true
if title is None or artist is None or album is None or genre is None:
continue
print()
print('Title:',title)
print('Artist:',artist)
print('Album:',album)
print('Genre:',genre)
print( 'Play Count:',count)
print('Rating:',rating)
print('Play Time:',length)
#execute sql queries based on extracted datasets to populate database
cur.execute('INSERT OR IGNORE INTO Artist(name) VALUES(?)',(artist,))
cur.execute('SELECT id FROM Artist WHERE name = ?',(artist,))
artist_id = cur.fetchone()[0]
cur.execute('INSERT OR IGNORE INTO Genre(name) VALUES(?)',(genre,))
cur.execute('SELECT id FROM Genre WHERE name = ?',(genre,))
genre_id = cur.fetchone()[0]
cur.execute('INSERT OR IGNORE INTO Album(title,artist_id) VALUES(?,?)',(album,artist_id))
cur.execute('SELECT id FROM Album WHERE title = ?',(album,))
album_id = cur.fetchone()[0]
cur.execute('''INSERT OR IGNORE INTO Track(title,album_id,genre_id,len,rating,count)
VALUES(?,?,?,?,?,?)''',(title,album_id,genre_id,length,rating,count))
conn.commit()
print()
print('Database query is printed below this line:')
#send query to database
sql_query = ('''SELECT Track.title, Artist.name, Album.title, Genre.name
FROM Track JOIN Genre JOIN Album JOIN Artist
ON Track.genre_id = Genre.ID and Track.album_id = Album.id
AND Album.artist_id = Artist.id
ORDER BY Artist.name ASC LIMIT 3''')
#loop thru sql_query
for row in cur.execute(sql_query):
print(row[0],row[1],row[2],row[3])
cur.close