00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 import os, sys
00025 from time import time
00026 try:
00027 from pysqlite2 import dbapi2 as sqlite3
00028 except ImportError:
00029 import sqlite3
00030
00031 from modules import functions
00032
00033
00034 functions.translation_gettext()
00035
00036
00037
00038 from clGstreamer010 import Track
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 class DB_Settings():
00050
00051
00052
00053 def __init__(self, pyjama):
00054
00055 self.pyjama = pyjama
00056
00057 self.db = os.path.join(functions.preparedirs(), "settings.db")
00058
00059 self.connection = None
00060
00061 self.query_counter = 0
00062
00063 if not os.path.exists(self.db):
00064 self.open()
00065 self.create_database()
00066 else:
00067 self.open()
00068
00069
00070
00071
00072
00073
00074
00075 def set_value(self, section, option, value, table="settings"):
00076 sql = "SELECT COUNT(*) FROM %s WHERE section='%s' and option='%s'" % (table, section, option)
00077 ret = self.query(sql)
00078
00079 if ret[0][0] == 0:
00080 sql = "INSERT INTO %s (section, option, value) values('%s', '%s', '%s')" % (table, section, option, value)
00081 else:
00082 sql = "UPDATE %s SET value='%s' WHERE section='%s' and option='%s'" % (table, value, section, option)
00083
00084 self.connection.execute(sql)
00085 self.connection.commit()
00086
00087 self.query_counter += 1
00088
00089 def increase_value(self, section, option, table="settings"):
00090 sql = "SELECT value FROM %s WHERE section='%s' and option='%s'" % (table, section, option)
00091 ret = self.query(sql)
00092
00093 if ret == []:
00094 sql = "INSERT INTO %s (section, option, value) values('%s', '%s', '1')" % (table, section, option)
00095 else:
00096 sql = "UPDATE %s SET value=value+1 WHERE section='%s' and option='%s'" % (table, section, option)
00097
00098 self.connection.execute(sql)
00099 self.connection.commit()
00100
00101 self.query_counter += 1
00102
00103
00104
00105
00106
00107
00108
00109 def get_value(self, section, option, default=None, table="settings"):
00110 sql = "SELECT value FROM %s WHERE section='%s' and option='%s'" % (table, section, option)
00111 ret = self.query(sql)
00112 if ret == [] or ret == -1:
00113 return default
00114 else:
00115 if len(ret[0]) == 1:
00116 return ret[0][0]
00117 else:
00118 return ret[0]
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 def create_table(self, table_name):
00132 sql = "select count(name) from sqlite_master where name = '%s'" % table_name
00133 ret = self.query(sql)
00134
00135 if ret[0][0] == 0:
00136 sql = """
00137 CREATE TABLE %s (
00138 uid INTEGER PRIMARY KEY,
00139 section STRING,
00140 option STRING,
00141 value string
00142 )
00143 """ % table_name
00144 self.connection.execute(sql)
00145 self.connection.commit()
00146 return "created"
00147 else:
00148 return "existant"
00149
00150
00151
00152 def create_database(self):
00153 print("Creating settings database 'settings.db'")
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 sql = """
00166 CREATE TABLE settings (
00167 uid INTEGER PRIMARY KEY,
00168 section STRING,
00169 option STRING,
00170 value string
00171 )
00172 """
00173 self.connection.execute(sql)
00174 self.connection.commit()
00175
00176
00177
00178 def open(self):
00179 self.connection = sqlite3.connect(self.db)
00180
00181
00182
00183 def close(self):
00184 self.connection.close()
00185
00186 def quit(self):
00187 pass
00188
00189
00190
00191
00192 def query(self, query):
00193 self.query_counter += 1
00194 try:
00195 cur = self.connection.cursor()
00196 cur.execute(query)
00197 rows = cur.fetchall()
00198 return rows
00199 except Exception, inst:
00200 desc = "There was an error while querying the settings database"
00201 self.pyjama.Events.raise_event("error", inst, desc)
00202 return -1
00203
00204
00205
00206
00207
00208
00209 class DB():
00210
00211
00212
00213 def __init__(self, par):
00214
00215 self.parent = par
00216
00217 self.home=functions.preparedirs()
00218
00219 self.db = os.path.join(self.home, "pyjama.db")
00220
00221 self.tracks, self.albums, self.artists = 0, 0, 0
00222 self.query_counter = 0
00223
00224
00225 self.results = {}
00226
00227
00228 self.queue = []
00229
00230
00231 self.connection = None
00232
00233 self.database_ok = True
00234 if not os.path.exists(os.path.join(functions.preparedirs(),"pyjama.db")):
00235 self.database_ok = False
00236 else:
00237 sql = "select count(name) from sqlite_master where name = 'albums' or name = 'artists' or name = 'tracks'"
00238 try:
00239 tablecount = self.__query(sql)
00240 if tablecount[0][0] < 3:
00241 print("Some tables in the database seems to be missing")
00242 print ("Marking the database as corrupt")
00243 self.database_ok = False
00244 except Exception:
00245 self.database_ok = False
00246
00247 if self.database_ok:
00248 try:
00249
00250 self.tracks = self.__query("SELECT COUNT (*) FROM tracks WHERE 1")[0][0]
00251
00252 self.albums = self.__query("SELECT COUNT (*) FROM albums WHERE 1")[0][0]
00253
00254 self.artists = self.__query("SELECT COUNT (*) FROM artists WHERE 1")[0][0]
00255 except TypeError, inst:
00256 self.tracks = 0
00257 self.albums = 0
00258 self.artists = 0
00259
00260 self.database_ok = False
00261 desc = "\n\n+-------------------------------------------------------+\n"
00262 desc += "| A neccessary table wasn't found. |\n"
00263 desc += "| Pyjama will now download and convert the needed files |\n"
00264 desc += "| If any problems occur, run 'pyjama -u' |\n"
00265 desc += "+-------------------------------------------------------+"
00266
00267 print desc
00268
00269
00270 if self.tracks < 135000 or self.albums < 19000 or self.artists < 10000:
00271 print ("There seem to be a lot of entries missing in the database")
00272 print ("Marking the database as corrupt")
00273 self.database_ok = False
00274
00275
00276
00277
00278
00279
00280
00281
00282 def open(self):
00283 self.connection = sqlite3.connect(self.db)
00284
00285
00286
00287
00288 def close(self):
00289 self.connection.close()
00290
00291
00292
00293
00294
00295 def query(self, query):
00296 if not self.parent.window.check_alldone():
00297 self.parent.Events.raise_event("error", None, "You are not allowed to query the database before the main-module was loaded succesfully. If you are writing a plugin, please connect to the 'alldone' event.")
00298 return -2
00299
00300 self.query_counter += 1
00301
00302 try:
00303 self.open()
00304 cur = self.connection.cursor()
00305 cur.execute(query)
00306 rows = cur.fetchall()
00307
00308 self.close()
00309
00310 return rows
00311 except Exception, inst:
00312 desc = "There was an error while querying the database"
00313 self.parent.Events.raise_event("error", inst, desc)
00314 return -1
00315
00316
00317
00318 def __query(self, query):
00319 self.query_counter += 1
00320
00321 try:
00322 self.open()
00323 cur = self.connection.cursor()
00324 cur.execute(query)
00325 rows = cur.fetchall()
00326
00327 self.close()
00328
00329 return rows
00330 except Exception, inst:
00331 desc = "There was an error while querying the database"
00332 self.parent.Events.raise_event("error", inst, desc)
00333 return -1
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 def artistinfos(self, id, thread_id = 0):
00376 sql = """
00377 SELECT
00378 artists.name, artists.country, artists.image, artists.url, artists.id, albums.name, albums.id, artists.albumcount
00379 FROM
00380 artists, albums
00381 WHERE
00382 artists.id=%s and albums.artist_id=artists.id
00383 """ % str(id)
00384 ret = self.query(sql)
00385 counter = 0
00386 artist = {}
00387 for info in ret:
00388 artist[counter] = {}
00389 artist[counter]['artist_name'] = info[0]
00390 artist[counter]['artist_country'] = info[1]
00391 artist[counter]['artist_image'] = info[2]
00392 artist[counter]['artist_url'] = info[3]
00393 artist[counter]['artist_id'] = info[4]
00394 artist[counter]['album_name'] = info[5]
00395 artist[counter]['album_id'] = info[6]
00396 artist[counter]['artist_albumcount'] = info[7]
00397 counter += 1
00398 self.results[thread_id]=artist
00399 return artist
00400
00401 def multiple_albuminfos(self, ids):
00402 album_string = "("
00403 for album in ids:
00404 album_string += " albums.id=%s or" % album
00405 album_string = album_string[:-3] + ") and albums.artist_id=artists.id"
00406
00407 sql = """
00408 SELECT
00409 albums.id, albums.name, artists.id, artists.name
00410 FROM
00411 albums, artists
00412 WHERE
00413 %s
00414 """ % album_string
00415
00416 ret = self.query(sql)
00417 return ret
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 def get_multiple_trackinfos(self, tracks):
00461 track_string = "("
00462 for track in tracks:
00463 track_string += " tracks.id=%s or" % track
00464 track_string = track_string[:-3] + ") and tracks.artist_id=artists.id and albums.id=tracks.album_id"
00465 sql = """
00466 SELECT
00467 tracks.name, tracks.duration, tracks.numalbum, tracks.license, tracks.album_id, tracks.artist_id, artists.name, albums.name, tracks.uid, tracks.id3genre, tracks.id
00468 FROM
00469 tracks, artists, albums
00470 WHERE
00471 %s
00472 """ % track_string
00473
00474 ret = self.query(sql)
00475
00476 tracks=[]
00477 for track in ret:
00478 tr = Track()
00479 tr.name = track[0]
00480 tr.duration = track[1]
00481 tr.numalbum = track[2]
00482 tr.license = track[3]
00483 tr.album_id = track[4]
00484 tr.artist_id = track[5]
00485 tr.artist_name = track[6]
00486 tr.album_name = track[7]
00487 tr.uid = track[8]
00488 tr.id3genre = track[9]
00489 tr.id = track[10]
00490 tr.stream = "http://api.jamendo.com/get2/stream/track/redirect/?id=%i&streamencoding=%s" % (track[10], self.parent.settings.get_value("JAMENDO", "format_stream", "mp31"))
00491 tracks.append(tr)
00492 return tracks
00493
00494
00495
00496
00497
00498 def get_trackinfos2(self, trackid):
00499 sql = """
00500 SELECT
00501 tracks.name, tracks.duration, tracks.numalbum, tracks.license, tracks.album_id, tracks.artist_id, artists.name, albums.name, tracks.uid, tracks.id3genre, tracks.id
00502 FROM
00503 tracks, albums, artists
00504 WHERE
00505 tracks.id=%s and albums.id=tracks.album_id and artists.id=tracks.artist_id
00506 """ % str(trackid)
00507 ret = self.query(sql)
00508 if ret == []:
00509 return []
00510 ret = ret[0]
00511 tr = Track()
00512 tr.name = ret[0]
00513 tr.duration = ret[1]
00514 tr.numalbum = ret[2]
00515 tr.license = ret[3]
00516 tr.album_id = ret[4]
00517 tr.artist_id = ret[5]
00518 tr.artist_name = ret[6]
00519 tr.album_name = ret[7]
00520 tr.uid = ret[8]
00521 tr.id3genre = ret[9]
00522 tr.id = ret[10]
00523 tr.stream = "http://api.jamendo.com/get2/stream/track/redirect/?id=%i&streamencoding=%s" % (ret[10], self.parent.settings.get_value("JAMENDO", "format_stream", "mp31"))
00524
00525 return tr
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 def artisttracks(self, id):
00536 sql = """
00537 SELECT
00538 tracks.name, tracks.duration, tracks.numalbum, tracks.license, tracks.album_id, tracks.artist_id, artists.name, albums.name, tracks.uid, tracks.id3genre, tracks.id
00539 FROM
00540 tracks, albums, artists
00541 WHERE
00542 tracks.album_id=albums.id and tracks.artist_id=%s and albums.artist_id=%s and artists.id=tracks.artist_id
00543 """ % (str(id), str(id))
00544 ret = self.query(sql)
00545 tracks=[]
00546 for track in ret:
00547 tr = Track()
00548 tr.name = track[0]
00549 tr.duration = track[1]
00550 tr.numalbum = track[2]
00551 tr.license = track[3]
00552 tr.album_id = track[4]
00553 tr.artist_id = track[5]
00554 tr.artist_name = track[6]
00555 tr.album_name = track[7]
00556 tr.uid = track[8]
00557 tr.id3genre = track[9]
00558 tr.id = track[10]
00559 tr.stream = "http://api.jamendo.com/get2/stream/track/redirect/?id=%i&streamencoding=%s" % (track[10], self.parent.settings.get_value("JAMENDO", "format_stream", "mp31"))
00560 tracks.append(tr)
00561 return tracks
00562
00563
00564
00565
00566
00567 def albumtracks(self, id):
00568 sql = """
00569 SELECT
00570 tracks.name, tracks.duration, tracks.numalbum, tracks.license, tracks.album_id, tracks.artist_id, artists.name, albums.name, tracks.uid, tracks.id3genre, tracks.id
00571 FROM
00572 tracks, albums, artists
00573 WHERE
00574 tracks.album_id=%s and albums.id=%s and artists.id=albums.artist_id
00575 """ % (str(id), str(id))
00576 ret = self.query(sql)
00577 tracks=[]
00578 for track in ret:
00579 tr = Track()
00580 tr.name = track[0]
00581 tr.duration = track[1]
00582 tr.numalbum = track[2]
00583 tr.license = track[3]
00584 tr.album_id = track[4]
00585 tr.artist_id = track[5]
00586 tr.artist_name = track[6]
00587 tr.album_name = track[7]
00588 tr.uid = track[8]
00589 tr.id3genre = track[9]
00590 tr.id = track[10]
00591 tr.stream = "http://api.jamendo.com/get2/stream/track/redirect/?id=%i&streamencoding=%s" % (track[10], self.parent.settings.get_value("JAMENDO", "format_stream", "mp31"))
00592 tracks.append(tr)
00593 return tracks
00594
00595
00596
00597
00598
00599 def Search_tag(self, string):
00600
00601 sql = """
00602 SELECT
00603 albums.name, albums.id, artists.name, artists.id
00604 FROM
00605 albums, artists, tags
00606 WHERE
00607 tags.idstr='""" + string + """' and albums.id=tags.album_id LIMIT """ + str(self.parent.settings.get_value("PERFORMANCE", "db_search_limit"))
00608 ret = self.query(sql)
00609 tags = {}
00610 counter = 0
00611 for tag in ret:
00612 tags[counter] = {}
00613 tags[counter]['album_name'] = tag[0]
00614 tags[counter]['album_id'] = tag[1]
00615 tags[counter]['artist_name'] = tag[2]
00616 tags[counter]['artist_id'] = tag[3]
00617 counter += 1
00618 print tags
00619 return tags
00620
00621
00622
00623
00624
00625
00626 def search_track(self, string):
00627
00628 sql = """
00629 SELECT
00630 tracks.name, tracks.duration, tracks.numalbum, tracks.id, tracks.license, tracks.id3genre, albums.id, albums.name, artists.id, artists.name, tracks.uid
00631 FROM
00632 tracks, albums, artists
00633 WHERE
00634 tracks.name LIKE '%""" + string + """%' and tracks.album_id=albums.id and albums.artist_id=artists.id LIMIT """ + str(self.parent.settings.get_value("PERFORMANCE", "db_search_limit"))
00635 ret = self.query(sql)
00636 tracks=[]
00637 for track in ret:
00638 tr = Track()
00639 tr.name = track[0]
00640 tr.duration = track[1]
00641 tr.numalbum = track[2]
00642 tr.id = track[3]
00643 tr.license = track[4]
00644 tr.id3genre = track[5]
00645 tr.album_id = track[6]
00646 tr.album_name = track[7]
00647 tr.artist_id = track[8]
00648 tr.artist_name = track[9]
00649 tr.uid = track[10]
00650 tr.stream = "http://api.jamendo.com/get2/stream/track/redirect/?id=%i&streamencoding=%s" % (track[3], self.parent.settings.get_value("JAMENDO", "format_stream", "mp31"))
00651 tracks.append(tr)
00652 return tracks
00653
00654
00655
00656
00657
00658 def search_artist(self, string):
00659 sql = """
00660 SELECT
00661 artists.name, artists.id
00662 FROM
00663 artists
00664 WHERE
00665 artists.name LIKE '%""" + string + """%' LIMIT """ + str(self.parent.settings.get_value("PERFORMANCE", "db_search_limit"))
00666 ret = self.query(sql)
00667 artists = {}
00668 counter = 0
00669 for artist in ret:
00670 artists[counter] = {}
00671 artists[counter]['artist_name'] = artist[0]
00672 artists[counter]['artist_id'] = artist[1]
00673
00674
00675 counter += 1
00676 return artists
00677
00678
00679
00680
00681
00682 def search_album(self, string):
00683
00684 sql = """
00685 SELECT
00686 albums.name, albums.id, artists.name, artists.id
00687 FROM
00688 albums, artists
00689 WHERE
00690 albums.name LIKE '%""" + string + """%' and albums.artist_id=artists.id LIMIT """ + str(self.parent.settings.get_value("PERFORMANCE", "db_search_limit"))
00691 ret = self.query(sql)
00692 albums = {}
00693 counter = 0
00694 for album in ret:
00695 albums[counter] = {}
00696 albums[counter]['album_name'] = album[0]
00697 albums[counter]['album_id'] = album[1]
00698 albums[counter]['artist_name'] = album[2]
00699 albums[counter]['artist_id'] = album[3]
00700 counter += 1
00701 return albums
00702
00703
00704
00705
00706
00707 def gettags(self, id):
00708
00709 sql = """
00710 SELECT
00711 tags
00712 FROM
00713 albums
00714 WHERE
00715 id=%s
00716 """ % str(id)
00717 ret = self.query(sql)
00718 return ret
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745