Skip to content

Commit

Permalink
Redirector option for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-alpha committed Feb 20, 2017
1 parent 068ca95 commit 210ad46
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 71 deletions.
103 changes: 65 additions & 38 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ICON_SIMILAR = "icon-similar.png"
ICON_OTHERSEASONS = "icon-otherseasons.png"
ICON_HOT = "icon-hot.png"
ICON_ENTER = "icon-enter.png"
ICON_QUEUE = "icon-bookmark.png"
ICON_UNAV = "MoviePosterUnavailable.jpg"
ICON_PREFS = "icon-prefs.png"
Expand All @@ -39,6 +40,8 @@
ICON_CLEAR = "icon-clear.png"
ICON_DK_ENABLE = "icon-dumbKeyboardE.png"
ICON_DK_DISABLE = "icon-dumbKeyboardD.png"
ICON_GL_ENABLE = "icon-gl-enable.png"
ICON_GL_DISABLE = "icon-gl-disable.png"
ICON_INFO = "icon-info.png"
ICON_STAR = "icon-star.png"
ICON_PEOPLE = "icon-people.png"
Expand All @@ -65,6 +68,8 @@

CUSTOM_TIMEOUT_DICT = {}

CUSTOM_TIMEOUT_CLIENTS = {'Plex Web': 15}

######################################################################################

def Start():
Expand Down Expand Up @@ -116,7 +121,7 @@ def MainMenu():
oc.add(DirectoryObject(key = Callback(Bookmarks, title="Bookmarks"), title = "Bookmarks", thumb = R(ICON_QUEUE)))
oc.add(DirectoryObject(key = Callback(SearchQueueMenu, title = 'Search Queue'), title = 'Search Queue', summary='Search using saved search terms', thumb = R(ICON_SEARCH_QUE)))

if common.UseDumbKeyboard():
if common.UsingOption(key='ToggleDumbKeyboard'):
DumbKeyboard(PREFIX, oc, Search,
dktitle = 'Search',
dkthumb = R(ICON_SEARCH)
Expand All @@ -143,15 +148,19 @@ def Options():

session = common.getSession()
if Dict['ToggleDumbKeyboard'+session] == None or Dict['ToggleDumbKeyboard'+session] == 'disabled':
oc.add(DirectoryObject(key = Callback(common.ToggleDumbKeyboard, session=session), title = 'Enable DumbKeyboard', summary='Click here to Enable DumbKeyboard for this Device', thumb = R(ICON_DK_ENABLE)))
oc.add(DirectoryObject(key=Callback(common.setDictVal,key='ToggleDumbKeyboard',session=session, val='enabled'), title = 'Enable DumbKeyboard', summary='Click here to Enable DumbKeyboard for this Device', thumb = R(ICON_DK_ENABLE)))
else:
oc.add(DirectoryObject(key=Callback(common.setDictVal,key='ToggleDumbKeyboard',session=session, val='disabled'), title = 'Disable DumbKeyboard', summary='Click here to Disable DumbKeyboard for this Device', thumb = R(ICON_DK_DISABLE)))

if Dict['ToggleRedirector'+session] == None or Dict['ToggleRedirector'+session] == 'disabled':
oc.add(DirectoryObject(key=Callback(common.setDictVal,key='ToggleRedirector',session=session, val='enabled'), title = 'Enable Redirector', summary='Click here to Enable Redirector method for this Device', thumb = R(ICON_GL_ENABLE)))
else:
oc.add(DirectoryObject(key = Callback(common.ToggleDumbKeyboard, session=session), title = 'Disable DumbKeyboard', summary='Click here to Disable DumbKeyboard for this Device', thumb = R(ICON_DK_DISABLE)))
oc.add(DirectoryObject(key=Callback(common.setDictVal,key='ToggleRedirector',session=session, val='disabled'), title = 'Disable Redirector', summary='Click here to Disable Redirector method for this Device', thumb = R(ICON_GL_DISABLE)))

oc.add(DirectoryObject(key = Callback(ClearCache), title = "Clear Cache", summary='Forces clearing of the Cache cookies and links', thumb = R(ICON_CLEAR)))
oc.add(DirectoryObject(key = Callback(ClearCache), title = "Clear Cache", summary='Forces clearing of the Cache cookies and links. Cache items: %s' % (len(fmovies.CACHE)), thumb = R(ICON_CLEAR)))

return oc



######################################################################################
@route(PREFIX + "/clearcache")
def ClearCache():
Expand Down Expand Up @@ -259,7 +268,7 @@ def ShowMenu(title):
)
)

