How to access viewCount attribute via python agent?

I noticed the xml metadata object that is created when media is scanned includes the viewCount object, but when I try to access it in my python agent, I get "'TV_Show' object has no attribute 'viewCount'"

 

Is there an example anywhere of how to access the viewCount attribute from within an agent?

Getting the xml file using a simple GET request and parse with ElementTree lib should suffice. You do not provide much detail on your python agent, what you want to achieve and how you currently do it. Hard to guide you.

Thanks.  So is the only way to access viewCount via the XML and not via the media or metadata object?

Good question on the agent.  I've been helping to develop an agent to pull in TV Shows recorded via SageTV (using SageTV's JSON API to access metadata).  It is working very well, we are just working to fine tune some things, one of which is syncing the "watched" status between Plex and SageTV.  e.g. if you watch something in Plex, the next time the agent runs, it would see the viewCount>0 and then set the watched status back in Sage.

More here if you're interested: https://code.google.com/p/sagetv-for-plexmediacenter/downloads/list

Not sure if that is the only way but that is definitely one way. Also, I have just checked but the viewCount attribute only appears in the xml view when an item has been viewed at least once.

So maybe your current error is because you don't consider that "no attribute" = 0 viewed 

Not sure if that is the only way but that is definitely one way. Also, I have just checked but the viewCount attribute only appears in the xml view when an item has been viewed at least once.

So maybe your current error is because you don't consider that "no attribute" = 0 viewed 

Good point, but I did notice that too and I've been testing on only items that have viewCounts to at least get the base case working.  I'll try the XML parsing route using the ElementTree library and see how it goes.

That would be pretty much safe to assume so for the viewCount that it only appears when viewed once and the behavior is the same whenever you want the viewOffset that gives the position in the video and also tells you whether the video is currently being watched or not.

Also, I found that working XML for Plex to be quite pleasant. It's straightforward and you get pretty much all of the information you need as it is meant to serve remote points.

Also, if you start playing with XML, I would strongly suggest to rely on the "requests" library that makes HTTP requests very easy to do. Not saying, using urllib2 and the such is much harder but you get a pleasant level of abstraction.

That would be pretty much safe to assume so for the viewCount that it only appears when viewed once and the behavior is the same whenever you want the viewOffset that gives the position in the video and also tells you whether the video is currently being watched or not.

Also, I found that working XML for Plex to be quite pleasant. It's straightforward and you get pretty much all of the information you need as it is meant to serve remote points.

I'm doing something simple like below... is there a better approach?

xmlURL = PLEX_HOST + '/library/metadata/' + id
response = urllib.urlopen(xmlURL)
xmlContent = response.read()
xmlTree = etree.fromstring(xmlContent)
videoNode = xmlTree.find('Video')
plexViewCount = videoNode.get('viewCount', 0)

Nope, sounds perfect to me.

One more question for you while I have you.  Looks like the XML parsing/reading is working great, but what if I want to update or write back to the XML?  Is there a way I can update the viewOffset attribute for example?

I don't think so. From what I have seen, the viewOffset is updated in a recurrent manner whenever the video is being played.

Moreover, I don't see any reason for the Plex developer to allow the modification of this value whenever a video is not being played as its purpose is to keep progression synced across all clients.

Now, if you start playing with the http urls. Use Plex/Web and Chrome in dev mode and observe what URLs are being requested whenever you do something. When my video is playing, I get the following. A dirty workaround for your case (if it works) is to try to call that URL and modify the time value with the OffsetValue you want to set and see if that updates the value or not.

I couldn't test because I can't dev right now and calling that URL directly got me a 400 error (possibly because I don't have the right headers set).

  1. Request URL:
  2. Request Method:
    GET
  3. Status Code:
     
    200 OK
  4. Request Headersview source
    1. Accept:
      text/plain, */*; q=0.01
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      fr,en-US;q=0.8,en;q=0.6
    4. Connection:
      keep-alive
    5. Cookie:
      __utma=96992031.1018365403.1383497835.1383497835.1386869632.2; __utmb=96992031.2.10.1386869632; __utmc=96992031; __utmz=96992031.1383497835.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
    6. Host:
      127.0.0.1:32400
    7. Referer:
    8. User-Agent:
      Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
    9. X-Plex-Client-Identifier:
      sxa796mszv9uow29
    10. X-Plex-Device:
      Mac
    11. X-Plex-Device-Name:
      Plex/Web (Chrome)
    12. X-Plex-Platform:
      Chrome
    13. X-Plex-Platform-Version:
      31
    14. X-Plex-Product:
      Web Client
    15. X-Plex-Version:
      1.2.22

Yeah I'm definitely not going there.

Before jumping to a solution, ultimately I was hoping for a way to be able to update the viewOffset in Plex.  If XML updating isn't possible, are there any API calls or other things I can do from within a python agent to do this?  I'm sure Plex has a way, just not sure if it's a private solution or something they publicly expose.

Well. I tested this morning. With the proper headings, at least I got a 200 response code but it doesn't update the viewOffset. I have just asked to Tobias and he confirmed me that it can only be updated when playing a video.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.