Python正则表达式的可能性

时间:2015-09-21 07:45:56

标签: python regex kodi

我有一个很大的json字符串,我试图用python解析。 基本上这个字符串包含iptv流。

我正在尝试为xmbc / kodi开发插件。

字符串示例:http://pastebin.com/SmEGXaN9

我还需要将流分为两个不同的:LIVE和VOD,我可以用正则表达式吗?

在json LIVE中有" live":" 1"和VOD有"直播":0"

我尝试捕捉"直播":"(。+?)"然后做if语句来查看它的LIVE还是VOD,但有时它不会返回任何" live":" 1"出于某种原因。

是否可以使用正则表达式而不是if语句拆分流?

这是LIVE抓取的功能:

def LIVE(url):
        xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_TITLE )
        vidlocation=('%s:%s/live/%s/%s/'%(site,port,usern,passw))
        link = OPEN_URL(url) #GET DATA FROM JSON API
        match=re.compile('{"name":"(.+?)","stream_id":"(.+?)","stream_icon":"(.+?)","live":"(.+?)"').findall(link)
        for name,streamid,iconimage,live in match:
           if "1" in live:
               addLink('%s'%(live),'%s%s.ts'%(vidlocation,streamid),'%s'%(iconimage).replace("\/","/"),'')

对于LIVE流,我需要检索3件事: name,stream_id,stream_icon

这是VOD抓取的功​​能:

def VOD(url):
        vidlocation=('%s:%s/movie/%s/%s/'%(site,port,usern,passw))
        link = OPEN_URL(url)
        match=re.compile('{"name":"(.+?)","stream_id":"(.+?)","live":"(.+?)","container_extension":"(.+?)","stream_icon":"(.+?)"').findall(link)
        for name,streamid,live,container,iconimage in match:
            if not "1" in live:
                addLink3('%s'%(name),'%s%s.%s'%(vidlocation,streamid,container),'','%s'%(iconimage).replace("\/","/"),'','PLOT')                
            else:
                #novod

对于VOD流,我需要捕获4件事: name,stream_id,container_extension; stream_icon

有人可以帮助我根据自己的需要制作正确的tregex,还请详细说明它的作用和方式。 Python正则表达式文档对我来说非常复杂。

0 个答案:

没有答案