<s> from __future__ import unicode_literals <EOL> import time <EOL> import math <EOL> import os . path <EOL> import re <EOL> from . common import InfoExtractor <EOL> from . . compat import ( <EOL> compat_html_parser , <EOL> compat_urllib_parse , <EOL> compat_urllib_request , <EOL> compat_urlparse , <EOL> ) <EOL> from . . utils import ExtractorError <EOL> class GroovesharkHtmlParser ( compat_html_parser . HTMLParser ) : <EOL> def __init__ ( self ) : <EOL> self . _current_object = None <EOL> self . objects = [ ] <EOL> compat_html_parser . HTMLParser . __init__ ( self ) <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> attrs = dict ( ( k , v ) for k , v in attrs ) <EOL> if tag == '<STR_LIT:object>' : <EOL> self . _current_object = { '<STR_LIT>' : attrs , '<STR_LIT>' : [ ] } <EOL> elif tag == '<STR_LIT>' : <EOL> self . _current_object [ '<STR_LIT>' ] . append ( attrs ) <EOL> def handle_endtag ( self , tag ) : <EOL> if tag == '<STR_LIT:object>' : <EOL> self . objects . append ( self . _current_object ) <EOL> self . _current_object = None <EOL> @ classmethod <EOL> def extract_object_tags ( cls , html ) : <EOL> p = cls ( ) <EOL> p . feed ( html ) <EOL> p . close ( ) <EOL> return p . objects <EOL> class GroovesharkIE ( InfoExtractor ) : <EOL> _VALID_URL = r'<STR_LIT>' <EOL> _TEST = { <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> } <EOL> do_playerpage_request = True <EOL> do_bootstrap_request = True <EOL> def _parse_target ( self , target ) : <EOL> uri = compat_urlparse . urlparse ( target ) <EOL> hash = uri . fragment [ <NUM_LIT:1> : ] . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> token = os . path . basename ( hash . rstrip ( '<STR_LIT:/>' ) ) <EOL> return ( uri , hash , token ) <EOL> def _build_bootstrap_url ( self , target ) : <EOL> ( uri , hash , token ) = self . _parse_target ( target ) <EOL> query = '<STR_LIT>' % ( compat_urllib_parse . quote ( hash , safe = '<STR_LIT>' ) , self . ts ) <EOL> return ( compat_urlparse . urlunparse ( ( uri . scheme , uri . netloc , '<STR_LIT>' , None , query , None ) ) , token ) <EOL> def _build_meta_url ( self , target ) : <EOL> ( uri , hash , token ) = self . _parse_target ( target ) <EOL> query = '<STR_LIT>' % ( compat_urllib_parse . quote ( hash , safe = '<STR_LIT>' ) , self . ts ) <EOL> return ( compat_urlparse . urlunparse ( ( uri . scheme , uri . netloc , '<STR_LIT>' , None , query , None ) ) , token ) <EOL> def _build_stream_url ( self , meta ) : <EOL> return compat_urlparse . urlunparse ( ( '<STR_LIT:http>' , meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' , None , None , None ) ) <EOL> def _build_swf_referer ( self , target , obj ) : <EOL> ( uri , _ , _ ) = self . _parse_target ( target ) <EOL> return compat_urlparse . urlunparse ( ( uri . scheme , uri . netloc , obj [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] , None , None , None ) ) <EOL> def _transform_bootstrap ( self , js ) : <EOL> return re . split ( '<STR_LIT>' , js ) [ <NUM_LIT:0> ] . split ( '<STR_LIT>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . strip ( ) . rstrip ( '<STR_LIT:;>' ) <EOL> def _transform_meta ( self , js ) : <EOL> return js . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . rstrip ( '<STR_LIT:;>' ) <EOL> def _get_meta ( self , target ) : <EOL> ( meta_url , token ) = self . _build_meta_url ( target ) <EOL> self . to_screen ( '<STR_LIT>' % meta_url ) <EOL> headers = { '<STR_LIT>' : compat_urlparse . urldefrag ( target ) [ <NUM_LIT:0> ] } <EOL> req = compat_urllib_request . Request ( meta_url , headers = headers ) <EOL> res = self . _download_json ( req , token , <EOL> transform_source = self . _transform_meta ) <EOL> if '<STR_LIT>' not in res : <EOL> raise ExtractorError ( <EOL> '<STR_LIT>' ) <EOL> if res [ '<STR_LIT>' ] is None : <EOL> raise ExtractorError ( <EOL> '<STR_LIT>' , <EOL> expected = True ) <EOL> return res [ '<STR_LIT>' ] <EOL> def _get_bootstrap ( self , target ) : <EOL> ( bootstrap_url , token ) = self . _build_bootstrap_url ( target ) <EOL> headers = { '<STR_LIT>' : compat_urlparse . urldefrag ( target ) [ <NUM_LIT:0> ] } <EOL> req = compat_urllib_request . Request ( bootstrap_url , headers = headers ) <EOL> res = self . _download_json ( req , token , fatal = False , <EOL> note = '<STR_LIT>' , <EOL> errnote = '<STR_LIT>' , <EOL> transform_source = self . _transform_bootstrap ) <EOL> return res <EOL> def _get_playerpage ( self , target ) : <EOL> ( _ , _ , token ) = self . _parse_target ( target ) <EOL> webpage = self . _download_webpage ( <EOL> target , token , <EOL> note = '<STR_LIT>' , <EOL> errnote = '<STR_LIT>' , <EOL> fatal = False ) <EOL> if webpage is not None : <EOL> error_msg = self . _html_search_regex ( <EOL> r'<STR_LIT>' , webpage , <EOL> '<STR_LIT>' , default = None ) <EOL> if error_msg is not None : <EOL> error_msg = error_msg . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) <EOL> raise ExtractorError ( '<STR_LIT>' % error_msg ) <EOL> if webpage is not None : <EOL> o = GroovesharkHtmlParser . extract_object_tags ( webpage ) <EOL> return ( webpage , [ x for x in o if x [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] == '<STR_LIT>' ] ) <EOL> return ( webpage , None ) <EOL> def _real_initialize ( self ) : <EOL> self . ts = int ( time . time ( ) * <NUM_LIT:1000> ) <EOL> def _real_extract ( self , url ) : <EOL> ( target_uri , _ , token ) = self . _parse_target ( url ) <EOL> swf_referer = None <EOL> if self . do_playerpage_request : <EOL> ( _ , player_objs ) = self . _get_playerpage ( url ) <EOL> if player_objs is not None : <EOL> swf_referer = self . _build_swf_referer ( url , player_objs [ <NUM_LIT:0> ] ) <EOL> self . to_screen ( '<STR_LIT>' % swf_referer ) <EOL> if self . do_bootstrap_request : <EOL> bootstrap = self . _get_bootstrap ( url ) <EOL> self . to_screen ( '<STR_LIT>' % bootstrap [ '<STR_LIT>' ] ) <EOL> meta = self . _get_meta ( url ) <EOL> stream_url = self . _build_stream_url ( meta ) <EOL> duration = int ( math . ceil ( float ( meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) / <NUM_LIT> ) ) <EOL> post_dict = { '<STR_LIT>' : meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] } <EOL> post_data = compat_urllib_parse . urlencode ( post_dict ) . encode ( '<STR_LIT:utf-8>' ) <EOL> headers = { <EOL> '<STR_LIT>' : len ( post_data ) , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' <EOL> } <EOL> if swf_referer is not None : <EOL> headers [ '<STR_LIT>' ] = swf_referer <EOL> return { <EOL> '<STR_LIT:id>' : token , <EOL> '<STR_LIT:title>' : meta [ '<STR_LIT>' ] [ '<STR_LIT:Name>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> '<STR_LIT:url>' : stream_url , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : duration , <EOL> '<STR_LIT>' : post_data , <EOL> '<STR_LIT>' : headers , <EOL> } </s>
