Вы находитесь на странице: 1из 40

Plugin creation XBMC. Introduction.

Hello, I`m Vince. Voinage to the Xbmc community, when i first started using Xbmc it was Xbox media player and the Tv schedules where awful and apparently in a world with more choice than ever over Tv content I felt I had none. !orever damned to download via strange methods two hours before i wanted to watch something then !T" the file to my Xbox. #rrrrrrgh. Then Xbmp evolved into the Xbmc that we all $now and love, with it`s intuitive interface, clean design and powerful "ython integration it was far in advance of all other media experiences and best of all it was running on the Xbox platform. I would read through the !orum as a guest, probably li$e many of you, waiting for a new tv script then i would pounce and install, but this was in the early days of streaming and scripting. There was never really any good content. %ne morning, "lugins arrived direct access to any media via a fast structured interface, it was great. # new era had dawned for streaming and the floodgates didn`t open, they burst. &nbehagen founding father of plugins for Xbmc, had created a monster. 'ell done Hagen. (oon after the Xbmc user could en)oy a myriad of Tv delights all streamed to the Tv in their main room, warm and comfortable. Instead of s*uinting at the cold unfriendly glare of their "c monitor with an ear pressed to the spea$er. +ad old days, never again, will i resemble a ,ole caught on a riverban$ toilet in the moonlight. (tage -, .oox, (urf the channel, "ower flv /hoice was finally mine. It was a great time friends, people helping each other, a real community rallying together to gain the freedom of choice. 0o adverts )ust the right content at the right price free.

Then, it , happened. +lac$ 'ednesday. (tage1- closed. (urfthechannel changed it`s algorithms, powerflv chugged on but it was never the same.

I waited in the dar$, hoping for new plugins but they never came. #lone in the dar$ with no Tv I )oined The !orum and began to arm myself with "ython. 0ever again would i be beholden to the Tv execs, never again would i wait in the dar$. 02ITH23 'I44 5%&.

Selecting your Target.


0ow that you have decided to get of your 6 #rse 72nglish8 +ottom , #ss 72nglish8 9on$ey. #re you sitting comfortably : ;ood, then we shall begin. 5ou will need 61 5ourself , /oncentration , 9etermination. 'ireshar$ 1 http6<<prdownloads.sourceforge.net<wireshar$<wireshar$1setup1 =.>.?.exe "ython 1 http6<<www.python.org<download< Tutorial source code Default.py , test.py

(electing a target, sounds easy. I`ll )ust pic$ what i li$e. ;reat . 9on`t release it, en)oy it yourelf. .ust because you li$e Tropical fish videos doesn`t mean the /ommunity will. Try here 6 http6<<www.ovguide.com< 1 plenty of streaming targets , eh : 0ow, chec$ my re*uests thread here 1 http6<<xbmc.org<forum<showthread.php:t@AA--B then chec$ my ;ooglecode page 1 http6<<code.google.com<p<voinage1xbmc1plugins<downloads<list does one already exist : (earch the forum. There are more than enough re*uests to go around. 9on`t duplicate, what`s the point. 5ou spend hours on a plugin only to find that one already exists and )ust needs fixing. !ix it help a Voinage in distress. Have you chec$ed the threads and the !orum : 3emember the A /`s 61 Consider, Community, Content.

Install "ython and find where you put the source code that came with this tutorial. Right clic on 6 1 Default.py and select !dit "ith Idle. 9on`t be afraid, this is the fun part. Honestly, it is.

Plugin template # Brea do"n.


This is )ust to familiarise yourself with the layout of the plugin template. It`s not in depth "ython really can feel difficult. 3emember it`s all about the Tabs.

"ython is )ust li$e you and I, it has the ability to use information from other sources. If you don`t $now anything about "ython you can read a boo$ from a library. (o a "ython library is a collection of useful functions that the main program can use. To use them we need to import them, you will only use C at maximum for basic plugin creation. import urlli$,urlli$%,re,&$mcplugin,&$mcgui

urlli$ ' a collection of http routines. urlli$% ' a more advanced collection of http routines. re ' a collection of string manipulation routines. &$mcplugin ' specific Xbmc routines. &$mcgui ' specific Xbmc routines. 'ithout the above imports, python has no reference and the plugin will error and fail. The next thing you will see is the 7 C(T!)*RI!S +, # 8 function.

This handy little function ta es the stress out of Directory listing.

def C(T!)*RI!S+,# addDir+--,--,.,--, addDir+ --,--,.,--, addDir+ --,--,.,--, addDir+--,--,.,--,

5ou define a function in python with the def command. This function adds the first 9irectories that you see when you start the plugin. 4et`s brea$ it down. (can down the "lugin template.

def addDir+name,url,mode,iconimage,# u/sys.arg01234-5url/-4urlli$.6uote7plus+url,4-8mode/-4str+mode, 4-8name/-4urlli$.6uote7plus+name, o /True li9/&$mcgui.:istItem+name, iconImage/-Default;older.png-, thum$nailImage/iconimage, li9.setInfo+ type/-<ideo-, info:a$els/= -Title-# name > ,

o /&$mcplugin.addDirectoryItem+handle/int+sys.arg01.3,,url/u,listitem/li9,is;older/True , return o

I`m not going to go into too much detail here, when you call the addDir+, command you are calling the above function. #ll that is happening is a name, url, and image are being passed to Xbmc in a format it li$es and it`s being told to display it. name @ The name of the 9irectory, either a string or the product of a scrape. "ython li$es strings or stored strings. To ma$e a string, give it a name then type your string in *uotes. a/ ? this is a string@ , notice how it turns green. !or the name that addDir needs you can either pass it a string.

addDir+?string@, @@ , . , @@, or a stored string addDir+name, A@ , . , @@ , 'e will examine stored strings later. url @ The web address, either a string or the product of a scrape. #s above pass either a string D http#BB""".testing.comE or a stored url string url. 0ext on the list is6 mode @ 'here in the program to )ump to .

4oo$ at the bottom of the Template.


if mode@@0one or url@@0one or len7url8F=6 print GG /#T2;%3I2(78 elif mode@@=6 print GGHurl I092X7url8 elif mode@@I6 print GGHurl VI92%4I0J(7url,name8

'e have A modes here you can have as many as you need. mode > @ /#T2;%3I2(78 mode=@ I092X7url,name8 modeI@VI92%4I0J(7url,name8 (o in the /#T2;%3I2( 78 6 function, we call add9ir7DE,EE,=,EE8 which tells the plugin to move on to mode = @ I092X7url8. Tell Xbmc to add the first 9irectories then move on to ICD!X function. (ee it`s not awful. The last term here is 6

Iconimage @ #n image for display with the 9irectory or lin$, either string or stored string. +asically a thumbnail. add9ir 7name, url , = , Ehttp6<<testing.com<image.gifE8 or a stored string add9ir7name, url , = ,thumb 8 Xbmc can handle pretty much all image formats 6 )pg ,gif, png etc The add4in$7name,url,iconimage8 is exactly the same except the isfolder@ option is set to false to tell Xbmc that this is a video lin$ and not a folder.

The next !unction is the 7 ICD!X +url, # 8 this opens the webpage of your chosen website, pretends to be a browser, reads it, stores the page and then scans the page for lin$s to the sacred video files.
def ICD!X+url,# re6 / urlli$%.Re6uest+url, re6.add7header+DEserF(gentD, D Mo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD, response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile+DD,.findall+lin , for thum$nail,url,name in match# addDir+name,url,%,DD,

'e are going to call on the urlli$% library not the Erlli$ library to connect to the web page because we can spoof the website into believing that the plugin is a real person using a browser.
re6 / urlli$%.Re6uest+url, re6.add7header+DEserF(gentD, D Mo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, oB%22L2J%M.N ;irefo&BK.2.KD,

)ec

url 1 is the web address that we passed during the previous C(T!)*RI!S +, F addDir+name, url , . ,thum$ , 'e have added a header, EserFagent. This allows "ython to pretend to be anything else, !irefox, Internet 2xplorer,Ipod,"hone,Veoh.

'e can send any headers here using the same format. .ust copy the re*.addKheader and put it under the other one. (ome sites need specific headers to access their information. This pretends to be the latest !irefox.
re6.add7header+DEserF(gentD, D Mo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD, re6.add7header+DContentFtypeD, DoctetFstreamBfl0D,

response

adds the re* calls and causes connection with the webpage.
F Oolds the "e$page that "as read 0ia the F Closes the connection after "e ha0e read

response / urlli$%.urlopen+re6, lin /response.read+, response.read+, command. response.close+, the "e$page. match/re.compile+DD,.findall+lin , for thum$nail,url,name in match#

lin is the holder for the page 7url from /#T2;%3I2(8 that was read. 'e close the open connection as we have the page and want to save memory. 0ow we scrape the screen for the relevant information. 'e are using the re library here6
match/re.compile+DD,.findall+lin ,

match is the holder for the compiled bits of information we have scraped. 'e will explore this in depth in the next part of the tutorial. .ust familiarise yourself for now. %pen http#B&$mc.orgBforumBregister.php in your browser 7 I use firefox 8. 3ight clic$ and select <ie" page source. (croll right down to the bottom of the page do you see 6 1
<div class="smallfont" align="center">

to scrape the words smallfont and center we would use


match/re.compile+D<div class="(.+?)" align="(.+?)">D,.findall+lin ,

so match now holds smallfont and center. The order matters so remember smallfont is first, center is second. Imagine that this was the url and name of a video.

match123 / smallfont match1.3 / center 4ets move on. for 5 in match# ;ets all the information stored in match and creates multiple lin$s or directories on your screen. for url,name in match# addDir+name,url,.,@@, url / The first thing held in match name / The second 5ou can call them whatever you wish. (o a directory will be created named @ center with the url @ smallfont #s you can see the <ID!*:ICP+, function is )ust the same really, except for add4in$ instead of add9ir. ,inimiLe the template, open your browser, right clic$ edit "ith idle 1 on test.py and lets really do something. If you are still unsure a$out "hat you ha0e read, re ' read.

Test, test, test again.


3ight, let`s have some fun. %pen your +rowser 7 I use !irefox for the tutorial8 Ctrl 8 clic the lin$ 6 1 http#BB""".o0guide.comB (croll down see T< D(SO, that`s the fellow for this tutorial. /lic$ it or Ctrl 8 clic the lin$ 6 1 http#BB""".t0dash.comB %nce it`s open +oo$mar$ it.

2xcellent /hoice, at first glance we have thumbnails for the movies M tv great for Xbmc. The site loo$s clean and tidy great for grabbing urls, without headaches. 4oo$ at the top center of of the screen, Home, movies, tvshows,games. They will become our main directories and as there are only two that we need, we will add them manually. +efore we do that lets get the scraping and testing done. /lic$ on the D M*<I!S D lin$.

# lovely list of movies, ha, nothing ma$es me feel more li$e a "irate than this #aargh .im`lad. Hoist the test.py. 3emember the name of the first movie listed here, we will need it later. !irst copy the url from the address bar of your browser. Right clic and copy. /lic$ to restore the test.py window you will see this or something li$e it. import urlli$%,urlli$,re
url/D Delete meD
re6 / urlli$%.Re6uest+url, re6.add7header+DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K )ec oB%22L2J%M.N ;irefo&BK.2.KD, response / urlli$%.urlopen+re6, lin /lemon.read+, response.close+, match/re.compile+DDelete me %D,.findall+lin , print match

/lic$ and delete the text url/D Delete meD url/D D 3eplace it with the url you have stored. Ctrl 8 <.

url/D http#BB""".t0dash.comBmo0ies.html@

"ress D ;G E

which is really run module in "ython terms.

The parentheses are the graphical representation of what`s stored in match. In this
case, nothing, yet. The D Print match D command causes the display, you can print any variable or string. 3emove it by putting Q in front of it Qprint match see ho" it turns red. (ny te&t you put after a Q character $ecomes a nonFusea$le note R )reat for adding your plugin title. The reason for this is that we haven`t provided any regular expression to scrape the data, we will soon.

+ut for now, we have a nice "ython shell open. #ll commands are case sensitive so type exactly what you have in test.py only. Type D print response.info+, D !CT!R this will bring up the server response from the website.

