Commit 557ad8db authored by Paweł Wegner's avatar Paweł Wegner
Browse files

Added YouTube.

parent 50577a52
/*****************************************************************************
* YouTube.cpp : YouTube implementation
*
*****************************************************************************
* Copyright (C) 2016-2016 VideoLAN
*
* Authors: Paweł Wegner <pawel.wegner95@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "YouTube.h"
#include <jsoncpp/json/json.h>
#include <iostream>
#include "Utility/Item.h"
#include "Utility/Utility.h"
namespace cloudstorage {
YouTube::YouTube() : CloudProvider(make_unique<Auth>()) {}
std::string YouTube::name() const { return "youtube"; }
HttpRequest::Pointer YouTube::getItemDataRequest(const std::string& id,
std::ostream&) const {
auto request = make_unique<HttpRequest>(
"https://www.googleapis.com/youtube/v3/playlistItems",
HttpRequest::Type::GET);
request->setParameter("part", "contentDetails,snippet");
request->setParameter("id", id);
return request;
}
HttpRequest::Pointer YouTube::listDirectoryRequest(
const IItem& item, const std::string& page_token, std::ostream&) const {
if (item.id() == rootDirectory()->id()) {
if (page_token.empty())
return make_unique<HttpRequest>(
"https://www.googleapis.com/youtube/v3/"
"channels?mine=true&part=contentDetails",
HttpRequest::Type::GET);
else if (page_token == "real_playlist")
return make_unique<HttpRequest>(
"https://www.googleapis.com/youtube/v3/"
"playlists?mine=true&part=snippet",
HttpRequest::Type::GET);
else
return make_unique<HttpRequest>(
"https://www.googleapis.com/youtube/v3/"
"playlists?mine=true&part=snippet&pageToken=" +
page_token,
HttpRequest::Type::GET);
} else {
auto request = make_unique<HttpRequest>(
"https://www.googleapis.com/youtube/v3/playlistItems",
HttpRequest::Type::GET);
request->setParameter("part", "snippet");
request->setParameter("playlistId", item.id());
if (!page_token.empty()) request->setParameter("pageToken", page_token);
return request;
}
}
HttpRequest::Pointer YouTube::uploadFileRequest(const IItem&,
const std::string&,
std::istream&,
std::ostream&) const {
return nullptr;
}
HttpRequest::Pointer YouTube::downloadFileRequest(const IItem& item,
std::ostream&) const {
return make_unique<HttpRequest>(item.url(), HttpRequest::Type::GET);
}
IItem::Pointer YouTube::getItemDataResponse(std::istream& stream) const {
Json::Value response;
stream >> response;
return toItem(response["items"][0], response["kind"].asString());
}
std::vector<IItem::Pointer> YouTube::listDirectoryResponse(
std::istream& stream, std::string& next_page_token) const {
Json::Value response;
stream >> response;
std::vector<IItem::Pointer> result;
if (response["kind"].asString() == "youtube#channelListResponse") {
Json::Value related_playlits =
response["items"][0]["contentDetails"]["relatedPlaylists"];
for (const std::string& name : related_playlits.getMemberNames())
result.push_back(make_unique<Item>(
name, related_playlits[name].asString(), IItem::FileType::Directory));
next_page_token = "real_playlist";
} else {
for (const Json::Value& v : response["items"])
result.push_back(toItem(v, response["kind"].asString()));
}
if (response.isMember("nextPageToken"))
next_page_token = response["nextPageToken"].asString();
return result;
}
IItem::Pointer YouTube::toItem(const Json::Value& v, std::string kind) const {
if (kind == "youtube#playlistListResponse") {
auto item =
make_unique<Item>(v["snippet"]["title"].asString(), v["id"].asString(),
IItem::FileType::Directory);
item->set_thumbnail_url(
v["snippet"]["thumbnails"]["default"]["url"].asString());
return item;
} else {
auto item = make_unique<Item>(v["snippet"]["title"].asString(),
v["id"].asString(), IItem::FileType::Video);
item->set_thumbnail_url(
v["snippet"]["thumbnails"]["default"]["url"].asString());
item->set_url(
"http://youtube-dl.appspot.com/api/play?url=https://www.youtube.com/"
"watch?v=" +
v["snippet"]["resourceId"]["videoId"].asString());
return item;
}
}
std::string YouTube::Auth::authorizeLibraryUrl() const {
return "https://accounts.google.com/o/oauth2/auth?client_id=" + client_id() +
"&redirect_uri=" + redirect_uri() +
"&scope=https://www.googleapis.com/auth/"
"youtube&response_type=code&access_type=offline";
}
} // namespace cloudstorage
/*****************************************************************************
* YouTube.h : YouTube headers
*
*****************************************************************************
* Copyright (C) 2016-2016 VideoLAN
*
* Authors: Paweł Wegner <pawel.wegner95@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef YOUTUBE_H
#define YOUTUBE_H
#include "CloudProvider/CloudProvider.h"
#include "GoogleDrive.h"
#include "Utility/Item.h"
namespace cloudstorage {
class YouTube : public CloudProvider {
public:
YouTube();
std::string name() const;
private:
HttpRequest::Pointer getItemDataRequest(const std::string&,
std::ostream&) const;
HttpRequest::Pointer listDirectoryRequest(const IItem&,
const std::string& page_token,
std::ostream& input_stream) const;
HttpRequest::Pointer uploadFileRequest(const IItem& directory,
const std::string& filename,
std::istream& stream,
std::ostream& input_stream) const;
HttpRequest::Pointer downloadFileRequest(const IItem&,
std::ostream& input_stream) const;
IItem::Pointer getItemDataResponse(std::istream& response) const;
std::vector<IItem::Pointer> listDirectoryResponse(
std::istream&, std::string& next_page_token) const;
IItem::Pointer toItem(const Json::Value&, std::string kind) const;
class Auth : public cloudstorage::GoogleDrive::Auth {
public:
std::string authorizeLibraryUrl() const;
};
};
} // namespace cloudstorage
#endif // YOUTUBE_H
......@@ -23,6 +23,8 @@ libcloudstorage_la_SOURCES = \
CloudProvider/AmazonDrive.cpp \
CloudProvider/Box.cpp \
CloudProvider/Box.h \
CloudProvider/YouTube.cpp \
CloudProvider/YouTube.h \
Request/Request.cpp \
Request/HttpCallback.cpp \
Request/AuthorizeRequest.cpp \
......
......@@ -31,6 +31,7 @@
#include "CloudProvider/Dropbox.h"
#include "CloudProvider/GoogleDrive.h"
#include "CloudProvider/OneDrive.h"
#include "CloudProvider/YouTube.h"
#include "Utility/Utility.h"
namespace cloudstorage {
......@@ -38,7 +39,7 @@ namespace cloudstorage {
CloudStorage::CloudStorage()
: providers_({make_unique<GoogleDrive>(), make_unique<OneDrive>(),
make_unique<Dropbox>(), make_unique<AmazonDrive>(),
make_unique<Box>()}) {}
make_unique<Box>(), make_unique<YouTube>()}) {}
std::vector<ICloudProvider::Pointer> CloudStorage::providers() const {
return providers_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment