"""Plugin for panda.tv by Fat Deer"""

import re
import types
from livestreamer.plugin import Plugin
from livestreamer.plugin.api import http, validate
from livestreamer.stream import HTTPStream

ROOM_API = "http://www.panda.tv/api_room?roomid="
SD_URL_PATTERN = "http://pl{0}.live.panda.tv/live_panda/{1}.flv"
HD_URL_PATTERN = "http://pl{0}.live.panda.tv/live_panda/{1}_mid.flv"
# I don't know ordinary-definition url pattern, sorry for ignore it.
OD_URL_PATTERN = "http://pl{0}.live.panda.tv/live_panda/{1}_mid.flv"

_url_re = re.compile("http(s)?://(\w+.)?panda.tv/(?P<channel>[^/&?]+)")

_room_schema = validate.Schema(
        {
            "data": validate.any(
                validate.text,
                dict,
                {
                    "videoinfo": validate.any(
                        validate.text,
                        {
                            "room_key": validate.text,
                            "plflag": validate.text,
                            "status": validate.text,
                            "stream_addr": {
                                "HD": validate.text,
                                "OD": validate.text,
                                "SD": validate.text
                            }
                        }
                    )
                }
            )
        },
        validate.get("data"))


class pandatv(Plugin):
    @classmethod
    def can_handle_url(self, url):
        return _url_re.match(url)

    def _get_streams(self):
        match = _url_re.match(self.url)
        channel = match.group("channel")

        url = ROOM_API + channel
        res = http.get(url)
        data = http.json(res, schema=_room_schema)
        if type(data) is not types.DictionaryType or data['videoinfo']['status'] != "2":
            return

        streams = {}
        plflag = data['videoinfo']['plflag'].split('_')[1]
        room_key = data['videoinfo']['room_key']

        # SD(Super high Definition) has higher quality than HD(High Definition) which
        # conflict with existing code, use ehq and hq instead.
        if data['videoinfo']['stream_addr']['SD'] == '1':
            streams['ehq'] = HTTPStream(self.session, SD_URL_PATTERN.format(plflag, room_key))

        if data['videoinfo']['stream_addr']['HD'] == '1':
            streams['hq'] = HTTPStream(self.session, HD_URL_PATTERN.format(plflag, room_key))

        return streams

__plugin__ = pandatv


@@ -1708,56 +1708,321 @@
  if 
-type(data) is not types.DictionaryType or data%5B'
+not isinstance(data, dict):%0A            self.logger.error(%22Please Check PandaTV Room API%22)%0A            return%0A    %0A        videoinfo = data.get('videoinfo')%0A%0A        if not videoinfo or not videoinfo.get('status'):%0A            self.logger.error(%22Please Check PandaTV Room API%22)%0A            return%0A    %0A        if 
 vide
@@ -2030,19 +2030,21 @@
 info
-'%5D%5B
+.get(
 'status'
 %5D !=
@@ -2043,17 +2043,70 @@
 tus'
-%5D
+)
  != 
-%222%22:
+'2':%0A            self.logger.info(%22Channel offline now!%22)
 %0A   
@@ -2160,22 +2160,16 @@
 lflag = 
-data%5B'
 videoinf
@@ -2173,20 +2173,172 @@
 info
-'%5D%5B'plflag'%5D
+.get('plflag')%0A        if not plflag or not '_' in plflag:%0A            self.logger.error(%22Please Check PandaTV Room API%22)%0A            return%0A        plflag = plflag
 .spl
@@ -2367,22 +2367,16 @@
 m_key = 
-data%5B'
 videoinf
@@ -2376,19 +2376,21 @@
 ideoinfo
-'%5D%5B
+.get(
 'room_ke
@@ -2391,17 +2391,17 @@
 oom_key'
-%5D
+)
 %0A%0A      
@@ -2555,49 +2555,104 @@
     
-if data%5B'videoinfo'%5D%5B'stream_addr'%5D%5B
+stream_addr = videoinfo.get('stream_addr')%0A    %0A        if stream_addr and stream_addr.get(
 'SD'
-%5D
+)
  == 
@@ -2767,27 +2767,24 @@
  if 
-data%5B'videoinfo'%5D%5B'
+stream_addr and 
 stre
@@ -2794,16 +2794,18 @@
 addr
-'%5D%5B
+.get(
 'HD'
-%5D
+)
  == 

Update pandatv.py