0

I have big json string, that i am trying to parse with python. Basicly this string contains iptv streams.

I am trying to develope addon for xmbc/kodi.

String example: http://pastebin.com/SmEGXaN9

I also need to split streams to 2 different ones: LIVE and VOD, can i do this with regex ?

In json LIVE streams have "live":"1" and VOD has "live":0"

I try to capture "live":"(.+?)" and then do if statement to see if its LIVE or VOD, but sometimes it doesnt return any "live":"1" for some reason.

Is it possible to split streams up with regex instead of if statement?

Here is function for LIVE grabbing:

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("\/","/"),'')

For LIVE streams i need to retrive 3 things: name,stream_id,stream_icon

Here is function for VOD grabbing:

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

For VOD streams i need to capture 4 things: name,stream_id,container_extension;stream_icon

Can someone help me build correct tregex for my need, and also please explain exactly what and how it does work. Python regex documentation is pretty complex for me.

user2033139
  • 573
  • 2
  • 10
  • 21
  • 5
    I read the first line, and now I wonder why you don't use the built-in Python `json` module? –  Sep 21 '15 at 07:49
  • Echo above. Json is not a regular language. Strictly speaking it is not parsable with regex, not without assumptions on structure and depth, plus tons of complex logic. And complex logic doesn't make you cool. – Patrick the Cat Sep 21 '15 at 08:50
  • Thank you for suggestion, i will go and try with json module. Sounds more reasonable. – user2033139 Sep 21 '15 at 12:33
  • Since your file size is about 220 Mo, I think it's a better idea to use `ijson` (https://pypi.python.org/pypi/ijson) instead of the json module. `ijson` is an event based parser, so your file is read as a stream and is not fully loaded in the memory. – Casimir et Hippolyte Sep 21 '15 at 14:52

0 Answers0