~vanvugt/+junk/mediatomb

« back to all changes in this revision

Viewing changes to src/youtube_video_url.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-03-02 13:09:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080302130916-zlljdze3kt7vuq4b
Tags: 0.11.0-1
* New upstream release.
* Include message about which inotify headers will be used when enabling
  inotify runtime support.
* Fixed error with use of INTERFACE in init script. Also removed use of -m
  option.
* Including new config.xml options.
* Added more build dependencies for new upstream release.
* Removed build dependency of libid3-dev, taglib is now preferred.
* mediatomb.xpm and manpage.xml is now included in orig tarball.
* inotify patch is not needed anymore.
* md5 patch has been committed upstream and is no longer needed. Also removed
  README.Debian.
* TwinHelix PNG fix is now used. Removed from TODO.
* Adding dependency of iceweasel for mediatomb package.
* Updated copyright file.
* Updated watch file.
* Updated rules file for proper configure options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    youtube_video_url.cc - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
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.
 
17
    
 
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.
 
22
    
 
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.
 
26
    
 
27
    $Id: youtube_video_url.cc 1714 2008-03-01 17:33:21Z lww $
 
28
*/
 
29
 
 
30
/// \file youtube_video_url.cc
 
31
/// \brief Definitions of the Transcoding classes. 
 
32
 
 
33
#ifdef HAVE_CONFIG_H
 
34
    #include "autoconfig.h"
 
35
#endif
 
36
 
 
37
#ifdef YOUTUBE
 
38
 
 
39
#include <pthread.h>
 
40
 
 
41
#include "youtube_video_url.h"
 
42
#include "tools.h"
 
43
#include "url.h"
 
44
 
 
45
using namespace zmm;
 
46
 
 
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()
 
54
{
 
55
    curl_handle = curl_easy_init();
 
56
    if (!curl_handle)
 
57
        throw _Exception(_("failed to initialize curl!\n"));
 
58
 
 
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));
 
63
 
 
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
 
67
    pid = pthread_self();
 
68
}
 
69
    
 
70
YouTubeVideoURL::~YouTubeVideoURL()
 
71
{
 
72
    if (curl_handle)
 
73
        curl_easy_cleanup(curl_handle);
 
74
}
 
75
 
 
76
String YouTubeVideoURL::getVideoURL(String video_id)
 
77
{
 
78
    long retcode; 
 
79
    String flv_location;
 
80
    String watch;
 
81
#ifdef LOG_TOMBDEBUG
 
82
    bool verbose = true;
 
83
#else
 
84
    bool verbose = false;
 
85
#endif
 
86
 
 
87
    if (!string_ok(video_id))
 
88
        throw _Exception(_("No video ID specified!"));
 
89
 
 
90
    watch = _(YOUTUBE_URL_WATCH) + video_id;
 
91
 
 
92
    Ref<URL> url(new URL(YOUTUBE_PAGESIZE));
 
93
 
 
94
    Ref<StringBuffer> buffer = url->download(watch, &retcode, curl_handle,
 
95
                                             false, verbose, true);
 
96
    if (retcode != 200)
 
97
    {
 
98
        throw _Exception(_("Failed to get URL for video with id ")
 
99
                         + watch + _("HTTP response code: ") + 
 
100
                         String::from(retcode));
 
101
    }
 
102
 
 
103
    log_debug("------> GOT BUFFER %s\n", buffer->toString().c_str());
 
104
 
 
105
    Ref<Matcher> matcher =  reVideoURLParams->matcher(buffer->toString());
 
106
    String params;
 
107
    if (matcher->next())
 
108
    {
 
109
        params = trim_string(matcher->group(1));
 
110
    }
 
111
    else
 
112
    {
 
113
        throw _Exception(_("Failed to get URL for video with id (step 1)") + video_id);
 
114
    }
 
115
 
 
116
    params = _(YOUTUBE_URL_GET) + YOUTUBE_URL_PARAM_VIDEO_ID + '=' + 
 
117
             video_id + '&' + YOUTUBE_URL_PARAM_T + '=' + params;
 
118
 
 
119
    buffer = url->download(params, &retcode, curl_handle, true, verbose, true);
 
120
 
 
121
    matcher = redirectLocation->matcher(buffer->toString());
 
122
    if (matcher->next())
 
123
    {
 
124
        if (string_ok(trim_string(matcher->group(1))))
 
125
            return trim_string(matcher->group(1));
 
126
        else
 
127
            throw _Exception(_("Failed to get URL for video with id (step 2)")+ 
 
128
                             video_id);
 
129
    }
 
130
 
 
131
    if (retcode != 303)
 
132
    {
 
133
        throw _Exception(_("Unexpected reply from YouTube: ") + 
 
134
                         String::from(retcode));
 
135
    }
 
136
 
 
137
    throw _Exception(_("Could not retrieve YouTube video URL"));
 
138
}
 
139
 
 
140
#endif//YOUTUBE