Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2010
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 KB | None | 0 0
  1. diff -ur mediatomb/src/youtube_video_url.cc mediatomb-0.12.1/src/youtube_video_url.cc
  2. --- mediatomb/src/youtube_video_url.cc  2010-12-24 15:40:11.917173541 +1000
  3. +++ mediatomb-0.12.1/src/youtube_video_url.cc   2010-12-24 15:32:38.265167072 +1000
  4. @@ -47,11 +47,11 @@
  5.  #define YOUTUBE_URL_PARAMS_REGEXP   "var swfHTML.*\\;"
  6.  #define YOUTUBE_URL_LOCATION_REGEXP "\nLocation: (http://[^\n]+)\n"
  7.  #define YOUTUBE_URL_WATCH           "http://www.youtube.com/watch?v="
  8. -#define YOUTUBE_URL_GET             "http://www.youtube.com/get_video?"
  9.  #define YOUTUBE_URL_PARAM_VIDEO_ID  "video_id"
  10. -#define YOUTUBE_URL_PARAM_T_REGEXP  ".*&t=([^&]+)&"
  11. -#define YOUTUBE_URL_PARAM_T         "t"
  12. +#define YOUTUBE_URL_PARAM_FMT_REGEXP  ".*&fmt_url_map=([^&]+)&"
  13. +#define YOUTUBE_URL_PARAM_FMT         "fmt_url_map"
  14.  #define YOUTUBE_IS_HD_AVAILABLE_REGEXP  "IS_HD_AVAILABLE[^:]*: *([^,]*)"
  15. +
  16.  YouTubeVideoURL::YouTubeVideoURL()
  17.  {
  18.      curl_handle = curl_easy_init();
  19. @@ -62,8 +62,9 @@
  20.      reVideoURLParams->compile(_(YOUTUBE_URL_PARAMS_REGEXP));
  21.      redirectLocation = Ref<RExp>(new RExp());
  22.      redirectLocation->compile(_(YOUTUBE_URL_LOCATION_REGEXP));
  23. -    param_t = Ref<RExp>(new RExp());
  24. -    param_t->compile(_(YOUTUBE_URL_PARAM_T_REGEXP));
  25. +
  26. +    FMT = Ref<RExp>(new RExp());
  27. +    FMT->compile(_(YOUTUBE_URL_PARAM_FMT_REGEXP));
  28.  
  29.      HD = Ref<RExp>(new RExp());
  30.      HD->compile(_(YOUTUBE_IS_HD_AVAILABLE_REGEXP));
  31. @@ -82,7 +83,12 @@
  32.  
  33.  String YouTubeVideoURL::getVideoURL(String video_id, bool mp4, bool hd)
  34.  {
  35. -    long retcode;
  36. +    Ref<StringBuffer> buffer;
  37. +    Ref<Matcher> matcher;
  38. +
  39. +    long retcode;
  40. +    String params;
  41. +    String urls;
  42.      String flv_location;
  43.      String watch;
  44.  #ifdef TOMBDEBUG
  45. @@ -91,112 +97,50 @@
  46.      bool verbose = false;
  47.  #endif
  48.  
  49. -   /*
  50. -// ###########################################################
  51. -
  52. -    String swfargs = read_text_file("/home/jin/Work/UPnP/MediaTomb/YouTube/swf_args_new2.txt");
  53. -    
  54. -    Ref<Matcher> m2 = param_t->matcher(swfargs);
  55. -    
  56. -    if (m2->next())
  57. -    {
  58. -        String hmm = m2->group(1);
  59. -        if (string_ok(hmm))
  60. -            log_debug("############### t: %s\n", hmm.c_str());
  61. -        else
  62. -            log_debug("no match?\n");
  63. -    }
  64. -    throw _Exception(_("OVER"));
  65. -*/
  66. -
  67. -// ###########################################################
  68. -
  69. -
  70.      if (!string_ok(video_id))
  71.          throw _Exception(_("No video ID specified!"));
  72.  
  73.      watch = _(YOUTUBE_URL_WATCH) + video_id;
  74. -
  75.      Ref<URL> url(new URL(YOUTUBE_PAGESIZE));
  76.  
  77. -    Ref<StringBuffer> buffer = url->download(watch, &retcode, curl_handle,
  78. -                                             false, verbose, true);
  79. -    if (retcode != 200)
  80. -    {
  81. -        throw _Exception(_("Failed to get URL for video with id ")
  82. -                         + watch + _("HTTP response code: ") +
  83. -                         String::from(retcode));
  84. -    }
  85. -
  86. -    log_debug("------> GOT BUFFER %s\n", buffer->toString().c_str());
  87. -
  88. -    Ref<Matcher> matcher =  reVideoURLParams->matcher(buffer->toString());
  89. -    String params;
  90. -    if (matcher->next())
  91. -    {
  92. -//        params = trim_string(matcher->group(1));
  93. -        params = trim_string( matcher->group( 0 ) );
  94. -      /*
  95. -        int brace = params.index( '{' );
  96. -        if ( brace > 0 )
  97. -            params = params.substring( brace );
  98. -        brace = params.index( '}' );
  99. -        if ( brace > 0 )
  100. -            params = params.substring( 0, brace + 1 );
  101. -            */
  102. -        Ref<Matcher> m2 = param_t->matcher(params);
  103. -        if (m2->next())
  104. -        {
  105. -            String hmm = m2->group(1);
  106. -            if (string_ok(hmm))
  107. -                params = hmm;
  108. -            else
  109. -            {
  110. -                throw _Exception(_("Could not retrieve \"t\" parameter."));
  111. -            }
  112. -        }
  113. +    buffer = url->download(watch, &retcode, curl_handle, false, verbose, false);
  114. +    if(retcode != 200)
  115. +        throw _Exception(_("Failed to get URL for video with id ") + watch + _("HTTP response code: ") + String::from(retcode));
  116. +
  117. +    log_debug("------> REQUEST URL1: %s\n", watch.c_str());
  118. +    log_debug("------> GOT BUFFER1: %s\n", buffer->toString().c_str());
  119. +
  120. +    matcher = FMT->matcher(buffer->toString());
  121. +    if(matcher->next())
  122. +    {
  123. +   urls = url_unescape(trim_string(matcher->group(1)).c_str());
  124. +   log_debug("------> GOT BUFFER2: %s\n", params.c_str());
  125. +   return whichURL(urls, mp4, hd);
  126.      }
  127. -    else
  128. -    {
  129. -        throw _Exception(_("Failed to get URL for video with id (step 1)") + video_id);
  130. -    }
  131. -
  132. -    params = _(YOUTUBE_URL_GET) + YOUTUBE_URL_PARAM_VIDEO_ID + '=' +
  133. -             video_id + '&' + YOUTUBE_URL_PARAM_T + '=' + params;
  134.  
  135. -    if (mp4)
  136. -    {
  137. -        String format = _("&fmt=18");
  138. -        
  139. -        if (hd)
  140. -        {
  141. -            matcher = HD->matcher(buffer->toString());
  142. -            if (matcher->next())
  143. -            {
  144. -                if (trim_string(matcher->group(1)) == "true")
  145. -                    format = _("&fmt=22");
  146. -            }
  147. -        }
  148. -                    
  149. -        params = params + format;
  150. -    }
  151. -
  152. -    buffer = url->download(params, &retcode, curl_handle, true, verbose, true);
  153. -
  154. -    matcher = redirectLocation->matcher(buffer->toString());
  155. -    if (matcher->next())
  156. -    {
  157. -        if (string_ok(trim_string(matcher->group(1))))
  158. -            return trim_string(matcher->group(1));
  159. -        else
  160. -            throw _Exception(_("Failed to get URL for video with id (step 2)")+
  161. -                             video_id);
  162. -    }
  163. +    throw _Exception(_("Could not retrieve YouTube video URL"));
  164. +}
  165.  
  166. -    if (retcode != 303)
  167. +String YouTubeVideoURL::whichURL(String tmpStr, bool mp4, bool hd)
  168. +{
  169. +    while(tmpStr.find(",") > 0)
  170.      {
  171. -        throw _Exception(_("Unexpected reply from YouTube: ") +
  172. -                         String::from(retcode));
  173. +   String fmturl = tmpStr.substring(0, tmpStr.find(","));
  174. +   tmpStr = tmpStr.substring(tmpStr.find(",") + 1);
  175. +   if(fmturl.find("|") <= 0)
  176. +       continue;
  177. +
  178. +   int fmt = atoi(fmturl.substring(0, fmturl.find("|")).c_str());
  179. +   fmturl = fmturl.substring(fmturl.find("|") + 1);
  180. +
  181. +   log_debug("------> GOT BUFFER3: %d -- %s\n", fmt, fmturl.c_str());
  182. +
  183. +   if(hd && fmt == 22)
  184. +       return fmturl;
  185. +   if((hd || mp4) && fmt == 18)
  186. +       return fmturl;
  187. +   if(fmt == 5)
  188. +       return fmturl;
  189.      }
  190.  
  191.      throw _Exception(_("Could not retrieve YouTube video URL"));
  192. diff -ur mediatomb/src/youtube_video_url.h mediatomb-0.12.1/src/youtube_video_url.h
  193. --- mediatomb/src/youtube_video_url.h   2010-12-24 15:40:11.905172760 +1000
  194. +++ mediatomb-0.12.1/src/youtube_video_url.h    2010-12-24 15:11:27.146641639 +1000
  195. @@ -57,6 +57,7 @@
  196.      /// available
  197.      /// \return the url to the .flv or .mp4 file
  198.      zmm::String getVideoURL(zmm::String video_id, bool mp4, bool hd);
  199. +    zmm::String whichURL(zmm::String tmpStr, bool mp4, bool hd);
  200.  
  201.  protected:
  202.      // the handle *must never be used from multiple threads*
  203. @@ -64,7 +65,8 @@
  204.      pthread_t pid;
  205.      zmm::Ref<RExp> reVideoURLParams;
  206.      zmm::Ref<RExp> redirectLocation;
  207. -    zmm::Ref<RExp> param_t;
  208. +    zmm::Ref<RExp> param_fmt;
  209. +    zmm::Ref<RExp> FMT;
  210.      zmm::Ref<RExp> HD;
  211.  };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement