# need basic help with plugin writing

**URL:** <https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [February 1, 2011, 5:03pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638 "2011-02-01T17:03:04Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![hrcolb0](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/hrcolb0/32/380_2.png) [@hrcolb0](https://forums.plex.tv/u/hrcolb0)\
**Post date:** [February 19, 2011, 12:42am UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/101 "2011-02-19T00:42:19Z")

</div>

for example   
  
  
  
content equals

> [@](#):
>
> Lots of stuff in here  
> ![]()

  
  
  
item in your code equals  

> [@](#):
>
> Lots of stuff in here

  
  
and if you use: thumb=content.xpath('//img').get('src')  
then thumb should equal  

> [@](#):
>
> http:// i am an image

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 19, 2011, 12:43am UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/102 "2011-02-19T00:43:48Z")

</div>

> [@](#):
>
> [...]  
> i want to add a completely different xpath for where the XXXXXX is. it's not within the //td[contains(@id,"large\_groupid\_")]'  
> how do i write that? just write a random example. :)

  
That depends completely on where the data is that you want/need (is it somewhere else on the same webpage or maybe even on another page?)

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 19, 2011, 3:24pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/103 "2011-02-19T15:24:55Z")

</div>

thanks on all the help.  
  
i was able to parse it.  
  
i ended up writing it as: drc = content.xpath(’//td[@colspan=“2”]/a’)[1].text  
  
and then later its:  
  
dir.Append(Function(DirectoryItem(PTPTorrent, subtitle=None, summary=“Director: %s” % (drc), title=title, thumb=thumb), thumb=thumb, url=url))  
  
  
  
it’s the director name that i just put there.  
  
however, mysteriously now every single item features the same, f.ex. “Director: Roger Rabbit” (bad example i know lol)  
  
it seems to take just the first one and continue to use the same one on all next items in the list that’s created.  
  
  
  
  
  
…  
  
  
  
also i found a bug my mod of the transmission plugin, therefore i need to be able to download a file that is located in the name torrenturl.  
  
  
  
what would the code be to make plex download the file to a custom location on the computer? (lets say to a folder called “/torrents” on MacintoshHD.  
  
something like   
  
download def():  
  
………………………   
  
  
  
any help?

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 19, 2011, 8:31pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/104 "2011-02-19T20:31:01Z")

</div>

> [@](#):
>
> i ended up writing it as: drc = content.xpath('//td[@colspan="2"]/a')[1].text  
> [...]  
> it's the director name that i just put there.  
> however, mysteriously now every single item features the same, f.ex. "Director: Roger Rabbit" (bad example i know lol)

  
Looking at the previous couple of posts, the variable \*content\* contains the complete HTML document. If you do an xpath query on that like //td you'll always get the same result. You need to find the element(s) you want to loop over, then do .//td on the element(s) you're using for your loop.

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 19, 2011, 9:45pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/105 "2011-02-19T21:45:24Z")

</div>

sorry, i don’t quite understand how to put that into code :blink:  
  
i tried drc = content.xpath(’.//td[@colspan=“2”]/a’)[1].text  
  
with the .//td but i still get always the same result.

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 20, 2011, 12:32am UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/106 "2011-02-20T00:32:45Z")

</div>

> [@](#):
>
> sorry, i don't quite understand how to put that into code :blink:  
> i tried drc = content.xpath('.//td[@colspan="2"]/a')[1].text  
> with the .//td but i still get always the same result.

  
  
\*content\* is the complete HTML document, so no matter where you place content.xpath('.//td[@colspan="2"]/a')[1].text in your code, it's always going to give you the same result, as the xpath is always applied to the exact same piece of HTML code.  
  
What you probably want is to use the xpath on each item you found in the for-loop:  
  

```auto

for item in content.xpath('.......'):<br />
  drc = item.xpath('.//td[@colspan="2"]/a')[1].text<br />

```

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 20, 2011, 1:02am UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/107 "2011-02-20T01:02:54Z")

</div>

got it to work. 🙂 thank you!  
  
  
  
do you maybe know if there is a possibiility to download a file inside an url ( is it the variable “print”?)

> [@](#):
>
> what would the code be to make plex download the file to a custom location on the computer? (lets say to a folder called “/torrents” on MacintoshHD.  
>   
> something like   
>   
> download def():

  
  
thank you!!

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 20, 2011, 2:08pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/108 "2011-02-20T14:08:50Z")

</div>

Can’t you let Transmission handle the .torrent download using the AddTorrent function that’s already present?  
  
[https://github.com/plexinc-plugins/Transmission.bundle/blob/master/Contents/Code/](https://github.com/plexinc-plugins/Transmission.bundle/blob/master/Contents/Code/) **init**.py#L366

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 20, 2011, 2:51pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/109 "2011-02-20T14:51:12Z")

</div>

there’s a bug in the transmission webserver where it ignores custom download locations. therefore i wanna just download the .torrent file to a specific folder. can you tell me how? 🙂 thanks!

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 20, 2011, 4:14pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/110 "2011-02-20T16:14:10Z")

</div>

> [@](#):
>
> there's a bug in the transmission webserver where it ignores custom download locations.

  
??? Can you tell us more about this? I think it's just a problem with authentication: you login inside the plugin, but Transmission downloading the .torrent file is done in a different session where you are not logged in.

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 20, 2011, 4:31pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/111 "2011-02-20T16:31:24Z")

</div>

no actually everything works. it’s this Transmission bug (and supposedly there is already a ticket on it)  
  
in transmission you can create groups to download files to certain folders depending on their filename/extension.  
  
this is not funcitonal yet using the webserver.   
  
  
  
[https://forum.transmissionbt.com/viewtopic.php?f=4&t=11269](https://forum.transmissionbt.com/viewtopic.php?f=4&t=11269)

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 22, 2011, 4:28pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/112 "2011-02-22T16:28:11Z")

</div>

i wanna be able to go to the next page of the search results, because only 50 are displayed in each page.  
  
it should appear at the bottom of the search results.  
  
the page links are here in the html:

```auto

<div class="linkbox"><a href="torrents.php?page=1"><strong>&lt;&lt; First</strong></a> <a href="torrents.php?page=2"><strong>&lt; Prev</strong></a> | <a href="torrents.php?page=1"><strong>1-50</strong></a> | <a href="torrents.php?page=2"><strong>51-100</strong></a> | <strong>101-150</strong> | <a href="torrents.php?page=4"><strong>151-200</strong></a> | <a href="torrents.php?page=5"><strong>201-250</strong></a> | <a href="torrents.php?page=6"><strong>251-300</strong></a> | <a href="torrents.php?page=7"><strong>301-350</strong></a> | <a href="torrents.php?page=8"><strong>351-400</strong></a> | <a href="torrents.php?page=9"><strong>401-450</strong></a> | <a href="torrents.php?page=10"><strong>451-500</strong></a> | <a href="torrents.php?page=11"><strong>501-550</strong></a> | <a href="torrents.php?page=4"><strong>Next &gt;</strong></a> <a href="torrents.php?page=605"><strong> Last &gt;&gt;</strong></a></div><br />

```

  
  
i want the (second before last) hyperlink, containing the text Next strong (=bold).  
i tried the following code but it doesn't work (please help me figure out the correct xpath for the nxt variable and the right dir.append for it)  
  

```auto

  for item in content.xpath('//tr[@class="group"]'):<br />
    Log(item)<br />
    title = item.xpath('./td[contains(@id,"large_groupid_")]/a/img')[0].get('title')<br />
    Log(title)<br />
    thumb = item.xpath('./td[contains(@id,"large_groupid_")]/a/img')[0].get('src')<br />
    url = item.xpath('./td[contains(@id,"large_groupid_")]/a')[0].get('href')<br />
    Log(url)<br />
    nxt = content.xpath('//a/strong[contains(@text,"Next")]')[0].get('href')<br />
    dir.Append(Function(DirectoryItem(PTPTorrent, subtitle=None, title=title, thumb=thumb), thumb=thumb, url=url))<br />
    dir.Append(Function(DirectoryItem(PTPTorrent, subtitle=None, title=title, thumb=thumb), thumb=thumb, url= PTPBASE_URL + nxt))<br />
  return dir<br />

```

  
  
i get the following log thrown at me:  
2/22/11 5:27:33 PM com.plexapp.mediaserver[181] nxt = content.xpath('//a/strong[contains(@text,"Next")]')[0].get('href')  
2/22/11 5:27:33 PM com.plexapp.mediaserver[181] IndexError: list index out of range  
  
thanks!

---

<div class="post-metadata">

**Author:** ![sander1](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@sander1](https://forums.plex.tv/u/sander1)\
**Post date:** [February 22, 2011, 5:50pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/113 "2011-02-22T17:50:24Z")

</div>

The right XPath to grab those “Next” link(s) is:

```auto

//a/strong[contains(text(), "Next")]/parent::a

```

  
And you probably don't want it inside your for-loop :rolleyes:  
  
Also, have a look at this XPath Tutorial some time, it covers almost everything you need when working with XPaths: http://wap.w3schools.com/xpath/default.asp

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 22, 2011, 10:29pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/114 "2011-02-22T22:29:40Z")

</div>

yea it shouldn’t be in there, but how else do i do it?  
  
  
  
2/22/11 11:28:37 PM com.plexapp.mediaserver[181] TypeError: cannot concatenate ‘str’ and ‘list’ objects

```auto

def SearchPassthePopcorn(sender, query, genre):<br />
  dir = MediaContainer()<br />
  Log("Success!")<br />
  if genre != "1" or "2":<br />
    pageUrl2 = "http://passthepopcorn.me/torrents.php?order_by=year&" + genre + "searchstr=%s" % (String.Quote(query, usePlus=True))<br />
  if genre == "1":<br />
    pageUrl2 = "http://passthepopcorn.me/torrents.php?order_by=year&artistname=%s" % (String.Quote(query, usePlus=True))<br />
  if genre == "2":<br />
    pageUrl2 = "http://passthepopcorn.me/torrents.php?year=%s" % (String.Quote(query, usePlus=True))<br />
  Log("Success!")<br />
  webpage = HTTP.Request(pageUrl2)<br />
  content = XML.ElementFromString(webpage, isHTML=True)<br />
  for item in content.xpath('//tr[@class="group"]'):<br />
    Log(item)<br />
    title = item.xpath('./td[contains(@id,"large_groupid_")]/a/img')[0].get('title')<br />
    Log(title)<br />
    thumb = item.xpath('./td[contains(@id,"large_groupid_")]/a/img')[0].get('src')<br />
    url = item.xpath('./td[contains(@id,"large_groupid_")]/a')[0].get('href')<br />
    Log(url)<br />
    dir.Append(Function(DirectoryItem(PTPTorrent, subtitle=None, title=title, thumb=thumb), thumb=thumb, url=url))<br />
  for item in content.xpath('//div[@class="linkbox"]'):<br />
    nxt = item.xpath('./a/strong[contains(text(), "Next")]/parent::a')<br />
    dir.Append(Function(DirectoryItem(PTPnextpage, "More Results", subtitle=None, thumb=None), thumb=None, url=PTPBASE_URL + nxt))<br />
  return dir<br />

```

---

<div class="post-metadata">

**Author:** ![jwray](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jwray/32/99832_2.png) [@jwray](https://forums.plex.tv/u/jwray)\
**Post date:** [February 23, 2011, 1:38am UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/115 "2011-02-23T01:38:08Z")

</div>

How about something like:

```auto

<br />
content = XML.ElementFromString(webpage, isHTML=True)<br />
  for item in content.xpath('//tr[@class="group"]'):<br />
    Log(item)<br />
    title = item.xpath('./td[contains(@id,"large_groupid_")]/a/img')[0].get('title')<br />
    Log(title)<br />
    thumb = item.xpath('./td[contains(@id,"large_groupid_")]/a/img')[0].get('src')<br />
    url = item.xpath('./td[contains(@id,"large_groupid_")]/a')[0].get('href')<br />
    Log(url)<br />
    dir.Append(Function(DirectoryItem(PTPTorrent, subtitle=None, title=title, thumb=thumb), thumb=thumb, url=url))<br />
  next = content.xpath('//div[@class="linkbox"]/a/strong[contains(text(), "Next")]/parent::a')<br />
  if len(next) > 0:<br />
    dir.Append(Function(DirectoryItem(PTPnextpage, "More Results", subtitle=None, thumb=None), thumb=None, url=PTPBASE_URL + next[0].get('href')))<br />
  return dir<br />

```

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 23, 2011, 12:47pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/116 "2011-02-23T12:47:26Z")

</div>

thank you! worked!  
  
  
  
2 more questions  
  
  
  
**1.** i’m having a lil bit of trouble with replacing in here.

```auto

year = item.xpath('./td[@colspan="2"]/child::text()[position()=2]')

```

  
  
year turns out to be f.ex. 

```auto

[' [1964] by ']

```

  
  
instead of 

```auto

1964

```

  
  
i tried using .replace but i don't know where and how in that huge xpath.  
  
the html bit the year info is stored in looks like:  
  

```auto

<td colspan="2"><br />
   <a href="torrents.php?id=2057" title="View Torrent">MOVIE TITLE</a> [1961] by <a href="artist.php?id=784">DIRECTOR</a> <br />

```

  
  
  
  
\*\*2.\*\* the director info is not always displayed in the same way. the search gives me 50 results, and most often one or two of them have no director info, or maybe more than 1 director. then the page won't display and i get   
  
2/23/11 1:36:27 PM [0x0-0x52052].com.plexapp.plexmediaserver[803] AttributeError: 'list' object has no attribute 'text'  
  
  
usually, html code is:  

```auto

			<br />
<td colspan="2"><br />
   <a href="torrents.php?id=2057" title="View Torrent">MOVIE TITLE</a> [1961] by <a href="artist.php?id=784">DIRECTOR</a> <br />

```

  
  
sometimes if the director is unknown however it's just:  
  

```auto

<br />
<td colspan="2"><br />
  <a href="torrents.php?id=2057" title="View Torrent">MOVIE TITLE</a> [1961] <br />

```

  
  
or if there's several directors it's:  
  

```auto

<br />
<td colspan="2"><br />
   <a href="torrents.php?id=2057" title="View Torrent">MOVIE TITLE</a> [1961] by <a href="artist.php?id=17153">DIRECTOR 1</a> and <a href="artist.php?id=17344">DIRECTOR 1</a>	<br />

```

  
  
you see the xpath's are getting more n more complex, that's why i keep asking in here. i read through the guides you showed me but this seems to be a tad too difficult.

---

<div class="post-metadata">

**Author:** ![jwray](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jwray/32/99832_2.png) [@jwray](https://forums.plex.tv/u/jwray)\
**Post date:** [February 23, 2011, 4:46pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/117 "2011-02-23T16:46:03Z")

</div>

Quickly, I’m running out.   
  
  
  
For 1. use the xpath you have and the python string split commands to extract the data you want.   
  
  
  
For 2. use the same xpath but loop through the results and build whatever string you need to represent all the directors.  
  
  
  
You don’t have to use xpath for everything, python has a lot of powerful string processing functions  
  
  
  
Jonny

---

<div class="post-metadata">

**Author:** ![pooploser](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pooploser/32/133851_2.png) [@pooploser](https://forums.plex.tv/u/pooploser)\
**Post date:** [February 23, 2011, 5:22pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/118 "2011-02-23T17:22:32Z")

</div>

that sounds great, only that i have even less knowledge of python than of xpath.  
  
could you give me an example code?

---

<div class="post-metadata">

**Author:** ![hrcolb0](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/hrcolb0/32/380_2.png) [@hrcolb0](https://forums.plex.tv/u/hrcolb0)\
**Post date:** [February 23, 2011, 5:37pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/119 "2011-02-23T17:37:52Z")

</div>

pooplooser,  
  
  
  
Google some stuff. You’re being given a huge leg up over a lot of people when they first program their first plugins. If someone mentions some command you don’t know, try typing it in google. Maybe even add the word “example” to your search, like [this](http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=python+string+split+commands#sclient=psy&hl=en&source=hp&q=python+string+split+command+example&aq=f&aqi=&aql=&oq=&pbx=1&bav=on.1,or.&fp=e87e3794f75b2bfa). While I understand it is a lot easier to ask for a more specific example on the boards, there are a lot of well thought out and written examples of some things you are asking on the web. Don’t be afraid to search, or that you may not understand what is out there. That’s ok. Give it a go though, and you’ll gain a better understanding of what you’re writing and be able to make plugins faster.

---

<div class="post-metadata">

**Author:** ![jwray](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/jwray/32/99832_2.png) [@jwray](https://forums.plex.tv/u/jwray)\
**Post date:** [February 23, 2011, 5:42pm UTC](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638/120 "2011-02-23T17:42:45Z")

</div>

[3. An Informal Introduction to Python — Python 3.12.1 documentation](http://docs.python.org/tutorial/introduction.html#strings)  
  
[http://www.devshed.com/c/a/Python/String-Manipulation/](http://www.devshed.com/c/a/Python/String-Manipulation/)

```auto

<br />
year = item.xpath('./td[@colspan="2"]/child::text()[position()=2]').strip()<br />
tokens = year.split()<br />
actualYearWithBrackets = tokens[0]<br />
actualYear = actualYearWithBrackets [1:-1]<br />

```

  
  
I'm sure you could simplify this. I'll leave the second part to you since you'll understand python strings after reading that documentation.  
  

> [@](#):
>
> that sounds great, only that i have even less knowledge of python than of xpath.  
> could you give me an example code?

[Previous page](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638.md?page=5)

[Next page](https://forums.plex.tv/t/need-basic-help-with-plugin-writing/7638.md?page=7)
