3
MediaTomb - http://www.mediatomb.cc/
5
youtube_video_url.cc - this file is part of MediaTomb.
7
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
10
Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
11
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
Leonhard Wimmer <leo@mediatomb.cc>
14
MediaTomb is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License version 2
16
as published by the Free Software Foundation.
18
MediaTomb is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
23
You should have received a copy of the GNU General Public License
24
version 2 along with MediaTomb; if not, write to the Free Software
25
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
27
$Id: youtube_video_url.cc 1714 2008-03-01 17:33:21Z lww $
30
/// \file youtube_video_url.cc
31
/// \brief Definitions of the Transcoding classes.
34
#include "autoconfig.h"
41
#include "youtube_video_url.h"
47
#define YOUTUBE_URL_PARAMS_REGEXP "var swfArgs =.*\"t\": \"([^\"]+)\""
48
#define YOUTUBE_URL_LOCATION_REGEXP "\nLocation: (http://[^\n]+)\n"
49
#define YOUTUBE_URL_WATCH "http://www.youtube.com/watch?v="
50
#define YOUTUBE_URL_GET "http://www.youtube.com/get_video?"
51
#define YOUTUBE_URL_PARAM_VIDEO_ID "video_id"
52
#define YOUTUBE_URL_PARAM_T "t"
53
YouTubeVideoURL::YouTubeVideoURL()
55
curl_handle = curl_easy_init();
57
throw _Exception(_("failed to initialize curl!\n"));
59
reVideoURLParams = Ref<RExp>(new RExp());
60
reVideoURLParams->compile(_(YOUTUBE_URL_PARAMS_REGEXP));
61
redirectLocation = Ref<RExp>(new RExp());
62
redirectLocation->compile(_(YOUTUBE_URL_LOCATION_REGEXP));
64
// this is a safeguard to ensure that this class is not called from
65
// multiple threads - it is not allowed to use the same curl handle
66
// from multiple threads
70
YouTubeVideoURL::~YouTubeVideoURL()
73
curl_easy_cleanup(curl_handle);
76
String YouTubeVideoURL::getVideoURL(String video_id)
87
if (!string_ok(video_id))
88
throw _Exception(_("No video ID specified!"));
90
watch = _(YOUTUBE_URL_WATCH) + video_id;
92
Ref<URL> url(new URL(YOUTUBE_PAGESIZE));
94
Ref<StringBuffer> buffer = url->download(watch, &retcode, curl_handle,
95
false, verbose, true);
98
throw _Exception(_("Failed to get URL for video with id ")
99
+ watch + _("HTTP response code: ") +
100
String::from(retcode));
103
log_debug("------> GOT BUFFER %s\n", buffer->toString().c_str());
105
Ref<Matcher> matcher = reVideoURLParams->matcher(buffer->toString());
109
params = trim_string(matcher->group(1));
113
throw _Exception(_("Failed to get URL for video with id (step 1)") + video_id);
116
params = _(YOUTUBE_URL_GET) + YOUTUBE_URL_PARAM_VIDEO_ID + '=' +
117
video_id + '&' + YOUTUBE_URL_PARAM_T + '=' + params;
119
buffer = url->download(params, &retcode, curl_handle, true, verbose, true);
121
matcher = redirectLocation->matcher(buffer->toString());
124
if (string_ok(trim_string(matcher->group(1))))
125
return trim_string(matcher->group(1));
127
throw _Exception(_("Failed to get URL for video with id (step 2)")+
133
throw _Exception(_("Unexpected reply from YouTube: ") +
134
String::from(retcode));
137
throw _Exception(_("Could not retrieve YouTube video URL"));