play video using regex

I created the following to play a video using regex. After some trials it still does not work. Does anyone have any suggestions about what seems to be missing in order to make this code work?



def Video(sender, url):<br />
<br />
  dir = MediaContainer(title3=sender.itemTitle, art=R(ART), viewGroup="InfoList")<br />
<br />
  videos = XML.ElementFromURL(url, isHTML=True, errors='ignore').xpath(XPATH_VIDEOS)<br />
  for content in videos:<br />
    title = content.xpath("./a/span")[0].text<br />
    thumb = content.xpath("./a/img")[0].get('src')<br />
    summary = content.xpath("./a")[0].get('title')<br />
    url = content.xpath("./a")[0].get('href')<br />
    dir.Append(Function(VideoItem(PlayVideo, title=title, summary=summary, thumb=thumb), url=url))<br />
<br />
  return dir<br />
<br />
####################################################################################################<br />
<br />
def PlayVideo(sender, url):<br />
        <br />
        video_link = HTTP.Request(url)<br />
        file_name_link = re.search("sGlobalFileName='(.+?)';", video_link)<br />
        file_path_link = re.search("sGlobalContentFilePath='(.+?)';", video_link)<br />
        file_path = file_path_link.group(1)<br />
        file_name = file_name_link.group(1)<br />
        <br />
        total_video_link = 'http://media1.break.com/dnet/media/' + file_path '/' + file_name '.flv'<br />
        <br />
        dir.Append(VideoItem(total_video_link))

you could just move the whole playvideo function up inside the for loop above





or i guess if it needs to stay separate for whatever reason, try something like this this:



change the line


dir.Append(Function(VideoItem(PlayVideo, title=title, summary=summary, thumb=thumb), url=url))<br />



to:

dir.Append(PlayVideo(url, title, summary, thumb))


and the playvideo function to:

<br />
def PlayVideo(url, title, summary, thumb):<br />
        <br />
        video_link = HTTP.Request(url)<br />
        file_name_link = re.search("sGlobalFileName='(.+?)';", video_link)<br />
        file_path_link = re.search("sGlobalContentFilePath='(.+?)';", video_link)<br />
        file_path = file_path_link.group(1)<br />
        file_name = file_name_link.group(1)<br />
        <br />
        total_video_link = 'http://media1.break.com/dnet/media/' + file_path '/' + file_name '.flv'<br />
        <br />
        return VideoItem(total_video_link, title=title, summary=summary, thumb=thumb)<br />



theres probably half a dozen other valid ways to do it too though


I wouldn't recommend that, because then a lot of pages need to be opened and parsed when building the initial list. Opening and parsing one page to find which video to play is better imo.

I just tried the second option (below) and it did not work.



