def canonalize_path(path): """Convert a win32 path to unix path.""" if os.path.sep == "/": return path head, tail = ntsplit(path) lst = [tail] while head and tail: head, tail = ntsplit(head) lst.insert(0, tail) lst.insert(0, head) return posjoin(*lst)
def load_sentences(text_file, stopwords, lang): path, f = ntsplit(text_file) reader = PlaintextCorpusReader(path, f) sentences = [sent for sent in reader.sents()] clean = [] originalSentenceOf = {} if lang == "fr": stemmer = FrenchStemmer() elif lang == "en": stemmer = SnowballStemmer("english") # Data cleansing for sent in sentences: s = stemmize(stemmer, sent, stopwords) clean.append(" ".join(s)) originalSentenceOf[clean[-1]] = sent setClean = set(clean) return setClean, originalSentenceOf, sentences, clean
def addOrUpdateSongToKodiLibrary(self, MBitem, connection, cursor): kodiVersion = self.kodiversion embyId = MBitem["Id"] # If the item already exist in the local Kodi DB we'll perform a full item update # If the item doesn't exist, we'll add it to the database cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (embyId,)) try: songid = cursor.fetchone()[0] except: songid = None timeInfo = API().getTimeInfo(MBitem) userData = API().getUserData(MBitem) genres = MBitem.get('Genres') ##### The song details ##### playcount = userData.get('PlayCount') lastplayed = userData.get('LastPlayedDate') dateadded = API().getDateCreated(MBitem) checksum = API().getChecksum(MBitem) name = MBitem['Name'] musicBrainzId = API().getProvider(MBitem, "musicBrainzTrackId") genre = " / ".join(genres) artists = " / ".join(MBitem.get('Artists')) tracknumber = MBitem.get('IndexNumber', 0) disc = MBitem.get('ParentIndexNumber', 1) track = disc*2**16 + tracknumber year = MBitem.get('ProductionYear') bio = API().getOverview(MBitem) duration = timeInfo.get('TotalTime') if utils.settings('directstreammusic') == "true": WINDOW = xbmcgui.Window(10000) username = WINDOW.getProperty('currUser') server = WINDOW.getProperty('server%s' % username) playurl = PlayUtils().directStream(MBitem, server, embyId, "Audio") filename = "stream.mp3" path = playurl.replace(filename, "") else: # Get the path and filename playurl = PlayUtils().directPlay(MBitem) path, filename = ntsplit(playurl) if "/" in playurl: path = "%s/" % path elif "\\" in playurl: path = "%s\\" % path # Validate the path in database cursor.execute("SELECT idPath as pathid FROM path WHERE strPath = ?", (path,)) try: pathid = cursor.fetchone()[0] except: cursor.execute("select coalesce(max(idPath),0) as pathid from path") pathid = cursor.fetchone()[0] + 1 query = "INSERT INTO path(idPath, strPath) values(?, ?)" cursor.execute(query, (pathid, path)) # Get the album cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (MBitem.get("AlbumId"),)) try: albumid = cursor.fetchone()[0] except: # No album found, create a single's album cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") albumid = cursor.fetchone()[0] + 1 if kodiVersion == 15: # Kodi Isengard query = "INSERT INTO album(idAlbum, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?)" cursor.execute(query, (albumid, genre, year, dateadded, "single")) elif kodiVersion == 16: query = "INSERT INTO album(idAlbum, strGenres, iYear, strReleaseType) values(?, ?, ?, ?)" cursor.execute(query, (albumid, genre, year, "single")) else: # Kodi Gotham and Helix query = "INSERT INTO album(idAlbum, strGenres, iYear, dateAdded) values(?, ?, ?, ?)" cursor.execute(query, (albumid, genre, year, dateadded)) finally: cursor.execute("SELECT strArtists FROM album WHERE idAlbum = ?", (albumid,)) result = cursor.fetchone() if result and result[0] == "": # Link album to artists if MBitem['AlbumArtists']: album_artists = MBitem['AlbumArtists'] else: album_artists = MBitem['ArtistItems'] MBartists = [] for artist in album_artists: MBartists.append(artist['Name']) cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) try: artistid = cursor.fetchone()[0] except: pass else: query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" cursor.execute(query, (artistid, albumid, artist['Name'])) artists_onalbum = " / ".join(MBartists) if kodiVersion == 15: # Kodi Isengard query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?" cursor.execute(query, (artists_onalbum, albumid)) elif kodiVersion == 16: query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?" cursor.execute(query, (artists_onalbum, albumid)) else: # Kodi Gotham and Helix query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?" cursor.execute(query, (artists_onalbum, albumid)) ##### UPDATE THE SONG ##### if songid: self.logMsg("UPDATE song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) query = "UPDATE song SET idAlbum = ?, strArtists = ?, strGenres = ?, strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?, strFilename = ?, strMusicBrainzTrackID = ?, iTimesPlayed = ?, lastplayed = ? WHERE idSong = ?" cursor.execute(query, (albumid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed, songid)) # Update the checksum in emby table query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" cursor.execute(query, (checksum, embyId)) ##### OR ADD THE SONG ##### else: self.logMsg("ADD song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) # Create the song cursor.execute("select coalesce(max(idSong),0) as songid from song") songid = cursor.fetchone()[0] + 1 query = "INSERT INTO song(idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack, iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" cursor.execute(query, (songid, albumid, pathid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed)) # Create the reference in emby table query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" cursor.execute(query, (embyId, songid, "song", checksum)) # Add genres self.AddGenresToMedia(songid, genres, "song", cursor) # Link song to album if albumid: query = "INSERT OR REPLACE INTO albuminfosong(idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration) values(?, ?, ?, ?, ?)" cursor.execute(query, (songid, albumid, track, name, duration)) # Link song to artist for artist in MBitem.get('ArtistItems'): cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) try: artistid = cursor.fetchone()[0] except: pass else: query = "INSERT OR REPLACE INTO song_artist(idArtist, idSong, strArtist) values(?, ?, ?)" cursor.execute(query, (artistid, songid, artist['Name'])) # Update artwork self.textureCache.addArtwork(API().getAllArtwork(MBitem, parentInfo=True), songid, "song", cursor)
def addOrUpdateSongToKodiLibrary(self, MBitem, connection, cursor): kodiVersion = self.kodiversion embyId = MBitem["Id"] # If the item already exist in the local Kodi DB we'll perform a full item update # If the item doesn't exist, we'll add it to the database cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (embyId,)) try: songid = cursor.fetchone()[0] except: songid = None timeInfo = API().getTimeInfo(MBitem) userData = API().getUserData(MBitem) genres = MBitem.get('Genres') ##### The song details ##### playcount = userData.get('PlayCount') lastplayed = userData.get('LastPlayedDate') dateadded = API().getDateCreated(MBitem) checksum = API().getChecksum(MBitem) name = MBitem['Name'] musicBrainzId = API().getProvider(MBitem, "musicBrainzTrackId") genre = " / ".join(genres) artists = " / ".join(MBitem.get('Artists')) track = MBitem.get('IndexNumber') year = MBitem.get('ProductionYear') bio = API().getOverview(MBitem) duration = timeInfo.get('TotalTime') if utils.settings('directstreammusic') == "true": WINDOW = xbmcgui.Window(10000) username = WINDOW.getProperty('currUser') server = WINDOW.getProperty('server%s' % username) playurl = PlayUtils().directStream(MBitem, server, embyId, "Audio") filename = "stream.mp3" path = playurl.replace(filename, "") else: # Get the path and filename playurl = PlayUtils().directPlay(MBitem) path, filename = ntsplit(playurl) if "/" in playurl: path = "%s/" % path elif "\\" in playurl: path = "%s\\" % path # Validate the path in database cursor.execute("SELECT idPath as pathid FROM path WHERE strPath = ?", (path,)) try: pathid = cursor.fetchone()[0] except: cursor.execute("select coalesce(max(idPath),0) as pathid from path") pathid = cursor.fetchone()[0] + 1 query = "INSERT INTO path(idPath, strPath) values(?, ?)" cursor.execute(query, (pathid, path)) # Get the album cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (MBitem.get("AlbumId"),)) try: albumid = cursor.fetchone()[0] except: # No album found, create a single's album cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") albumid = cursor.fetchone()[0] + 1 if kodiVersion == 15: # Kodi Isengard query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?)" cursor.execute(query, (albumid, artists, genre, year, dateadded, "single")) elif kodiVersion == 16: query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, strReleaseType) values(?, ?, ?, ?, ?)" cursor.execute(query, (albumid, artists, genre, year, "single")) else: # Kodi Gotham and Helix query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded) values(?, ?, ?, ?, ?)" cursor.execute(query, (albumid, artists, genre, year, dateadded)) # Link album to artists for artist in MBitem['ArtistItems']: cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) try: artistid = cursor.fetchone()[0] except: pass else: query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" cursor.execute(query, (artistid, albumid, artist['Name'])) ##### UPDATE THE SONG ##### if songid: self.logMsg("UPDATE song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) query = "UPDATE song SET idAlbum = ?, strArtists = ?, strGenres = ?, strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?, strFilename = ?, strMusicBrainzTrackID = ?, iTimesPlayed = ?, lastplayed = ? WHERE idSong = ?" cursor.execute(query, (albumid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed, songid)) # Update the checksum in emby table query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" cursor.execute(query, (checksum, embyId)) ##### OR ADD THE SONG ##### else: self.logMsg("ADD song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) # Create the song cursor.execute("select coalesce(max(idSong),0) as songid from song") songid = cursor.fetchone()[0] + 1 query = "INSERT INTO song(idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack, iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" cursor.execute(query, (songid, albumid, pathid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed)) # Create the reference in emby table query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" cursor.execute(query, (embyId, songid, "song", checksum)) # Add genres self.AddGenresToMedia(songid, genres, "song", cursor) # Link song to album if albumid: query = "INSERT OR REPLACE INTO albuminfosong(idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration) values(?, ?, ?, ?, ?)" cursor.execute(query, (songid, albumid, track, name, duration)) # Link song to artist for artist in MBitem.get('ArtistItems'): cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) try: artistid = cursor.fetchone()[0] except: pass else: query = "INSERT OR REPLACE INTO song_artist(idArtist, idSong, strArtist) values(?, ?, ?)" cursor.execute(query, (artistid, songid, artist['Name'])) # Update artwork self.textureCache.addArtwork(API().getAllArtwork(MBitem), songid, "song", cursor)
def path_leaf(self, path): head, tail = ntsplit(path) return os.path.splitext(tail)[0] or os.path.splitext(ntbasename(head))[0]
def addOrUpdateSongToKodiLibrary(self, MBitem, connection, cursor): kodiVersion = self.kodiversion embyId = MBitem["Id"] # If the item already exist in the local Kodi DB we'll perform a full item update # If the item doesn't exist, we'll add it to the database cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (embyId,)) try: songid = cursor.fetchone()[0] except: songid = None timeInfo = API().getTimeInfo(MBitem) userData = API().getUserData(MBitem) genres = MBitem.get('Genres') ##### The song details ##### playcount = userData.get('PlayCount') lastplayed = userData.get('LastPlayedDate') dateadded = API().getDateCreated(MBitem) checksum = API().getChecksum(MBitem) name = MBitem['Name'] musicBrainzId = API().getProvider(MBitem, "musicBrainzTrackId") genre = " / ".join(genres) artists = " / ".join(MBitem.get('Artists')) track = MBitem.get('IndexNumber') year = MBitem.get('ProductionYear') bio = API().getOverview(MBitem) duration = timeInfo.get('TotalTime') # Get the path and filename playurl = PlayUtils().directPlay(MBitem) try: path, filename = ntsplit(playurl) if "/" in playurl: path = "%s/" % path elif "\\" in playurl: path = "%s\\" % path except: # playurl returned false - using server streaming path, because could not figure out plugin paths for music DB playurl = PlayUtils().directstream(MBitem, self.server, embyId, "Audio") filename = "stream.mp3" path = playurl.replace(filename, "") # Validate the path in database cursor.execute("SELECT idPath as pathid FROM path WHERE strPath = ?", (path,)) try: pathid = cursor.fetchone()[0] except: cursor.execute("select coalesce(max(idPath),0) as pathid from path") pathid = cursor.fetchone()[0] + 1 query = "INSERT INTO path(idPath, strPath) values(?, ?)" cursor.execute(query, (pathid, path)) # Get the album cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (MBitem.get("AlbumId"),)) try: albumid = cursor.fetchone()[0] except: # No album found, create a single's album cursor.execute("select coalesce(max(idAlbum),0) as albumid from album") albumid = cursor.fetchone()[0] + 1 if kodiVersion == 15 or kodiVersion == 16: # Kodi Isengard query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?)" cursor.execute(query, (albumid, artists, genre, year, dateadded, "single")) else: # Kodi Gotham and Helix query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded) values(?, ?, ?, ?, ?)" cursor.execute(query, (albumid, artists, genre, year, dateadded)) # Link album to artists for artist in MBitem['ArtistItems']: cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) try: artistid = cursor.fetchone()[0] except: pass else: query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)" cursor.execute(query, (artistid, albumid, artist['Name'])) ##### UPDATE THE SONG ##### if songid: self.logMsg("UPDATE song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) query = "UPDATE song SET idAlbum = ?, strArtists = ?, strGenres = ?, strTitle = ?, iTrack = ?, iDuration = ?, iYear = ?, strFilename = ?, strMusicBrainzTrackID = ?, iTimesPlayed = ?, lastplayed = ? WHERE idSong = ?" cursor.execute(query, (albumid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed, songid)) # Update the checksum in emby table query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" cursor.execute(query, (checksum, embyId)) ##### OR ADD THE SONG ##### else: self.logMsg("ADD song to Kodi library, Id: %s - Title: %s" % (embyId, name), 1) # Create the song cursor.execute("select coalesce(max(idSong),0) as songid from song") songid = cursor.fetchone()[0] + 1 query = "INSERT INTO song(idSong, idAlbum, idPath, strArtists, strGenres, strTitle, iTrack, iDuration, iYear, strFileName, strMusicBrainzTrackID, iTimesPlayed, lastplayed) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" cursor.execute(query, (songid, albumid, pathid, artists, genre, name, track, duration, year, filename, musicBrainzId, playcount, lastplayed)) # Create the reference in emby table query = "INSERT INTO emby(emby_id, kodi_id, media_type, checksum) values(?, ?, ?, ?)" cursor.execute(query, (embyId, songid, "song", checksum)) # Add genres self.AddGenresToMedia(songid, genres, "song", cursor) # Link song to album if albumid: query = "INSERT OR REPLACE INTO albuminfosong(idAlbumInfoSong, idAlbumInfo, iTrack, strTitle, iDuration) values(?, ?, ?, ?, ?)" cursor.execute(query, (songid, albumid, track, name, duration)) # Link song to artist for artist in MBitem.get('ArtistItems'): cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],)) try: artistid = cursor.fetchone()[0] except: pass else: query = "INSERT OR REPLACE INTO song_artist(idArtist, idSong, strArtist) values(?, ?, ?)" cursor.execute(query, (artistid, songid, artist['Name'])) # Update artwork self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), songid, "song", "thumb", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Primary"), songid, "song", "poster", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Banner"), songid, "song", "banner", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Logo"), songid, "song", "clearlogo", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Art"), songid, "song", "clearart", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Thumb"), songid, "song", "landscape", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Disc"), songid, "song", "discart", cursor) self.addOrUpdateArt(API().getArtwork(MBitem, "Backdrop"), songid, "song", "fanart", cursor)