Skip to content

Commit

Permalink
implemented cache database
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanparai committed Nov 13, 2015
1 parent a71dff2 commit ff53414
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 31 deletions.
45 changes: 33 additions & 12 deletions addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ def showMoviesList(page=1):

url = '{0}?action=play&video={1}'.format(__url__, movie.url)

li = xbmcgui.ListItem(label=movie.title, thumbnailImage=movie.posterImage)

bPosterImage = "https://image.tmdb.org/t/p/w396" + movie.posterImage
bBackdropImage = "https://image.tmdb.org/t/p/w780" + movie.backdropImage

li.setArt({ 'poster': movie.posterImage, 'fanart' : movie.backdropImage, 'thumb' : movie.posterImage })
sPosterImage = "https://image.tmdb.org/t/p/w185" + movie.posterImage

print "sPosterImage: " + sPosterImage

li = xbmcgui.ListItem(label=movie.title, thumbnailImage=sPosterImage)

li.setArt({ 'poster': bPosterImage, 'fanart' : bBackdropImage, 'thumb' : sPosterImage })

info = {
'genre': movie.genres,
Expand All @@ -139,7 +146,7 @@ def showMoviesList(page=1):

# add contex menu
cm =[]
msg = 'RunPlugin({0}?action=addToLibrary&title={1}&year={2}&url={3})'.format(__url__, urllib.quote_plus(movie.title), movie.year, movie.url)
msg = 'RunPlugin({0}?action=addToLibrary&imdbid={1}&title={2}&year={3}&url={4})'.format(__url__, movie.imdbid, urllib.quote_plus(movie.title), movie.year, movie.url)
cm.append(('Add To Library', msg))
li.addContextMenuItems(cm, False)

Expand Down Expand Up @@ -190,28 +197,31 @@ def router(paramstring):

elif params['action'] == 'playStream':
print "all params : " + params['title']
play_stream(params['title'], params['year'], params['video'])

if params.has_key('imdbid'):
play_stream(params['title'], params['year'], params['video'], params['imdbid'])
else:
play_stream(params['title'], params['year'], params['video'])

elif params['action']=='recentMoviesWithPage':
showMoviesList(params['page'])

elif params['action'] == 'addToLibrary':
print "plugin.video.cinehub: " + params['title']
addMovieToLibrary(params['title'], params['year'], params['url'])
addMovieToLibrary(params['title'], params['year'], params['url'], params['imdbid'])

else:

showCatagoryList()

def addMovieToLibrary(title, year , url):
def addMovieToLibrary(title, year , url, imdbid):

# get movie folder location
#
# os.path.join is for translating between different OS path structure
# xbmc.translatePath() is used for translating "speciall://sample" path to full path for writing to disk
# xbmcaddon.Addon().getSetting('id') is for reading movie libray info from setting
library_folder = os.path.join(xbmc.translatePath(xbmcaddon.Addon().getSetting('movie_library')))
print "Movie library path: " + library_folder

# make movie directory if not already created
xbmcvfs.mkdir(library_folder)
Expand All @@ -230,7 +240,7 @@ def addMovieToLibrary(title, year , url):

file = xbmcvfs.File(movie_file, 'w')

content = '%s?action=playStream&title=%s&year=%s&video=%s' % (__url__, urllib.quote_plus(title), year, url)
content = '%s?action=playStream&imdbid=%s&title=%s&year=%s&video=%s' % (__url__, imdbid, urllib.quote_plus(title), year, url)

file.write(str(content))

Expand All @@ -247,7 +257,7 @@ def play_video(path):
# Pass the item to the Kodi player.
xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)

def play_stream(title, year, path):
def play_stream(title, year, path, imdbid=''):

# get movie info
mInfo = movieinfo()
Expand All @@ -256,7 +266,13 @@ def play_stream(title, year, path):
name = '%s (%s)' % (title, str(year))

scrobber = tmdbscraper()
movie = scrobber.getMovieInfo(name)

print "imdbid from play_stream " + imdbid

if imdbid:
movie = scrobber.getMovieInfo(name, imdbid=imdbid)
else:
movie = scrobber.getMovieInfo(name)

info = {
'genre': movie.genres,
Expand All @@ -274,12 +290,17 @@ def play_stream(title, year, path):
'director' : movie.director,
'writer' : movie.writer
}
play_item = xbmcgui.ListItem(label=movie.title, thumbnailImage=movie.posterImage, path=path)

sPosterImage = "https://image.tmdb.org/t/p/w185" + movie.posterImage

print "sPosterImage url while playing: " + sPosterImage

play_item = xbmcgui.ListItem(label=movie.title, thumbnailImage=sPosterImage, path=path)

play_item.setInfo('video', info)


