need basic help with plugin writing

for example



content equals




item in your code equals


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

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?)

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?


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.

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:


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


got it to work. :slight_smile: thank you!



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




thank you!!

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/init.py#L366

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? :slight_smile: thanks!


??? 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.

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

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:



<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)


  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!

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



//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

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



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 />


How about something like:



<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 />


thank you! worked!



2 more questions



1. i’m having a lil bit of trouble with replacing in here.



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



year turns out to be f.ex.

[' [1964] by ']



instead of

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:


<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:

			<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:


<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:


<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.

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

that sounds great, only that i have even less knowledge of python than of xpath.

could you give me an example code?

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. 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.

3. An Informal Introduction to Python — Python 3.12.1 documentation

http://www.devshed.com/c/a/Python/String-Manipulation/



<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.