# play video using regex

**URL:** <https://forums.plex.tv/t/play-video-using-regex/3283>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [January 1, 2010, 1:32pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283 "2010-01-01T13:32:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Veld](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@David\_Veld](https://forums.plex.tv/u/David_Veld)\
**Post date:** [January 1, 2010, 1:32pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/1 "2010-01-01T13:32:18Z")

</div>

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?

```auto

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

```

---

<div class="post-metadata">

**Author:** ![sansnipple](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@sansnipple](https://forums.plex.tv/u/sansnipple)\
**Post date:** [January 1, 2010, 5:10pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/2 "2010-01-01T17:10:34Z")

</div>

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

```auto

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

```

  
to:  

```auto

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

```

  
and the playvideo function to:  

```auto

<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

---

<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:** [January 1, 2010, 6:16pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/3 "2010-01-01T18:16:56Z")

</div>

> [@](#):
>
> you could just move the whole playvideo function up inside the for loop above

  
  
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.

---

<div class="post-metadata">

**Author:** ![David\_Veld](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@David\_Veld](https://forums.plex.tv/u/David_Veld)\
**Post date:** [January 1, 2010, 7:01pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/4 "2010-01-01T19:01:33Z")

</div>

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

```auto

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:  
  

```auto

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?

---

<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:** [January 1, 2010, 7:39pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/5 "2010-01-01T19:39:35Z")

</div>

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

```auto

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

```

  
  
Jonny

---

<div class="post-metadata">

**Author:** ![sansnipple](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@sansnipple](https://forums.plex.tv/u/sansnipple)\
**Post date:** [January 1, 2010, 7:40pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/6 "2010-01-01T19:40:15Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![David\_Veld](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@David\_Veld](https://forums.plex.tv/u/David_Veld)\
**Post date:** [January 1, 2010, 11:17pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/7 "2010-01-01T23:17:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![David\_Veld](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@David\_Veld](https://forums.plex.tv/u/David_Veld)\
**Post date:** [January 2, 2010, 12:28am UTC](https://forums.plex.tv/t/play-video-using-regex/3283/8 "2010-01-02T00:28:00Z")

</div>

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

```auto

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

---

<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:** [January 2, 2010, 3:03am UTC](https://forums.plex.tv/t/play-video-using-regex/3283/9 "2010-01-02T03:03:34Z")

</div>

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.

```auto

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

```

---

<div class="post-metadata">

**Author:** ![David\_Veld](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@David\_Veld](https://forums.plex.tv/u/David_Veld)\
**Post date:** [January 2, 2010, 3:31pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/10 "2010-01-02T15:31:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 8:34pm UTC](https://forums.plex.tv/t/play-video-using-regex/3283/11 "2019-12-20T20:34:46Z")

</div>

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