def PlayVideo(url, title, summary, thumb):<br />
        <br />
        video_link = HTTP.Request(url)<br />
        file_name_link = re.search("sGlobalFileName='(.+?)';", video_link)<br />
        file_path_link = re.search("sGlobalContentFilePath='(.+?)';", video_link)<br />
        file_path = file_path_link.group(1)<br />
        file_name = file_name_link.group(1)<br />
        <br />
        total_video_link = 'http://media1.break.com/dnet/media/' + file_path '/' + file_name '.flv'<br />
        <br />
        return VideoItem(total_video_link, title=title, summary=summary, thumb=thumb



After that I made some adjustments in the total_video_link and then the plugin worked but did not play the video.

The log gave the following error:

Calling named function 'PlayVideo'
18:47:00.657282: com.plexapp.plugins.break : (Framework) An exception happened:
Traceback (most recent call last):
File "/Users/davidvanineveld/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/1/Python/PMS/Plugin.py", line 640, in __call
return function(*args, **kwargs)
TypeError: PlayVideo() takes exactly 5 non-keyword arguments (2 given)

18:47:00.657351: com.plexapp.plugins.break : (Framework) Request not handled by plug-in

18:47:01.899191: com.plexapp.plugins.break : (Framework) Saved shared HTTP data


The new code is:


def Video(sender, url):<br />
  <br />
  dir = MediaContainer(title3=sender.itemTitle, art=R(ART), viewGroup="InfoList")<br />
  <br />
  videos = XML.ElementFromURL(url, isHTML=True, errors='ignore').xpath(XPATH_VIDEOS)<br />
  for content in videos:<br />
    title = content.xpath("./a/span")[0].text<br />
    thumb = content.xpath("./a/img")[0].get('src')<br />
    summary = content.xpath("./a")[0].get('title')<br />
    url = content.xpath("./a")[0].get('href')<br />
    dir.Append(Function(VideoItem(PlayVideo, title=title, summary=summary, thumb=thumb), url=url))<br />
  <br />
  return dir<br />
<br />
#################################################################<br />
<br />
def PlayVideo(sender, url):<br />
	<br />
	video_link = HTTP.Request(url)<br />
	file_name_link = re.search("sGlobalFileName='(.+?)';", video_link)<br />
	file_path_link = re.search("sGlobalContentFilePath='(.+?)';", video_link)<br />
	file_path = file_path_link.group(1)<br />
	file_name = file_name_link.group(1)<br />
	slash = '/'<br />
	flv = '.flv'<br />
	total_video_link = BREAK_MEDIA + file_path  + slash + file_name + flv<br />
	return VideoItem(total_video_link)




What seems to be the problem here. Do you think regex is not reading the content properly or is there something wrong with the VideoItem?

Hi David,



Sander gave you the correct syntax in the other thread. Your last line should be:



<br />
return Redirect(total_video_link)<br />




Jonny

for my method, you;d need to make both changes i mentioned, it looks like you forgot to change the function call to dir.Append(PlayVideo(url, title, summary, thumb)) , giving you the the arguments error traceback. if you want to do it the way you have it in the new code you posted, you need to use the return Redirect() that sander mentioned in the other thread (probably the cleanest method)

Wow… I feel so stupid. I just left the computer for a couple of hours alone and tried to get my concentration back and now I returned to it and read your messages it all sees so clear.



Thank you for your support!

The code seems to be OK right now, but as a last error the log is telling that their is a AttributeError: ‘NoneType’ object has no attribute ‘group’.



This error is targeted at the destination page were it should extract the value of the sGlobalContentPath= and sGlobalFileName= in the source code.



Log Error:



Calling named function ‘PlayVideo’

01:40:03.157265: com.plexapp.plugins.break : (Framework) Received gzipped response from http://www.break.com/index/travis-pastrana-jumps-267-feet-in-rally-car.html

01:40:03.192385: com.plexapp.plugins.break : (Framework) An exception happened:

Traceback (most recent call last):

File “/Users/davidvanineveld/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/1/Python/PMS/Plugin.py”, line 640, in __call

return function(*args, **kwargs)

File “/Users/davidvanineveld/Library/Application Support/Plex Media Server/Plug-ins/Break.bundle/Contents/Code/init.py”, line 100, in PlayVideo

total_video_link = ‘http://media1.break.com/dnet/media/’ + link.group(1) + ‘/’ + link.group(2) + ‘.flv’

AttributeError: ‘NoneType’ object has no attribute ‘group’





Although the error says that there is no such value it actually is as I show below. Why the error?


Travis Pastrana Jumps 267 Feet In Rally Car Video ..........


The final code is now

def Video(sender, url):<br />
<br />
  dir = MediaContainer(title3=sender.itemTitle, art=R(ART), viewGroup="InfoList")<br />
<br />
  videos = XML.ElementFromURL(url, isHTML=True, errors='ignore').xpath(XPATH_VIDEOS)<br />
  for content in videos:<br />
    title = content.xpath("./a/span")[0].text<br />
    thumb = content.xpath("./a/img")[0].get('src')<br />
    summary = content.xpath("./a")[0].get('title')<br />
    url = content.xpath("./a")[0].get('href')<br />
    dir.Append(Function(VideoItem(PlayVideo, title=title, summary=summary, thumb=thumb), url=url))<br />
<br />
  return dir<br />
<br />
####################################################################################################<br />
<br />
def PlayVideo(sender, url):<br />
        <br />
  video_link = HTTP.Request(url)<br />
  link = re.search("sGlobalContentFilePath='(.+?)';.+sGlobalFileName='(.+?)';", video_link, re.DOTALL)<br />
  total_video_link = 'http://media1.break.com/dnet/media/' + link.group(1) + '/' + link.group(2) + '.flv'<br />
        <br />
  return Redirect(total_video_link)



With special thanks to Billy Joe, Jonny Wray and Sander1

The problem is the order in which sGlobalFileName and sGlobalContentFilePath are listed in the javascript. In this error message and on the source page sGlobalFileName comes before sGlobalContentFilePath, but in the example you posted in the other thread sGlobalFileName came after sGlobalContentFilePath.

If the order in which both variables are listed is different across the website, it’s best to change back to two seperate regexes. That way the correct pieces of text will always be captured no matter what the order in the javascript is.



<br />
def PlayVideo(sender, url):<br />
<br />
        video_link = HTTP.Request(url)<br />
        file_name = re.search("sGlobalFileName='(.+?)';", video_link).group(1)<br />
        file_path = re.search("sGlobalContentFilePath='(.+?)';", video_link).group(1)<br />
        <br />
        total_video_link = 'http://media1.break.com/dnet/media/' + file_path + '/' + file_name + '.flv'<br />
        return Redirect(total_video_link)<br />


Yes, it is all working now. thank you all for your support.

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