play_item.setArt({'thumb' : movie.posterImage })
play_item.setArt({'thumb' : sPosterImage })

# Pass the item to the Kodi player.
xbmcplugin.setResolvedUrl(addon_handle, True, listitem=play_item)
Expand Down
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="plugin.video.cinehub" name="Cinehub24" version="1.1.12" provider-name="Ratan Sunder Parai">
<addon id="plugin.video.cinehub" name="Cinehub24" version="1.2.0" provider-name="Ratan Sunder Parai">
<requires>
<import addon="xbmc.python" version="2.1.0" />
<import addon="script.module.requests" version="1.1.0"/>
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.2.0
- The addon is getting faster and responce time is getting lower :p
- implemented cache so that you can remember movie information in lightning speed. But don't try to break the sound barrier, you are not quite ready yet!

v1.1.12
- better movie recognition

Expand Down
103 changes: 99 additions & 4 deletions resources/lib/cinehub.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,62 @@
from tmdbscraper import tmdbscraper
import threading
import xbmcaddon
import os
import xbmc
import sqlite3
import sys

class cinehub:

# movie list
movieList = []

# path to database
dburl = urldb = os.path.join(xbmc.translatePath("special://userdata/addon_data/plugin.video.cinehub/cinehubdb.db"))

def __init__(self):

dbcon = sqlite3.connect(self.dburl)

# for dic output
# dbcon.row_factory = sqlite3.Row

# get cursor
dbcur = dbcon.cursor()

# create database if not already created
try:
#create link_url table
dbcur.execute("CREATE TABLE url_matcher (link_url text , video_url text)")

#create movie_info table
dbcur.execute('''CREATE TABLE movie_info
(
url text,
title text,
imdbid text,
genres text,
rating real,
runtime integer,
tagline text,
totalVote integer,
releaseDate text,
overview text,
posterImage text,
backdropImage text,
year integer,
writer text,
director text,
castandrole text
)''')

# commit change
dbcon.commit()
except:
pass

#close the database connection
dbcon.close()

# function to call the getMovieList with appropiate URL and page number
def getSearchedMovieList(self, movieName):
Expand Down Expand Up @@ -89,15 +140,24 @@ def getMovieList(self, url):
return self.movieList

def worker(self, name, url, session):
resolvedUrl = self.getVideoLink(session, url)
# get cursor
dbcon = sqlite3.connect(self.dburl)
dbcur = dbcon.cursor()

resolvedUrl = self.matchUrl(session, url, dbcon, dbcur)

# close the connection
dbcon.close()

# if resolvedUrl is none then stop the thread and do nothing
if resolvedUrl == None:
if resolvedUrl == None or resolvedUrl == "":
return

# save movie info into database

# now try to get meta info from tmdb database
scrapper = tmdbscraper()
mMovieInfo = scrapper.getMovieInfo(name)
mMovieInfo = scrapper.getMovieInfo(name, videoUrl=resolvedUrl)

# add previously resolved url to the list
mMovieInfo.url = resolvedUrl
Expand All @@ -106,9 +166,28 @@ def worker(self, name, url, session):
# add the movieino object to the movie list
self.movieList.append(mMovieInfo)

def matchUrl(self, session, url, dbcon, dbcur):
# check if the url is already being parsed
t = (url,)
dbcur.execute("SELECT * FROM url_matcher WHERE link_url = ? " , t)

result = dbcur.fetchone()


# if we found it
if result :
# close the connection
dbcon.close()
# return the parsed video url
print "found in databse: " + url
return result[1]

# if not found in database fetch the link
print "Not found in databae. starting query for url"
return self.getVideoLink(session, url, dbcur, dbcon)
# get playable video link for the movie if available
# if no link is available then return nothing
def getVideoLink(self, session, URL):
def getVideoLink(self, session, URL, dbcur, dbcon):

# get html file
result = session.get(URL).content
Expand All @@ -134,11 +213,26 @@ def getVideoLink(self, session, URL):
if link.lower().find("sample") != -1 or link.lower().find("trailer") != -1 :
pass
else:
print "inserting into database"
t = (URL, link)
dbcur.execute("INSERT INTO url_matcher VALUES (?, ?)", t)
dbcon.commit()
dbcon.close()
print "insert successful"
print "Link is : " + link
return link
else:
tdId += 5
except sqlite3.OperationalError:
raise

except:
break
# if no link found insert empty link to database
t = (URL, '')
dbcur.execute("INSERT INTO url_matcher VALUES (?, ?)", t)
dbcon.commit()
dbcon.close()

# Login to the cinehub24 server
def doLogin(self):
Expand Down Expand Up @@ -171,5 +265,6 @@ def doLogin(self):
# TODO

return session_requests



Loading

0 comments on commit ff53414

Please sign in to comment.