# encoding: utf-8
from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..utils import int_or_none


class MusicPlayOnIE(InfoExtractor):
    _VALID_URL = r'https?://(?:.+?\.)?musicplayon\.com/play(?:-touch)?\?(?:v|pl=100&play)=(?P<id>\d+)'

    _TEST = {
        'url': 'http://en.musicplayon.com/play?v=433377',
        'info_dict': {
            'id': '433377',
            'ext': 'mp4',
            'title': 'Rick Ross - Interview On Chelsea Lately (2014)',
            'description': 'Rick Ross Interview On Chelsea Lately',
            'duration': 342,
            'uploader': 'ultrafish',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
    }

    def _real_extract(self, url):
        mobj = re.match(self._VALID_URL, url)
        video_id = mobj.group('id')

        page = self._download_webpage(url, video_id)

        title = self._og_search_title(page)
        description = self._og_search_description(page)
        thumbnail = self._og_search_thumbnail(page)
        duration = self._html_search_meta('video:duration', page, 'duration', fatal=False)
        view_count = self._og_search_property('count', page, fatal=False)
        uploader = self._html_search_regex(
            r'<div>by&nbsp;<a href="[^"]+" class="purple">([^<]+)</a></div>', page, 'uploader', fatal=False)

        formats = [
            {
                'url': 'http://media0-eu-nl.musicplayon.com/stream-mobile?id=%s&type=.mp4' % video_id,
                'ext': 'mp4',
            }
        ]

        manifest = self._download_webpage(
            'http://en.musicplayon.com/manifest.m3u8?v=%s' % video_id, video_id, 'Downloading manifest')

        for entry in manifest.split('#')[1:]:
            if entry.startswith('EXT-X-STREAM-INF:'):
                meta, url, _ = entry.split('\n')
                params = dict(param.split('=') for param in meta.split(',')[1:])
                formats.append({
                    'url': url,
                    'ext': 'mp4',
                    'tbr': int(params['BANDWIDTH']),
                    'width': int(params['RESOLUTION'].split('x')[1]),
                    'height': int(params['RESOLUTION'].split('x')[-1]),
                    'format_note': params['NAME'].replace('"', '').strip(),
                })

        return {
            'id': video_id,
            'title': title,
            'description': description,
            'thumbnail': thumbnail,
            'uploader': uploader,
            'duration': int_or_none(duration),
            'view_count': int_or_none(view_count),
            'formats': formats,
        }

@@ -56,19 +56,8 @@
 ls%0A%0A
-import re%0A%0A
 from
@@ -86,16 +86,53 @@
 tractor%0A
+from ..compat import compat_urlparse%0A
 from ..u
@@ -143,16 +143,22 @@
  import 
+(%0A    
 int_or_n
@@ -160,16 +160,53 @@
 _or_none
+,%0A    js_to_json,%0A    mimetype2ext,%0A)
 %0A%0A%0Aclass
@@ -404,32 +404,83 @@
 play?v=433377',%0A
+        'md5': '00cdcdea1726abdf500d1e7fd6dd59bb',%0A
         'info_di
@@ -764,219 +764,82 @@
     
-    'params': %7B%0A            # m3u8 download%0A            'skip_download': True,%0A        %7D,%0A    %7D%0A%0A    def _real_extract(self, url):%0A        mobj = re.match(self._VALID_URL, url)%0A        video_id = mobj.group('id'
+%7D%0A%0A    def _real_extract(self, url):%0A        video_id = self._match_id(url
 )%0A%0A 
@@ -1374,586 +1374,191 @@
     
-formats = %5B%0A            %7B%0A                'url': 'http://media0-eu-nl.musicplayon.com/stream-mobile?id=%25s&type=.mp4' %25 video_id,%0A                'ext': 'mp4',%0A            %7D%0A        %5D%0A%0A        manifest = self._download_webpage(%0A            'http://en.musicplayon.com/manifest.m3u8?v=%25s' %25 video_id
+sources = self._parse_json(%0A            self._search_regex(r'setup%5C%5B%5C'_sources%5C'%5C%5D%5Cs*=%5Cs*(%5B%5E;%5D+);', page
 , 
+'
 video
-_id, 'Downloading manifest')%0A%0A        for entry in manifest.split('#')%5B1:%5D:%0A            if entry.startswith('EXT-X-STREAM-INF:'):%0A                meta, url, _ = entry.split('%5Cn')%0A                params = dict(param.split('=') for param in meta.split(',')%5B1:%5D)%0A       
+ sources'),%0A            video_id, transform_source=js_to_json)%0A
         
  for
@@ -1557,34 +1557,21 @@
     
- 
 formats
-.append(%7B%0A        
+ = %5B%7B%0A
     
@@ -1589,336 +1589,189 @@
 l': 
-url,%0A                    'ext': 'mp4',%0A                    'tbr': int(params%5B'BANDWIDTH'%5D),%0A                    'width': int(params%5B'RESOLUTION'%5D.split('x')%5B1%5D),%0A                    'height': int(params%5B'RESOLUTION'%5D.split('x')%5B-1%5D),%0A                    'format_note': params%5B'NAME'%5D.replace('%22', '').strip(),%0A                %7D)
+compat_urlparse.urljoin(url, source%5B'src'%5D),%0A            'ext': mimetype2ext(source.get('type')),%0A            'format_note': source.get('data-res'),%0A        %7D for source in sources%5D
 %0A%0A  

Fix extraction (closes #9222)