(ee how powerful "ython is : This is really useful, but not )ust yet. (ometimes /oo$ies that are re*uired to access sites reside here and can be accessed and manipulated to your benefit. /ommands and tabulation is what "ython is all about. +ac$ (ir<,adam, bac$ I say, to your browser and let`s loo$ at the source of the page. "ress Ctrl 8 E to view the page source. #t this point it is very easy to be put off, don`t be, once your eye is in becomes second nature. Honest. it

Hmmm ....... "ress Ctrl 8 f to bring up the search and enter the name of the first movie. .2 things i hate a$out you , 0ow this is more li$e it.

The name, iconimage 7Thumbnail8, url are all here. ;reat. !irst, maximiLe your "ython shell. In your "ython shell type 6 1 lin !CT!R

%nce it has completed, you will have a display of the webpage ready for scraping.

"ress Ctrl 8 f to bring up the search and enter .2 things I hate a$out you 5ou will see the above section. The highlighted area is exactly what we need. It has the thumbnail,name and url.
src/-http#BBfarmM.static.flic r.comBK.J2B%LL.%2JL%K7NJJ%LL.L.c7m.Spg$order/-2- BTUBtdTVn Utd class/-name"idth-TUa title/-Hatch .2 Things I Oate ($out Wou +.JJJ, +Re6uested, *nlinehref/-http#BB""".t0dash.comBmo0iesB.27Things7I7Oate7($out7Wou7+.JJJ,7+Re6ue sted,.html-T.2 Things I Oate ($out Wou +.JJJ, +Re6uested,UBaT

The yellow areas are what we need for the tutorial.


http#BBfarmM.static.flic r.comBK.J2B%LL.%2JL%K7NJJ%LL.L.c7m.Spg Thum$nail http#BB""".t0dash.comBmo0iesB.27Things7I7Oate7($out7Wou7+.JJJ,7+Re6uested,. html Erl .2 Things I Oate ($out Wou +.JJJ, +Re6uested,UBaT name

The 3ed section wouldn`t really loo$ nice displayed for a movie name, what do you thin$ : To scrape the text we need to show "ython what to ta$e and where it is.

Have a go at highlighting that whole section, 0%T .&(T TH2 5244%' +IT( , hold your left mouse button and drag it across the section. Then press Ctrl 8 c to copy to clipboard. /lose your "ython shell and restore your test.py. "lace your cursor in between the A A of the match/re.compile + A A ,.findall+lin , "ress Ctrl 8 <. 5ou should see this.
match/re.compile+Dsrc/-http#BBfarmM.static.flic r.comBK.J2B%LL.%2JL%K7NJJ%LL. L.c7m.Spg- $order/-2- BTUBtdTVn Utd class/-name"idth-TUa title/-Hatch .2 Things I Oate ($out Wou +.JJJ, +Re6uested, *nlinehref/-http#BB""".t0dash.comBmo0iesB.27Things7I7Oate7($out7Wou7+.JJJ,7+Re6ue sted,.html-T.2 Things I Oate ($out Wou +.JJJ, +Re6uested,UBaTD,.findall+lin ,

The text between the single *uotes is green because "ython recogniLes a string and highlights green. ;reen @ good 3ed @ bad , providing the code was not after the N character. "ython uses this to mar$ notes. ;uture reference. If the scrape has single *uotes in it 1 src/ A http#BBtester.html A you will need to change the wrapping *uotes match/re.compile+ Asrc/ A http#BBtester.html A A , to match/re.compile+ Xsrc/ A http#BBtester.html A X , to not confuse "ython. %$ay, let`s scrape that site. There are many useful regular expressions that "ython understands but generally for basic site scraping you only need 1 brac$ets around what you want to capture then delete the text and replace with6 +.45, ' Wep, it really can $e that simple ' sometimes. :et?s Sust say it means ta e e0erything inside the $rac ets ' it is a $asic tutorial. Oe,he. 'hen trouble brews and trust me , it will. Try here for really in depth help I did. http6<<www.am$.ca<python<howto<regex< http6<<www.python.org<doc<I.C.I<lib<re1syntax.html true meaning.

This will explain the

http6<<www.python.org<doc<I.C.I<lib<module1re.html http6<<www.regular1expressions.info<python.html

The scraping, following the tutorial 1 put brac$ets around what you want to capture then delete the text and replace with +.45, basically replace the yello" highlights "ith +.45, and we get 6
match/re.compile+Dsrc/-+.45,- $order/-2- BTUBtdTVn href/-+.45,-T+.45,UBaTD8.findall+lin , Utd class/-name"idth-TUa title/-

.45

(o here we have scraped the image,url and name. 5ou noticed didn`t you. The Red Oighlighted section has been replaced with .45 that is because that text will not be the same for every movie on the page. If the movie is A ;riday .Kth A it would say DHatch ;riday .Kth@ not AHatch .2 things I hate a$out you@ so we tell python to s$ip it with . 45 ' Co $rac ets, no scrape.
:et?s mo0e on . .you should end up "ith thi . . . Hhat do you mean you?re not ready. * ay go $ac and reFread the last section. Oonestly people today, "ouldn?t ha0e happened in my day . .

3eady now :

,aximiLe your "ython shell and type

OOO,atch !CT!R

Houston, we have an 32 match. The variable match now holds the information we wanted in the form of a Tuple.

0o not a Tribble

bad Tre$$ie.

!or information on tuples try here 6 http6<<en.wi$iboo$s.org<wi$i<"ythonK"rogramming<Tuples How many matches did we get : 4et`s use the len+, command length of variable. Type OOO len+match, !CT!R 'e get OOO%G. so our 32 pattern matched IC= times. That`s IC= movies. "ython always starts at Lero. (o the first match is match123 not match1.3 That means that the length really is IC>, so the last item is match1%G23 or match1F.3 # negative index number goes bac$wards, handy eh. Have a go, try a few things OOOmatch1.23 or match1F.23 etc 'e have A things stored in match try 6 match123123 hmmmm

Putting it together.
'ell where does that leave us : (o far we have scraped some information and stored it in the variable match. 'e have established that information stored in python variables can be accessed individually. 'e have established how to read a webpage contents then store and display them in python. 4et`s put it together.. 3estore Default.py and put your cursor between the *uotes of the name area.

Type D ,%VI2( D and again with url area this time restore test.py and copy, using Ctrl 8 c, the url/D http#BB""".t0dash.comBmo0ies.html@ text. ;o bac$ to Default.py and paste using Ctrl8< between the single *uotes in the url area.
addDir+-M*<I!S-,-http#BB""".t0dash.comBmo0ies.html- ,=,http#BBreggie$i$$s.files."ordpress.comB%22NB.%BtheFmo0iesKY2Fcrop.Spg- 8

If you would li$e Xbmc to display a custom thumbnail for the M*<I!S directory. "ut a url to an image or a path to an image, inside the single *uotes in the thum$nail area.

The path should be on your xbox not your "/. P6<X+,/<"4&;I0(<VI92%<5%&3 "4&;I0<I,#;2(<,ovie.png (o the previous.
addDir+-M*<I!S-,-http#BB""".t0dash.comBmo0ies.html- ,=,http#BBreggie$i$$s.files."ordpress.comB%22NB.%BtheFmo0iesKY2Fcrop.Spg- 8

#dds a directory folder named ,%VI2( to xbmc with the url http#BB""".t0dash.comBmo0ies.html moving to mode@= with the thumbnail
http#BBreggie$i$$s.files."ordpress.comB%22NB.%BtheFmo0iesKY2Fcrop.Spg

4et`s move to mode@= which is ICD!X+url, ;o to test.py and copy the text between the single *uotes of match/re.compile+A @, ;o to Default.py and delete the text between the match /re.compile+A@, *uotes and paste. 'e now should have6

for thum$nail,url,name in match# addDir+name,url,%,thum$nail, Here we go, for every occurrence of the Tuple +thum$nail,url,name, found in match add the directory 61 M*<I! C(M!, ER:, M*D!/%, TOEMBC(I: to Xbmc. 4ets go to mode/% which is <ID!*:ICPS+url,name, 61 'e are passing the Erl and the name from the ICD!X+url, 1 which only recieved the Erl from C(T!)*RI!S+, along to <ID!*:ICPS +url,name, whatever lives in the brac$ets is passed along. If you change this remem$er to change the mode area also R /lose "ython shell to clear the shell page. 3estore test.py and restore your $ro"ser "indo". Right clic on the first movie lin$ and Copy :in location.

+efore we move on it`s worth pointing out that nearly all video on websites is hidden or at least never )ust a direct lin 6 1 http#BBt0dash.comB.27things7i7hL7a$outyou.fl0 They will generally be hosted elsewhere by a provider such as6 Mega0ideo, Tudou, Wou u, Woutu$e, )oogle, <eoh. They re*uire a lot more handling than direct lin$s, but the routines are provided for you. 3ather than creating a class for you to import. 7'ell I did, but this is a basic tutorial8 .ust copy and paste them into Default.py as functions.

"aste the lin$ into your restored browser address bar and press !CT!R

'ell, what do we loo$ for : (ee the evil red play button. This e*uals ,egavideo it`s cut offs. It`s li$e the eye of (auron isn`t it bane of streaming with

,egavideo is very tric$y to resolve but than$s to /oolblaLe we can do it rather well.

(elect and restore test.py 9elete the url text inside the single *uotes and paste in the new url with Ctrl 8< 'e get 6 url@@http#BB""".t0dash.comBmo0iesB.27Things7I7Oate7($out7Wou7+.JJJ,7+Re6uested,.htm
l@

"ress A;G@ to run test.py Type OOOlin !CT!R

Ctrl 8 ; and type D ,egavideoE we $now it`s megavideo because of the 3ed play button. 4oo$ for 6
<embed src="http://www.megavideo.com/v/JE9VCAET" type="app icatio!/"#shoc$wave#% ash"

#s before we need the yellow not the red. The above is all we need for the 32 string so highlight it in the "ython shell window and copy it with Ctrl 8 C and close the python shell. 3estore the test.py and delete the text inside the single *uotes of match/re.compile+A @, paste the copied text. 5ou should have 6
match@re.compile+AUem$ed src/-http#BB""".mega0ideo.comB0BZ!J<C(!T- type/-applicationB&F shoc "a0eFflash-@8.findall7lin$8

(et your R! 6 match@re.compile+AUem$ed src/-+.45, - type/-.45-@8.findall7lin$8

(o we will ta$e the url to the video, s$ipping the type@ E.H:E , try your own 32 expressions. /lose the "ython shell then select test.py and press D ;G E and type OOOmatch

(o match123 now holds the url. &nfortunately, Xbmc cannot follow this type of lin$ as it calls a hidden swf file. Xbmc needs a directly resolved lin$. http6<<wwwQIA.megavideo.com<files<cQA>IQB-Q>>RBCd>f-ea=>IfCC-daeBb< 'e will find that later, for now, copy the R! from match/re.compile+A here @, in test.py 3estore Default.py and paste into the match/re.compile+A here @,.findall+lin , in <ID!*:ICPS +url,name, If this was a direct lin to an fl0 or an a0i you would use for url in match6 add4in$7name,url,DD8

or )ust simply61 add4in$7name,match123,DD8

#s we need a resolved ,egavideo url let`s press on. %pen up <ideoroutines.py you should see a very scary loo$ing set of routines. 9on`t panic

Highlight and copy part = then restore Default.py and paste it below <ID!*:ICPS+url,name, It starts with def a)oin7arr8 and ends with return endstr. ;reat, at this point don`t try to understand it , it`s *uite frightening. The next step is to copy part I, so bac$ to <ideoroutines.py and copy part I with Ctrl 8 C

3estore Default.py and paste it below part =. %$ that`s the ,egavideo routine in place, what it does is call up a hidden page that ,egavideo use for information on their files. (crape the information and use it to decrypt and resolve the ;l0 file. # round of applause for /oolblaLe of Xbmc forums and other fame, hurrah S !or now, close all open "ython windows. 3estore your browser and clic$ on D M*<I!S E scroll down until you find D Streets of fire +.JLM, E

/lic$ on it. 'hoa, a new video provider. It`s Stage<u ' than$fully they use direct lin$s. (ee the download lin$..... hmmmmm

I will help you a bit here as it isn`t as easy as it seems. <ie" source Ctrl 8 f :oo for stage0u
<i%rame sty e=&border: '( width: )*'p"( height: +,'p"& src=&http://stagev-.com/embed.width=)/'0amp(height=+'*0amp(-id=h-gygad%i1om& scro i!g=&!o&2</i%rame2

This is different than the ,egavideo R!.


<embed src="http://www.megavideo.com/v/JE9VCAET" type="app icatio!/"#shoc$wave#% ash"

#lso our <ideolin +url,name, is coded for the Mega0ideo format. 3ight. 'e need to change the !unction, but for now open test.py right clic$ and !dit "ith Idle. 3estore your browser and copy the url from the address bar. (elect test.py and replace the old url with the new one. Ctrl 8 < "ress D ;G E and type OOOmatch !CT!R

match comes up empty, the R! is different. 3emember the len78 command from earlier, it`s time to use it. /lose the "ython shell and restore test.py and press A;G@ type !CT!R

TTTlin

Try Ctrl 8 f then DstageE , anything : .ust a beep. They have tried to stop you finding the lin$. (croll up until you find the highlighted section. It`s in yellow on the above page if you cannot see the image. /opy it. /lose the "ython shell. 3estore test.py

&nder the last match type if len+match,U.# press !CT!R

#ll it means is that if the length of match is less than = and the 6 tells "ython that the instruction is completed. (ee how "ython uses tabs, this is correct don`t worry. Try leaving the # out and press !CT!R , see what happens. This will lead to an error , press D;GE

"ython li$es /olons.

"ress *P and put the #

bac$.

0ext after chec$ing the tabs, type match/re.compile+DD,.findall+lin , "aste the copied text between the single *uotes. 5ou should have 6
match/re.compile+D Uiframe style/VD$order# 2I "idth# NG2p&I height# ML2p&VD src/VDhttp#BBstage0u.comBem$ed5 "idth/N%28ampIheight/M2G8ampIuid/hugygadfi9omVD scrolling/VDnoVDTUBiframeTD,.findall+lin ,

9elete the 5ellow highlighted text and replace with 7.H:8 +elow is what you test.py should loo$ li$e, press A;G@
import urlli$%,urlli$,re url/Thttp#BB""".t0dash.comBmo0iesBStreets7of7;ire7+.JLM,.htmlD re6 / urlli$%.Re6uest+url, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile7TFembed src@G7.H:8G type@G.H:GT8.findall+lin , if len+match,U.# match/re.compile7TFiframe style@UTborder6 >V width6 QC>pxV height6 ?B>pxUT src@UT7.H:8UT scrolling@UTnoUTOF<iframeOT,.findall+lin , match123/re.su$+D8ampID,D8D,match123,

Type OOOmatch

%$, the Stage<u url. 0o it isn`t. It`s a lin$ to another page because the website used a frame, hence the UiframeT tag. 4oo$ at the lin$. 9oes it loo$ o$ay : http#BBstage0u.comBem$ed5 "idth/N%28ampIheight/M2G8ampIuid/hugygadfi9om Try Ctrl 8 clic 5 to follow the lin$. A)O*ST IC TO! SO!::@ , "hat the hell

The 8ampI is the problem. 'elcome to the re.su$+D area= E,EareaI E, string , command. 3egular expression substitute. (o we now have a program that chec$s two R!?s if the first one fails and stores the information in match. 'e access that with match123 as there is only one lin$. +asically swap whatever you put in area. for whatever is in area%. The string is whatever holds the text to be swapped, in this case match123 it could be a text file a response.read+, stored webpage, anything really.

&nder the last match type6 match123/re.su$+D8ampID,D8D,match123, The whole thing should loo$ li$e this.
import urlli$%,urlli$,re url/Thttp#BB""".t0dash.comBmo0iesBStreets7of7;ire7+.JLM,.htmlD re6 / urlli$%.Re6uest+url, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile7TFembed src@G7.H:8G type@G.H:GT8.findall+lin , if len+match,U.# match/re.compile7TFiframe style@UTborder6 >V width6 QC>pxV height6 ?B>pxUT src@UT7.H:8UT scrolling@UTnoUTOF<iframeOT,.findall+lin , match123/re.su$+D8ampID,D8D,match123,

"ress A;G@ and type OOO match123 !CT!R http#BBstage0u.comBem$ed5 "idth/N%28height/M2G8uid/hugygadfi9om http#BBstage0u.comBem$ed5 "idth/N%28ampIheight/M2G8ampIuid/hugygadfi9om +it of a difference, eh : /lose the "ython shell. Ctrl 8 clic both to see the difference. :ea0e the ne" lin open. Close the old one. 'e now have a lin$ to a page that has the direct lin$ to a video file.

Right Clic and <ie" page source Ctrl 8 ; type Da0iE you will get three choices. "ress D;KE to $eep searching. Fparam name@GsrcG value@G7.H:8G <O is the best choice.

(o, close the page source tab or window and restore test.py press A;G@ Ctrl 8 ; type Aa0i@ and press A;K@ until you find the Uparam name/ lin$. /opy it and close the python shell. It`s time to introduce basic error handling. try6 whatever e&cept6 whatever In other words try a piece of code, but, if an error occurs use another piece of code. It`s a lifesaver.

The last time we loo$ed at test.py it loo$ed li$e this.


import urlli$%,urlli$,re url/Thttp#BB""".t0dash.comBmo0iesBStreets7of7;ire7+.JLM,.htmlD

re6 / urlli$%.Re6uest+url, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile7TFembed src@G7.H:8G type@G.H:GT8.findall+lin , if len+match,U.# match/re.compile7TFiframe style@UTborder6 >V width6 QC>pxV height6 ?B>pxUT src@UT7.H:8UT scrolling@UTnoUTOF<iframeOT,.findall+lin , match123/re.su$+D8ampID,D8D,match123,

'ell we need to change it around to include the error handling.


import urlli$%,urlli$,re url/Thttp#BB""".t0dash.comBmo0iesBStreets7of7;ire7+.JLM,.htmlD re6 / urlli$%.Re6uest+url, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, try# match/re.compile7TFembed src@G7.H:8G type@G.H:GT8.findall+lin , Mega0ideo+match123, e&cept# match/re.compile7TFiframe style@UTborder6 >V width6 QC>pxV height6 ?B>pxUT src@UT7.H:8UT scrolling@UTnoUTOF<iframeOT,.findall+lin , match123/re.su$+D8ampID,D8D,match123, re6 / urlli$%.Re6uest+match123, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile+DUparam name/-src- 0alue/-+.45,- BTD,.findall+lin , add:in +name,match123,@@,

It seems li$e a big leap, it isn`t really. 5ou should be getting more familiar with I942 now so change the code to resemble the above. This will be a really good lesson in "ython formatting. He he he. 3emember to press enter after each command and you shouldn`t go far wrong. 3emember to press A;G@ every once in a while to see if you have made a mista$e. 3emember "ython is case sensitive. "ython will tell you in no uncertain terms, you may hate it and this may have stopped your fun. It`s tough love, honest. 5ou will than$ me later. /heers, Vin.

9id that ta$e a while . . . . (orry. 'hat were we doing, hmmmm , yes. Code e&plained. Try 6 1 finding a match then call the ,egavideo function we dropped in earlier. That`s right time to add it. Its called by typing it`s name with the video lin$ to resolve in the brac$ets. Mega0ideo+match123, F 3emember that ,egavideo lin$ is stored in match123 It ta$es the url that you supply and resolves it into a megavideo flv then adds a playable lin$. add:in +name, mo0ielin 4D5.fl0D,DD, (ometimes xbmc doesn`t recogniLe the ,egavideo lin$ is an flv so the 4E:.flvE (imply adds the 5.fl0 to the movielin$. Try here for more on strings 6 http6<<www.python.org<doc<I.A.C<tut<nodeC.html !&cept6 1 if an error occurs, li$e not finding a match, then execute some new code. 'hich in this case is the R! and urlli$% actions needed to get that direct Stage<u lin$.
match/re.compile7TFiframe style@UTborder6 >V width6 QC>pxV height6 ?B>pxUT src@UT7.H:8UT scrolling@UTnoUTOF<iframeOT,.findall+lin , match123/re.su$+D8ampID,D8D,match123, re6 / urlli$%.Re6uest+match123, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile+DUparam name/-src- 0alue/-+.45,- BTD,.findall+lin , add:in +name,match123,@@,

(o first we scrape the lin$ using our new R!. Then we fix the lin$ with the re.su$ command, but it`s a new page and we need the direct lin$. (o, we open and read the new page. re6 / urlli$%.Re6uest+match123, 'e re*uest match123 as that is the index representation on the lin$ stored in match.

'e then scrape it with the new R! and add a lin$ so xbmc can play it. The full code is available if you open and run Toughlo0e.py Before you run it put a Q in front of addlin as that command is only good for &$mc. Type OOO match123 Direct Stage<u lin . http6<<n=A.stagevu.com<vlin$<dCBARRBbabb-d=?b?RI=?fIa=CfA=I->.avi 3ight, fol$s time to copy the new Videolin$s code over to Default.py /lose everything other than test.py and Default.py (elect 9efault.py 61 in the VI92%4I0J(7url,name8 highlight and delete the match/re.compile+A@, It should loo$ li$e this6
def <ID!*:ICPS+url,name,# re6 / urlli$%.Re6uest+url, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+,

(elect test.py and highlight and copy everything below response.close+, +ac$ to 9efault.py and paste below response.close+, 'e should now have 6
def <ID!*:ICPS+url,name,# re6 / urlli$%.Re6uest+url, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, try# match/re.compile7TUem$ed src/-+.45,- type/-.45-T8.findall+lin , Mega0ideo+match123, e&cept# match/re.compile7TFiframe style/VD$order# 2I "idth# NG2p&I height# ML2p&VD src/VD+.45,VD scrolling/VDnoVDTUBiframeTT,.findall+lin , match123/re.su$+D8ampID,D8D,match123, re6 / urlli$%.Re6uest+match123, re6.add7header7DEserF(gentD, DMo9illaBG.2 +Hindo"sI EI Hindo"s CT G..I enF)BI r0#..J.2.K, )ec oB%22L2J%M.N ;irefo&BK.2.KD8 response / urlli$%.urlopen+re6, lin /response.read+, response.close+, match/re.compile+DUparam name/-src- 0alue/-+.45,- BTD,.findall+lin , add:in +name,match123,@@,

"ress A;G@ , any errors : If so fix your formatting. If it`s wor$ing and formed well you should see.

That`s fine. 5ou will probably get 6 0o modules named Xbmc M Xbmc etc. I have one module running. If you still can`t get it wor$ing use the full program 6 1 :astResort.py Don?t gi0e up on getting it right if you used the :ast resort R

4oo$ing bac$ , scanning forward.


'ell fol$sages, That about wraps it up. ,a$e a folder and name it 61 'hatever you want, try T0dash %pen your Tv 9ash boo$mar$ and Right clic on the Tv 9ash logo. (elect <ie" Bac ground image right clic$ on the image and Sa0e image as try default.t$n 2verytime you highlight the tvdash folder in xbmc it will display that image. !T" the folder over to X+,/<"4&;I0(<VI92% 2n)oy your hardwor$. Than$s for reading, 5our friendly neighbourhood <oinage B <in. %h , I almost forgot. There is still a TV section, have a go yourself. ( fe" things to eep in mind.

The Tv section is very different and you will have to create two new functions. 3emember the modes they allow you to direct the flow of the plugin and add new functions.

(ometimes scraped urls are not the full url 111 main.php when it needs to be http6<<tvdash.com<main.php so in the add:in +name,@http#BBt0dash.comB@4url,@@, simple eh. "m me if you need help and try loo$ing at the "ython in my plugins for new Video routines or new plugin tric$s. I thin$ i will release an advanced version at some point if eneough people read this one. 3est easy +rothers and (isters. <oinage has left the building. 7Try the :ast resort.py for the full plugin with a few ::::8 Than$s to Team Xbmc for a wonderful "latform. &nbehagen for his mastery of "ython and creating the "lugin system.
;uido van 3ossum for creating "ython. 5ou for reading and being enthused. The sins of my youth.

http6<<www.angelfire.com<tvI<broo$sidesoapboxA<gallery<)asonKshadwic$<ind ex.htm The aged (t ;ermaine in his alembic lab... (tatic hairday hehe.

Try in this folder for more help with X+,/ internals. Xbmc 4ibs Help files M a great tutorial from #lexpoet. Than$s #lex.

Вам также может понравиться