[aa2678]: plugin.video.pinkbike / default.py  Maximize  Restore  History

Download this file

161 lines (148 with data), 5.8 kB

  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
import xbmc
import xbmcgui
import xbmcplugin
import re
import urllib2
import urllib
import math
BASE_URL = "http://www.pinkbike.com/video"
from BeautifulSoup import MinimalSoup as BeautifulSoup, SoupStrainer
def getHTML(url):
try:
print 'getHTML :: url = ' + url
req = urllib2.Request(url)
response = urllib2.urlopen(req)
link = response.read()
response.close()
except urllib2.HTTPError, e:
print "HTTP error: %d" % e.code
except urllib2.URLError, e:
print "Network error: %s" % e.reason.args[1]
else:
return link
def listPage(url):
html = getHTML(urllib.unquote_plus(url))
soup = BeautifulSoup(html)
currentPage = soup.find('li', 'current-page').a['href']
nextPage = soup.find('li', 'next-page').a['href']
maxPage = soup.find('li', 'next-page').findPrevious('li').a['href']
for inItem in soup.findAll('div', 'inItem'):
try:
title = inItem.findAll('a')[1].contents[0].replace('&','&')
except:
title = "No title"
link = inItem.find('a')['href']
re_pinkbike = 'video/(\d+)/'
id = re.findall(re_pinkbike, link)[0]
id = int(id)
partId = int(math.fabs(id/10000))
url = 'http://lv1.pinkbike.org/vf/' + str(partId) + '/pbvid-' + str(id) + '.flv'
thumb = inItem.find('img', 'thimg')['src']
time = inItem.find('span', 'fblack').contents[0]
plot = inItem.find('p', 'uFullInfo f10 fgrey3').contents[0].strip()
listitem=xbmcgui.ListItem(title, iconImage="DefaultFolder.png", thumbnailImage=thumb)
listitem.setInfo(type="Video", infoLabels={ "Title": title, "Plot" : plot, "Duration" : time })
listitem.setPath(url)
listitem.setProperty("IsPlayable", "true")
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=listitem)
if currentPage != maxPage:
item=xbmcgui.ListItem('Next page...', iconImage="DefaultFolder.png")
xurl = sys.argv[0] + '?' + "next=true" + "&url=" + urllib.quote_plus(nextPage.replace('&','&'))
item.setInfo(type="Video", infoLabels={ "Title": ""})
item.setPath(xurl)
folder = True
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=xurl, listitem=item, isFolder=folder)
return
def nextPage(params):
get = params.get
url = get("url")
return listPage(url)
def firstPage():
html = getHTML(urllib.unquote_plus(BASE_URL))
# https://bugs.launchpad.net/beautifulsoup/+bug/838022
BeautifulSoup.NESTABLE_TAGS['td'] = ['tr', 'table']
soup = BeautifulSoup(html)
# Favorites
for links in soup.findAll('a','iconlink'):
try:
title = links.contents[0]
except:
title = "No title"
try:
link = links['href']
except:
link = None
if link and title and not "img" in str(title):
addPosts(('Most faved ' + str(title)), urllib.quote_plus(link.replace('&','&')))
# Topics
for table in soup.findAll('table'):
for line in table.findAll('tr'):
try:
title = line.find('a').contents[0]
except:
title = None
try:
link = line.find('a')['href']
except:
link = None
if title and link:
addPosts(str(title), urllib.quote_plus(link.replace('&','&')))
# Search
addPosts('Search..', '&search=True')
return
def addPosts(title, url):
listitem=xbmcgui.ListItem(title, iconImage="DefaultFolder.png")
listitem.setInfo( type="Video", infoLabels={ "Title": title } )
xurl = "%s?next=True&url=" % sys.argv[0]
xurl = xurl + url
listitem.setPath(xurl)
folder = True
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=xurl, listitem=listitem, isFolder=folder)
def search_site():
encodedSearchString = search()
if not encodedSearchString == "":
url = 'http://www.pinkbike.com/video/search/?q=' + encodedSearchString
listPage(url)
else:
firstPage()
return
def search():
searchString = unikeyboard("", 'Search PinkBike.com')
if searchString == "":
xbmcgui.Dialog().ok('PinkBike.com','Missing text')
elif searchString:
dialogProgress = xbmcgui.DialogProgress()
dialogProgress.create('PinkBike.com', 'Searching for: ' , searchString)
#The XBMC onscreen keyboard outputs utf-8 and this need to be encoded to unicode
encodedSearchString = urllib.quote_plus(searchString.decode("utf_8").encode("raw_unicode_escape"))
return encodedSearchString
#From old undertexter.se plugin
def unikeyboard(default, message):
keyboard = xbmc.Keyboard(default, message)
keyboard.doModal()
if (keyboard.isConfirmed()):
return keyboard.getText()
else:
return ""
#FROM plugin.video.youtube.beta -- converts the request url passed on by xbmc to our plugin into a dict
def getParameters(parameterString):
commands = {}
splitCommands = parameterString[parameterString.find('?')+1:].split('&')
for command in splitCommands:
if (len(command) > 0):
splitCommand = command.split('=')
name = splitCommand[0]
value = splitCommand[1]
commands[name] = value
return commands
if (__name__ == "__main__" ):
if (not sys.argv[2]):
firstPage()
else:
params = getParameters(sys.argv[2])
get = params.get
if (get("search")):
search_site()
if (get("next")) and not (get("search")):
nextPage(params)
xbmcplugin.endOfDirectory(int(sys.argv[1]))