2014-11-04, 12:16
I'm currently working on an AddOn that enables users to watch videos from Wintersporters.nl website (from YouTube and Vimeo) and gives additional infromation about ski-areas, weather-information, snow-conditions etc. using either a local CSV-file or a MySQL server.
One of the issues i'm still strugling with is that at the moment i'll have to create a seperate file for every ski-area, which isn't very practical. Python is all pretty new for me, so it might not always be the most logical way of getting things to work, but so far it all works pretty good.
The current code for the listing is looking like this:
the table on MySQL shows the name of the location in row[4]. row[5] gives the .py-file that contains the code for the video, while row[2] provides the country.
An example of such a area-file looks like this:
The only difference with all the other files is the "area"-variable. How can i get the proper listing, without having to create a seperate file for every location? changes can be made in the MySQL gebieden-table if necessary.
Thanks
One of the issues i'm still strugling with is that at the moment i'll have to create a seperate file for every ski-area, which isn't very practical. Python is all pretty new for me, so it might not always be the most logical way of getting things to work, but so far it all works pretty good.
The current code for the listing is looking like this:
Code:
import xbmcplugin
import xbmcgui
import xbmc
import xbmcaddon
import os
import sys
import xbmcplugin
import csv
import mysql.connector
addon_handle = int(sys.argv[1])
xbmcplugin.setContent(addon_handle, 'Movies')
addonID = 'plugin.video.wintersporters'
addonVersion = '0.3.0'
addonDate = "8 Oktober 2014"
DB = open(xbmc.translatePath('special://home/addons/'+addonID+'/modules/database.csv'))
CSV_DB = csv.reader(DB, delimiter=';')
LIB_DIR = xbmc.translatePath( os.path.join( xbmcaddon.Addon(id=addonID).getAddonInfo('path'), 'resources', 'lib' ) )
sys.path.append (LIB_DIR)
# Get plugin settings
DEBUG = xbmcaddon.Addon(id=addonID).getSetting('debug')
if (DEBUG) == 'true':
xbmc.log( "[ADDON] %s v%s (%s) is starting, ARGV = %s" % (addonID, addonVersion, addonDate, repr(sys.argv) ), xbmc.LOGNOTICE )
useSQL = xbmcaddon.Addon(id=addonID).getSetting('usedatabase')
mysqlhost = xbmcaddon.Addon(id=addonID).getSetting('mysqlhost')
mysqluser = xbmcaddon.Addon(id=addonID).getSetting('mysqluser')
mysqlpass = xbmcaddon.Addon(id=addonID).getSetting('mysqlpass')
mysqldb = xbmcaddon.Addon(id=addonID).getSetting('mysqldb')
def get_params():
param={}
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
def addDir(name, target, thumbnail, fanart, plot):
u=sys.argv[0]+"?module="+target
liz=xbmcgui.ListItem(label=name, iconImage=thumbnail, thumbnailImage=thumbnail)
liz.setInfo('Video', { 'Plot': plot})
liz.setProperty("fanart_image", fanart)
xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
params=get_params() # First, get the parameters
if 'target' in params: # Module chosen, load and execute module
target = 'modules.'+params['target']
__import__(target)
current_module = sys.modules[target]
else:
if useSQL == 'true':
database = mysql.connector
db = database.connect(host=mysqlhost, user=mysqluser, passwd=mysqlpass, db=mysqldb)
cursor = db.cursor(buffered=True)
cursor.execute("SELECT * FROM gebieden")
data = cursor.fetchall()
for row in data:
if row[2] == 'Duitsland':
addDir(row[4], row[5], xbmc.translatePath('special://home/addons/'+addonID+'/resources/media//thumbnails/'+row[2]+'.png'), xbmc.translatePath('special://home/addons/'+addonID+'/resources/media/backgrounds/'+row[2]+'.jpg'), row[27])
xbmcplugin.endOfDirectory(int(sys.argv[1]))
else:
for row in CSV_DB:
if row[2] == 'Duitsland':
addDir(row[4], row[5], xbmc.translatePath('special://home/addons/'+addonID+'/resources/media//thumbnails/'+row[2]+'.png'), xbmc.translatePath('special://home/addons/'+addonID+'/resources/media/backgrounds/'+row[2]+'.jpg'), row[27])
xbmcplugin.endOfDirectory(int(sys.argv[1]))
An example of such a area-file looks like this:
Code:
# -*- coding: utf-8 -*-
#------------------------------------------------------------
# http://www.youtube.com/user/epicmealtime
#------------------------------------------------------------
# License: GPL (http://www.gnu.org/licenses/gpl-3.0.html)
# Based on code from youtube addon
#------------------------------------------------------------
import plugintools
import xbmcgui
YOUTUBE_CHANNEL_ID = "UCbYBV0s2Oq8eep8K9i4PC0g"
area = "Winterberg"
# Entry point
def run():
plugintools.log(YOUTUBE_CHANNEL_ID+".run")
# Get params
params = plugintools.get_params()
if params.get("action") is None:
search_list(params)
else:
action = params.get("action")
exec action+"(params)"
plugintools.close_item_list()
# Search List
def search_list(params):
plugintools.log(YOUTUBE_CHANNEL_ID+".main_list "+repr(params))
# On first page, pagination parameters are fixed
if params.get("url") is None:
params["url"] = "http://gdata.youtube.com/feeds/api/users/"+YOUTUBE_CHANNEL_ID+"/uploads?q="+area+"&start-index=1&max-results=50"
# Fetch video list from YouTube feed
data = plugintools.read(params.get("url"))
# Extract items from feed
matches = plugintools.find_multiple_matches(data, "<entry>(.*?)</entry>")
for entry in matches:
# Not the better way to parse XML, but clean and easy
title = plugintools.find_single_match(entry,"<titl[^>]+>([^<]+)</title>")
plot = plugintools.find_single_match(entry,"<media\:descriptio[^>]+>([^<]+)</media\:description>")
thumbnail = plugintools.find_single_match(entry,"<media\:thumbnail url='([^']+)'")
video_id = plugintools.find_single_match(entry,"http\://www.youtube.com/watch\?v\=([^\&]+)\&").replace("&","&")
url = "plugin://plugin.video.youtube/?path=/root/video&action=play_video&videoid="+video_id
# Appends a new item to the xbmc item list
plugintools.add_item(action="play", title=title, plot=plot, url=url, thumbnail=thumbnail, folder=True)
# Play items
def play(params):
plugintools.play_resolved_url(params.get("url"))
run()
Thanks