if common.UseDumbKeyboard():
if common.UsingOption(key='ToggleDumbKeyboard'):
DumbKeyboard(PREFIX, oc, Search,
dktitle = 'Search',
dkthumb = R(ICON_SEARCH)
Expand Down Expand Up @@ -372,7 +381,7 @@ def SortMenu(title):
)
)

if common.UseDumbKeyboard():
if common.UsingOption(key='ToggleDumbKeyboard'):
DumbKeyboard(PREFIX, oc, Search,
dktitle = 'Search',
dkthumb = R(ICON_SEARCH)
Expand Down Expand Up @@ -455,7 +464,7 @@ def ShowCategory(title, key=' ', urlpath=None, page_count='1'):
)
)

if common.UseDumbKeyboard():
if common.UsingOption(key='ToggleDumbKeyboard'):
DumbKeyboard(PREFIX, oc, Search,
dktitle = 'Search',
dkthumb = R(ICON_SEARCH)
Expand Down Expand Up @@ -785,7 +794,7 @@ def EpisodeDetail(title, url, thumb):

try:
oc.add(DirectoryObject(
key = Callback(EpisodeDetail1, title=title_s, url=url, servers_list_new=servers_list_new[c], server_lab=(','.join(str(x) for x in server_lab)), summary=desc+'\n '+summary, thumb=thumb, art=art, year=year, rating=rating, duration=duration, genre=genre, directors=directors, roles=roles, serverts=serverts),
key = Callback(TvShowDetail, tvshow=title, title=title_s, url=url, servers_list_new=servers_list_new[c], server_lab=(','.join(str(x) for x in server_lab)), summary=desc+'\n '+summary, thumb=thumb, art=art, year=year, rating=rating, duration=duration, genre=genre, directors=directors, roles=roles, serverts=serverts),
title = title_s,
summary = desc+ '\n ' +summary,
art = art,
Expand Down Expand Up @@ -825,7 +834,7 @@ def EpisodeDetail(title, url, thumb):
try:
title_s = 'Ep:' + eps[server_lab[0]]['quality']
oc.add(DirectoryObject(
key = Callback(EpisodeDetail1, title=title_s, url=url, servers_list_new=servers_list_new[c], server_lab=(','.join(str(x) for x in server_lab)), summary='Episode Summary Not Available.\n ' + summary, thumb=thumb, art=art, year=year, rating=rating, duration=duration, genre=genre, directors=directors, roles=roles, serverts=serverts),
key = Callback(TvShowDetail, tvshow=title, title=title_s, url=url, servers_list_new=servers_list_new[c], server_lab=(','.join(str(x) for x in server_lab)), summary='Episode Summary Not Available.\n ' + summary, thumb=thumb, art=art, year=year, rating=rating, duration=duration, genre=genre, directors=directors, roles=roles, serverts=serverts),
title = title_s,
summary = 'Episode Summary Not Available.\n ' + summary,
thumb = Resource.ContentsOfURLWithFallback(url = thumb, fallback=ICON_UNAV)
Expand All @@ -849,7 +858,7 @@ def EpisodeDetail(title, url, thumb):
Log("case for presenting movies")

# create timeout thread
Thread.Create(ThreadTimeoutTimer, {}, E(url), client_id)
Thread.Create(ThreadTimeoutTimer, {}, Client.Product, E(url), client_id)

for label in server_lab:
for label_i in servers_list[label]:
Expand All @@ -867,27 +876,35 @@ def EpisodeDetail(title, url, thumb):
isVideoOnline = isItemVidAvailable(isOpenLoad=isOpenLoad, data=data)
status = common.GetEmoji(type=isVideoOnline) + ' '

durl = "fmovies://" + E(JSON.StringFromObject({"url":url, "server":server_info, "title":title, "summary":summary, "thumb":thumb, "art":art, "year":year, "rating":rating, "duration":str(duration), "genre":genre, "roles":roles, "directors":directors, "roles":roles, "isOpenLoad":str(isOpenLoad), "useSSL":str(Prefs["use_https_alt"]), "isVideoOnline":str(isVideoOnline)}))
try:
redirector_stat = ''
redirector_enabled = 'false'
if common.UsingOption(key='ToggleRedirector') and isOpenLoad == False:
redirector_stat = ' (via Redirector)'
redirector_enabled = 'true'

durl = "fmovies://" + E(JSON.StringFromObject({"url":url, "server":server_info, "title":title, "summary":summary, "thumb":thumb, "art":art, "year":year, "rating":rating, "duration":str(duration), "genre":genre, "roles":roles, "directors":directors, "roles":roles, "isOpenLoad":str(isOpenLoad), "useSSL":str(Prefs["use_https_alt"]), "isVideoOnline":str(isVideoOnline), "useRedirector": redirector_enabled}))

oc.add(VideoClipObject(
url = durl,
title = status + title + ' - ' + title_s,
title = status + title + ' - ' + title_s + redirector_stat,
thumb = thumb,
art = art,
summary = summary,
key = AddRecentWatchList(title=title, url=url, summary=summary, thumb=thumb)
)
)

except Exception as e:
Log('ERROR init.py>EpisodeDetail>Movie %s, %s' % (e.args, (title + ' - ' + title_s)))
pass
except Exception as e:
Log('ERROR init.py>EpisodeDetail>Movie %s, %s' % (e.args, (title + ' - ' + title_s)))
pass
if isTimeoutApproaching(item = E(url), client_id=client_id):
if isTimeoutApproaching(clientProd = Client.Product, item = E(url), client_id=client_id):
Log("isTimeoutApproaching action")
#break
return MC.message_container('Timeout', 'Timeout: Please try again !')
break
#return MC.message_container('Timeout', 'Timeout: Please try again !')

itemtype = ('show' if isTvSeries else 'movie')

Expand Down Expand Up @@ -942,8 +959,8 @@ def EpisodeDetail(title, url, thumb):

return oc

@route(PREFIX + "/episodedetail1")
def EpisodeDetail1(title, url, servers_list_new, server_lab, summary, thumb, art, year, rating, duration, genre, directors, roles, serverts):
@route(PREFIX + "/TvShowDetail")
def TvShowDetail(tvshow, title, url, servers_list_new, server_lab, summary, thumb, art, year, rating, duration, genre, directors, roles, serverts):

oc = ObjectContainer(title2 = title, art = art, no_cache=isForceNoCache())

Expand All @@ -956,7 +973,7 @@ def EpisodeDetail1(title, url, servers_list_new, server_lab, summary, thumb, art
client_id = '%s-%s' % (Client.Product, session)

# create timeout thread
Thread.Create(ThreadTimeoutTimer, {}, E(url), client_id)
Thread.Create(ThreadTimeoutTimer, {}, Client.Product, E(url), client_id)

for label in server_lab:
url_s = servers_list_new[label]['loc']
Expand All @@ -971,46 +988,56 @@ def EpisodeDetail1(title, url, servers_list_new, server_lab, summary, thumb, art
isVideoOnline = isItemVidAvailable(isOpenLoad=isOpenLoad, data=data)
status = common.GetEmoji(type=isVideoOnline) + ' '

durl = "fmovies://" + E(JSON.StringFromObject({"url":url, "server":server_info, "title":title, "summary":summary, "thumb":thumb, "art":art, "year":year, "rating":rating, "duration":str(duration), "genre":genre, "directors":directors, "roles":roles, "isOpenLoad":str(isOpenLoad), "useSSL":str(Prefs["use_https_alt"]), "isVideoOnline":str(isVideoOnline)}))

redirector_stat = ''
redirector_enabled = 'false'
if common.UsingOption(key='ToggleRedirector') and isOpenLoad == False:
redirector_stat = ' (via Redirector)'
redirector_enabled = 'true'

durl = "fmovies://" + E(JSON.StringFromObject({"url":url, "server":server_info, "title":title, "summary":summary, "thumb":thumb, "art":art, "year":year, "rating":rating, "duration":str(duration), "genre":genre, "directors":directors, "roles":roles, "isOpenLoad":str(isOpenLoad), "useSSL":str(Prefs["use_https_alt"]), "isVideoOnline":str(isVideoOnline), "useRedirector": redirector_enabled}))
try:
oc.add(VideoClipObject(
url = durl,
title = status + title + ' (' + label + ')',
title = status + title + ' (' + label + ')' + redirector_stat,
thumb = thumb,
art = art,
summary = summary,
key = AddRecentWatchList(title=title, url=url, summary=summary, thumb=thumb)
key = AddRecentWatchList(title = "%s - %s" % (tvshow,title), url=url, summary=summary, thumb=thumb)
)
)
except:
pass
if isTimeoutApproaching(item = E(url), client_id=client_id):

if isTimeoutApproaching(clientProd = Client.Product, item = E(url), client_id=client_id):
Log("isTimeoutApproaching action")
#break
return MC.message_container('Timeout', 'Timeout: Please try again !')
break
#return MC.message_container('Timeout', 'Timeout: Please try again !')

return oc

####################################################################################################
@route(PREFIX + "/ThreadTimeoutTimer")
def ThreadTimeoutTimer(item, client_id):
def ThreadTimeoutTimer(clientProd, item, client_id):

c=0
while c < 60:
CUSTOM_TIMEOUT_DICT[client_id][item] = c
time.sleep(1.0)
c += 1

del CUSTOM_TIMEOUT_DICT[client_id][item]
if clientProd in CUSTOM_TIMEOUT_CLIENTS:
c=0
while c < 60:
CUSTOM_TIMEOUT_DICT[client_id][item] = c
time.sleep(1.0)
c += 1

del CUSTOM_TIMEOUT_DICT[client_id][item]

####################################################################################################
@route(PREFIX + "/isTimeoutApproaching")
def isTimeoutApproaching(item, client_id):
def isTimeoutApproaching(clientProd, item, client_id):

# define custom timeouts for each client along with session & item to make it unique for multiple instances
if Client.Product == 'Plex Web':

if Client.Product in CUSTOM_TIMEOUT_CLIENTS:
t_sec = int(CUSTOM_TIMEOUT_DICT[client_id][item])
if t_sec < 15:
if t_sec < int(CUSTOM_TIMEOUT_CLIENTS[clientProd]):
if Prefs["use_debug"]:
Log("Custom Timout Timer: %s on %s: %s sec." % (D(item), client_id, t_sec))
return False
Expand Down
56 changes: 28 additions & 28 deletions Contents/Code/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

################################################################################
TITLE = "FMoviesPlus"
VERSION = '0.10' # Release notation (x.y - where x is major and y is minor)
VERSION = '0.11' # Release notation (x.y - where x is major and y is minor)
GITHUB_REPOSITORY = 'coder-alpha/FMoviesPlus.bundle'
PREFIX = "/video/fmoviesplus"
################################################################################
Expand Down Expand Up @@ -54,15 +54,17 @@ def GetKeyFromVal(list, val_look):
# Gets a client specific identifier
@route(PREFIX + '/getsession')
def getSession():

session = 'UnknownPlexClientSession'
if 'X-Plex-Client-Identifier' in str(Request.Headers):
return str(Request.Headers['X-Plex-Client-Identifier'])
session = str(Request.Headers['X-Plex-Client-Identifier'])
elif 'X-Plex-Target-Client-Identifier' in str(Request.Headers):
return str(Request.Headers['X-Plex-Target-Client-Identifier'])
session = str(Request.Headers['X-Plex-Target-Client-Identifier'])
elif 'User-Agent' in str(Request.Headers) and 'X-Plex-Token' in str(Request.Headers):
return 'UnknownClient-'+encode(str(Request.Headers['User-Agent']) + str(Request.Headers['X-Plex-Token'][:3]))[:10]
else:
return 'UnknownPlexClientSession'
session = 'UnknownClient-'+encode(str(Request.Headers['User-Agent']) + str(Request.Headers['X-Plex-Token'][:3]))[:10]

return (session)

#######################################################################################################
# base64 decode
@route(PREFIX + '/decode')
Expand All @@ -77,25 +79,23 @@ def encode(str):
return base64.b64encode(str)

#######################################################################################################
@route(PREFIX + "/UseDumbKeyboard")
def UseDumbKeyboard():
@route(PREFIX + "/setDictVal")
def setDictVal(key, session, val):
Dict[key+session] = val
Dict.Save()
if Prefs["use_debug"]:
Log("%s status: %s" % (key,val))

return ObjectContainer(header=key, message=key.replace('Toggle','') + ' has been ' + Dict[key+session] + ' for this device.', title1=key)

@route(PREFIX + "/UsingOption")
def UsingOption(key):
session = getSession()
if Dict['ToggleDumbKeyboard'+session] == None or Dict['ToggleDumbKeyboard'+session] == 'disabled':
if Dict[key+session] == None or Dict[key+session] == 'disabled':
return False
else:
return True

@route(PREFIX + "/ToggleDumbKeyboard")
def ToggleDumbKeyboard(session):

if Dict['ToggleDumbKeyboard'+session] == None or Dict['ToggleDumbKeyboard'+session] == 'disabled':
Dict['ToggleDumbKeyboard'+session] = 'enabled'
else:
Dict['ToggleDumbKeyboard'+session] = 'disabled'

Dict.Save()
return ObjectContainer(header='DumbKeyboard', message='DumbKeyboard has been ' + Dict['ToggleDumbKeyboard'+session] + ' for this device.', title1='DumbKeyboard')

####################################################################################################
# Get HTTP response code (200 == good)
@route(PREFIX + '/gethttpstatus')
Expand Down Expand Up @@ -161,34 +161,34 @@ def GetPageElements(url, headers=None):

######################################################################################
@route(PREFIX + "/GetPageAsString")
def GetPageAsString(url, headers=None):
def GetPageAsString(url, headers=None, timeout=15):

page_data_string = None
try:
if Prefs["use_https_alt"]:
if Prefs["use_debug"]:
Log("Using SSL Alternate Option")
Log("Url: " + url)
page_data_string = fmovies.request(url = url, headers=headers)
page_data_string = fmovies.request(url = url, headers=headers, timeout=timeout)
elif Prefs["use_web_proxy"]:
if Prefs["use_debug"]:
Log("Using SSL Web-Proxy Option")
Log("Url: " + url)

if headers == None:
page_data_string = HTTP.Request(fmovies.PROXY_URL + url).content
page_data_string = HTTP.Request(fmovies.PROXY_URL + url, timeout=timeout).content
else:
page_data_string = HTTP.Request(fmovies.PROXY_URL + url, headers=headers).content
page_data_string = HTTP.Request(fmovies.PROXY_URL + url, headers=headers, timeout=timeout).content
page_data_string = page_data_string.replace(fmovies.PROXY_PART1, fmovies.PROXY_PART1_REPLACE)
page_data_string = page_data_string.replace(fmovies.PROXY_PART2A, fmovies.PROXY_PART2_REPLACE)
page_data_string = page_data_string.replace(fmovies.PROXY_PART2B, fmovies.PROXY_PART2_REPLACE)
else:
if headers == None:
page_data_string = HTTP.Request(url).content
page_data_string = HTTP.Request(url, timeout=timeout).content
else:
page_data_string = HTTP.Request(url, headers=headers).content
page_data_string = HTTP.Request(url, headers=headers, timeout=timeout).content
except Exception as e:
Log('ERROR common.py>GetPageAsString: %s' % (e.args))
Log('ERROR common.py>GetPageAsString: %s URL: %s' % (e.args,url))
pass

return page_data_string
Expand Down
Binary file added Contents/Resources/icon-enter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Contents/Resources/icon-gl-disable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Contents/Resources/icon-gl-enable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 210ad46

Please sign in to comment.