forked from liberty-developer/plugin.video.metalliq-forqed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
executable file
·229 lines (206 loc) · 9.8 KB
/
service.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
# -*- coding: utf-8 -*-
if __name__ == '__main__':
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'resources', 'lib'))
import datetime
import glob
import re
import sqlite3
import xbmcgui
import xbmc
from xbmcswift2 import xbmc, xbmcvfs
from meta import plugin
from meta.play.players import patch
from meta.video_player import VideoPlayer
from meta.utils.properties import get_property, clear_property
from lastfm import lastfm
from default import update_library
from settings import SETTING_UPDATE_LIBRARY_INTERVAL, SETTING_MUSIC_LIBRARY_FOLDER, SETTING_TOTAL_SETUP_DONE, SETTING_AUTOPATCH
from language import get_string as _
player = VideoPlayer()
class Monitor(xbmc.Monitor):
def onDatabaseUpdated(self, database):
if database == "video":
if get_property("clean_library"):
xbmc.executebuiltin("CleanLibrary(video)")
clear_property("clean_library")
if database == "music":
# need to manualy change the database file to add strm files to it
music_directory = plugin.get_setting(SETTING_MUSIC_LIBRARY_FOLDER, unicode)
self.add_folder_to_music_database(music_directory)
def get_pathId(self,dirName, alternative = False):
if not alternative:
absDirName = os.path.abspath(dirName) + os.sep
absDirName = absDirName.replace('kodi', 'Kodi')
else:
absDirName = dirName
c.execute("SELECT * FROM path WHERE UPPER(strPath) = UPPER(?)", (absDirName,))
row = c.fetchone()
if row:
return row[0]
else:
return None
def get_albumId(self,album, artist):
c.execute("SELECT * FROM album WHERE strAlbum = ? AND strArtists = ?", (album, artist))
row = c.fetchone()
if row:
return row[0]
else:
album_info = lastfm.get_album_info(artist, album)
if "wiki" in album_info:
review = album_info["wiki"]["content"]
else:
review = ""
c.execute("INSERT INTO album (strAlbum,strArtists,strReleaseType, strReview) VALUES (?,?,?,?)",
(album, artist, "album", review))
id = c.lastrowid
conn.commit()
return id
def get_artistId(self, artist):
c.execute("SELECT * FROM artist WHERE strArtist = ?", (artist,))
row = c.fetchone()
if row:
return row[0]
else:
artist_info = lastfm.get_artist_info(artist)
if "bio" in artist_info:
biography = artist_info["bio"]["content"]
else:
biography = ""
image = "<thumb preview={0}</thumb>".format(artist_info["image"][-1]["#text"])
c.execute("INSERT INTO artist (strArtist,strBiography, strImage) VALUES (?,?,?)",
(artist, biography, image))
id = c.lastrowid
conn.commit()
return id
def get_songId(self, albumId, pathId, artist, song, song_number, filename):
c.execute("SELECT * FROM song WHERE idAlbum = ? AND idPath = ? And strArtists = ? AND strTitle = ?",
(albumId, pathId, artist, song))
row = c.fetchone()
if row:
return row[0]
else:
c.execute("INSERT INTO song (idAlbum, idPath, strArtists, strTitle, itrack, strFilename) VALUES (?,?,?,?,?,?)",
(albumId, pathId, artist, song, song_number, filename))
id = c.lastrowid
conn.commit()
return id
def add_albumArtist(self, albumId, artistId, artist):
c.execute("SELECT * FROM album_artist WHERE idArtist = ? AND idAlbum = ?", (artistId, albumId))
row = c.fetchone()
if not row:
c.execute("INSERT INTO album_artist (idArtist, idAlbum, strArtist) VALUES (?,?,?)",
(artistId, albumId, artist))
def add_songArtist(self, songId, artistId, artist):
c.execute("SELECT * FROM song_artist WHERE idArtist = ? AND idSong = ?", (artistId, songId))
row = c.fetchone()
if not row:
c.execute("INSERT INTO song_artist (idArtist, idSong, strArtist) VALUES (?,?,?)",
(artistId, songId, artist))
def add_albumArt(self, albumId, dirName):
c.execute("SELECT * FROM art WHERE media_id = ? AND media_type = ?", (albumId, "album"))
row = c.fetchone()
if not row:
absDirName = os.path.abspath(dirName) + os.sep
absDirName = absDirName.replace('kodi', 'Kodi')
thumb = absDirName + "folder.jpg"
c.execute("INSERT INTO art (media_id, media_type,type, url) VALUES (?,?,?,?)",
(albumId, "album", "thumb", thumb))
def add_artistArt(self, artistId, dirName):
c.execute("SELECT * FROM art WHERE media_id = ? AND media_type = ?", (artistId, "artist"))
row = c.fetchone()
if not row:
absDirName = os.path.abspath(dirName) + os.sep
absDirName = absDirName.replace('kodi', 'Kodi')
thumb = absDirName + ".." + os.sep + "folder.jpg"
c.execute("INSERT INTO art (media_id, media_type,type, url) VALUES (?,?,?,?)",
(artistId, "artist", "thumb", thumb))
def add_folder_to_music_database(self, music_folder):
import re
info_regex = re.compile(r'.*?<title>(.*?)</title>.*?<artist>(.*?)</artist>.*?<album>(.*?)</album>.*?<track>(.*?)</track>.*',
re.IGNORECASE | re.UNICODE | re.DOTALL) # regex to parse nfo file
self.setup_database_connection()
abs_music_folder = xbmc.translatePath(music_folder) # translate from special:// to absolute
for dirName, subdirList, fileList in os.walk(abs_music_folder):
pathId = self.get_pathId(dirName)
if not pathId:
if music_folder[-1] != "/":
music_folder = music_folder + "/"
special_dirname = dirName.replace(abs_music_folder, music_folder)
if special_dirname[-1] != "/":
special_dirname = special_dirname + "/"
pathId = self.get_pathId(special_dirname, True) # check on android
if not pathId:
special_dirname = dirName.replace(abs_music_folder, music_folder).replace("profile", "userdata")
if special_dirname[-1] != "/":
special_dirname = special_dirname + "/"
pathId = self.get_pathId(special_dirname, True) # check on android
if not pathId:
xbmc.log("pathid not found")
continue
fname_index = 0
for fname in fileList:
if fname.endswith("strm"):
try:
nfo = fname.replace('.strm', '.nfo')
info_string = open(dirName+ os.sep + nfo,'r').read()
result = re.match(info_regex, info_string)
song = result.group(1)
artist = result.group(2)
album = result.group(3)
song_number = result.group(4)
artistId = self.get_artistId(artist)
self.add_artistArt(artistId, dirName)
albumId = self.get_albumId(album, artist)
self.add_albumArtist(albumId, artistId, artist)
self.add_albumArt(albumId, dirName)
songId = self.get_songId(albumId, pathId, artist, song, song_number, fname)
self.add_songArtist(songId, artistId, artist)
finally:
conn.commit()
fname_index += 1
def setup_database_connection(self):
global c
global conn
# set up sqlite connection
path = xbmc.translatePath('special://home/userdata/Database')
files = glob.glob(os.path.join(path, 'MyMusic*.db'))
ver = 0
dbPath = ''
# Find the highest version number of textures, it's always been textures13.db but you can never be too careful!
for file in files:
dbversion = int(re.compile('MyMusic(.+?).db').findall(file)[0])
if ver < dbversion:
ver = dbversion
dbPath = file
db = xbmc.translatePath(dbPath)
conn = sqlite3.connect(db, timeout=10, detect_types=sqlite3.PARSE_DECLTYPES, check_same_thread=False)
conn.text_factory = str
c = conn.cursor()
c.row_factory = sqlite3.Row
monitor = Monitor()
def go_idle(duration):
while not xbmc.abortRequested and duration > 0:
if player.isPlayingVideo():
player.currentTime = player.getTime()
xbmc.sleep(1000)
duration -= 1
def future(seconds):
return datetime.datetime.now() + datetime.timedelta(seconds=seconds)
def main():
go_idle(15)
if plugin.get_setting(SETTING_TOTAL_SETUP_DONE, bool) == False:
xbmc.executebuiltin('RunPlugin(plugin://plugin.video.metalliq-forqed/setup/total)')
plugin.set_setting(SETTING_TOTAL_SETUP_DONE, "true")
if plugin.get_setting(SETTING_AUTOPATCH, bool) == True:
patch("auto")
xbmc.executebuiltin("RunPlugin(plugin://plugin.video.metalliq-forqed/movies/batch_add_to_library)")
next_update = future(0)
while not xbmc.abortRequested:
if next_update <= future(0):
next_update = future(plugin.get_setting(SETTING_UPDATE_LIBRARY_INTERVAL, int) * 60 * 60)
update_library()
go_idle(30*60)
if __name__ == '__main__':
main()