media/uploadを叩くやつ(#56)
This commit is contained in:
@@ -0,0 +1,78 @@
|
|||||||
|
#include <cocoatweet/api/media/upload.h>
|
||||||
|
#include <cocoatweet/api/model/mediaStore.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Medias {
|
||||||
|
Upload::Upload() {
|
||||||
|
url_ = "https://upload.twitter.com/1.1/media/upload.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Upload::media(const std::string& _media) {
|
||||||
|
media_ = _media;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Upload::mediaId(const std::string& _mediaId) {}
|
||||||
|
|
||||||
|
void Upload::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
std::ifstream ifs(media_, std::ios::binary);
|
||||||
|
ifs.seekg(0, std::ios::end);
|
||||||
|
unsigned long long size = ifs.tellg();
|
||||||
|
bodyParam_.insert_or_assign("total_bytes", std::to_string(size));
|
||||||
|
ifs.seekg(0);
|
||||||
|
std::string data((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
||||||
|
ifs.close();
|
||||||
|
|
||||||
|
// INIT
|
||||||
|
{
|
||||||
|
contentType_ = "application/x-www-form-urlencoded";
|
||||||
|
// contentType_ = "multipart/form-data";
|
||||||
|
bodyParam_.insert_or_assign("command", "INIT");
|
||||||
|
bodyParam_.insert_or_assign("media_type", "image/jpeg");
|
||||||
|
|
||||||
|
CocoaTweet::API::Model::MediaStore media;
|
||||||
|
HttpPost::process(_oauth,
|
||||||
|
[&media](const unsigned int _responseCode, const std::string& _rsv) {
|
||||||
|
std::cout << _rsv << std::endl;
|
||||||
|
media = CocoaTweet::API::Model::MediaStore::parse(_responseCode, _rsv);
|
||||||
|
});
|
||||||
|
|
||||||
|
bodyParam_.insert_or_assign("media_id", media.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
// APPEND
|
||||||
|
{
|
||||||
|
contentType_ = "multipart/form-data";
|
||||||
|
bodyParam_.erase("media_type");
|
||||||
|
bodyParam_.erase("total_bytes");
|
||||||
|
|
||||||
|
unsigned int segment = 0;
|
||||||
|
const unsigned long long chunk = 1024 * 512;
|
||||||
|
while (segment * chunk < data.size()) {
|
||||||
|
bodyParam_.insert_or_assign("command", "APPEND");
|
||||||
|
bodyParam_.insert_or_assign("segment_index", std::to_string(segment));
|
||||||
|
bodyParam_.insert_or_assign("media", data.substr(segment * chunk, chunk));
|
||||||
|
CocoaTweet::API::Model::MediaStore media;
|
||||||
|
HttpPost::process(_oauth, [](const unsigned int _responseCode, const std::string& _rsv) {
|
||||||
|
// std::cout << _responseCode << std::endl << _rsv<< std::endl;
|
||||||
|
});
|
||||||
|
segment++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FINALIZE
|
||||||
|
{
|
||||||
|
contentType_ = "application/x-www-form-urlencoded";
|
||||||
|
bodyParam_.insert_or_assign("command", "FINALIZE");
|
||||||
|
bodyParam_.erase("segment_index");
|
||||||
|
bodyParam_.erase("media");
|
||||||
|
CocoaTweet::API::Model::MediaStore media;
|
||||||
|
HttpPost::process(_oauth, [](const unsigned int _responseCode, const std::string& _rsv) {
|
||||||
|
std::cout << _rsv << std::endl;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// STATUS if needed
|
||||||
|
{}
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Medias
|
||||||
|
|||||||
@@ -1,22 +1,20 @@
|
|||||||
#ifndef COCOATWEET_API_MEDIA_UPLOAD_H_
|
#ifndef COCOATWEET_API_MEDIA_UPLOAD_H_
|
||||||
#define COCOATWEET_API_MEDIA_UPLOAD_H_
|
#define COCOATWEET_API_MEDIA_UPLOAD_H_
|
||||||
|
|
||||||
#include <cocoatweet/api/interface/postInterface.h>
|
#include <cocoatweet/api/interface/httpPost.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace CocoaTweet::API::Medias {
|
namespace CocoaTweet::API::Medias {
|
||||||
class Upload : public postInterface {
|
class Upload : public CocoaTweet::API::Interface::HttpPost {
|
||||||
public:
|
private:
|
||||||
enum Command : class st::string {
|
std::string media_;
|
||||||
INIT = "INIT",
|
|
||||||
APPEND = "APPEND",
|
|
||||||
FINALIZE = "FINALIZE",
|
|
||||||
};
|
|
||||||
|
|
||||||
Upload::Upload();
|
public:
|
||||||
|
Upload();
|
||||||
void media(const std::string& _media);
|
void media(const std::string& _media);
|
||||||
void mediaId(const std::string _mediaId);
|
void mediaId(const std::string& _mediaId);
|
||||||
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, Command _command);
|
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
}
|
};
|
||||||
} // namespace CocoaTweet::API::Medias
|
} // namespace CocoaTweet::API::Medias
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#include <cocoatweet/api/model/mediaStore.h>
|
||||||
|
#include <cocoatweet/exception/tweetNotFoundException.h>
|
||||||
|
#include <cocoatweet/exception/authenticateException.h>
|
||||||
|
#include <cocoatweet/exception/tweetDuplicateException.h>
|
||||||
|
#include <cocoatweet/exception/tweetTooLongException.h>
|
||||||
|
#include <cocoatweet/exception/rateLimitException.h>
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Model {
|
||||||
|
MediaStore MediaStore::parse(const unsigned int _responseCode, const std::string& _json) {
|
||||||
|
auto j = nlohmann::json::parse(_json);
|
||||||
|
MediaStore media;
|
||||||
|
|
||||||
|
if (_responseCode / 100 == 2) {
|
||||||
|
if(j.count("media_id_string") != 0){
|
||||||
|
media.id(j["media_id_string"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(j.count("size") != 0){
|
||||||
|
media.size(j["size"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(j.count("expires_after_secs") != 0){
|
||||||
|
media.expires(j["expires_after_secs"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(j.count("processing_info") == 0){
|
||||||
|
media.state("succeeded");
|
||||||
|
}else{
|
||||||
|
media.state(j["processing_info"]["state"]);
|
||||||
|
media.remain(j["processing_info"]["check_after_secs"].get<unsigned int>());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
|
||||||
|
return media;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaStore::id(const std::string _id) {
|
||||||
|
id_ = _id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaStore::size(const unsigned int _size){
|
||||||
|
size_ = _size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaStore::expires(const unsigned int _ex) {
|
||||||
|
expires_ = _ex;
|
||||||
|
}
|
||||||
|
void MediaStore::state(const std::string _state) {
|
||||||
|
state_ = _state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MediaStore::remain(const unsigned int _remain){
|
||||||
|
remain_ = _remain;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string MediaStore::id() const {
|
||||||
|
return id_;
|
||||||
|
}
|
||||||
|
const unsigned int MediaStore::size() const {
|
||||||
|
return size_;
|
||||||
|
}
|
||||||
|
const unsigned int MediaStore::expire() const {
|
||||||
|
return expires_;
|
||||||
|
}
|
||||||
|
const std::string MediaStore::state() const {
|
||||||
|
return state_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsigned int MediaStore::remain() const{
|
||||||
|
return remain_;
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Model
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#ifndef COCOATWEET_API_MODEL_MEDIASTORE_H_
|
||||||
|
#define COCOATWEET_API_MODEL_MEDIASTORE_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Model {
|
||||||
|
|
||||||
|
/// @brief data class for tweet object
|
||||||
|
class MediaStore final {
|
||||||
|
public:
|
||||||
|
/// @brief constructor
|
||||||
|
MediaStore() = default;
|
||||||
|
|
||||||
|
/// @brief copy constructor
|
||||||
|
MediaStore(const MediaStore&) = default;
|
||||||
|
|
||||||
|
/// @brief constructor for create object from json response
|
||||||
|
/// @param[in] const unsigned int _responseCode : http status code which received when post request
|
||||||
|
/// @param[in] const std::string& _json : received content from twitter endpoint
|
||||||
|
MediaStore(const unsigned int _responseCode, const std::string& _json)
|
||||||
|
: MediaStore(MediaStore::parse(_responseCode, _json)) {}
|
||||||
|
|
||||||
|
/// @brief response parser for tweet object
|
||||||
|
/// @param[in] const unsigned int _responseCode : http status code which received when post request
|
||||||
|
/// @param[in] const std::string& _json : received content from twitter endpoint
|
||||||
|
/// @param[out] CocoaTweet::API::Model::Tweet
|
||||||
|
static MediaStore parse(const unsigned int _responseCode, const std::string& _json);
|
||||||
|
|
||||||
|
/// @brief set id of tweet
|
||||||
|
/// @param[in] const std::string _id : tweet id to set
|
||||||
|
/// @param[out] none
|
||||||
|
void id(const std::string _id);
|
||||||
|
|
||||||
|
/// @brief set created time of tweet
|
||||||
|
/// @param[in] const std::string _at : tweet created time to set
|
||||||
|
/// @param[out] none
|
||||||
|
void size(const unsigned int _size);
|
||||||
|
|
||||||
|
/// @brief set tweet text
|
||||||
|
/// @param[in] const std::string _text : text of tweet to set
|
||||||
|
/// @param[out] none
|
||||||
|
void expires(const unsigned int _ex);
|
||||||
|
|
||||||
|
/// @brief set tweet source
|
||||||
|
/// @param[in] const std::string _source : source information to set
|
||||||
|
/// @param[out] none
|
||||||
|
void state(const std::string _state);
|
||||||
|
|
||||||
|
void remain(const unsigned int _remain);
|
||||||
|
|
||||||
|
/// @brief get tweet id
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : tweet id
|
||||||
|
const std::string id() const;
|
||||||
|
|
||||||
|
/// @brief get tweet create time
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : time of tweet created time
|
||||||
|
const unsigned int size() const;
|
||||||
|
|
||||||
|
/// @brief get tweet text
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : tweet text
|
||||||
|
const unsigned int expire() const;
|
||||||
|
|
||||||
|
/// @brief get tweet source information
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : source information
|
||||||
|
const std::string state() const;
|
||||||
|
|
||||||
|
const unsigned int remain() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string id_;
|
||||||
|
unsigned long long size_;
|
||||||
|
unsigned long long expires_;
|
||||||
|
std::string state_;
|
||||||
|
unsigned long long remain_;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Model
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,20 +4,68 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace CocoaTweet::API::Model {
|
namespace CocoaTweet::API::Model {
|
||||||
|
|
||||||
|
/// @brief data class for tweet object
|
||||||
class Tweet final {
|
class Tweet final {
|
||||||
public:
|
public:
|
||||||
|
/// @brief constructor
|
||||||
Tweet() = default;
|
Tweet() = default;
|
||||||
|
|
||||||
|
/// @brief copy constructor
|
||||||
Tweet(const Tweet&) = default;
|
Tweet(const Tweet&) = default;
|
||||||
|
|
||||||
|
/// @brief constructor for create object from json response
|
||||||
|
/// @param[in] const unsigned int _responseCode : http status code which received when post
|
||||||
|
/// request
|
||||||
|
/// @param[in] const std::string& _json : received content from twitter endpoint
|
||||||
Tweet(const unsigned int _responseCode, const std::string& _json)
|
Tweet(const unsigned int _responseCode, const std::string& _json)
|
||||||
: Tweet(Tweet::parse(_responseCode, _json)) {}
|
: Tweet(Tweet::parse(_responseCode, _json)) {}
|
||||||
|
|
||||||
|
/// @brief response parser for tweet object
|
||||||
|
/// @param[in] const unsigned int _responseCode : http status code which received when post
|
||||||
|
/// request
|
||||||
|
/// @param[in] const std::string& _json : received content from twitter endpoint
|
||||||
|
/// @param[out] CocoaTweet::API::Model::Tweet
|
||||||
static Tweet parse(const unsigned int _responseCode, const std::string& _json);
|
static Tweet parse(const unsigned int _responseCode, const std::string& _json);
|
||||||
|
|
||||||
|
/// @brief set id of tweet
|
||||||
|
/// @param[in] const std::string _id : tweet id to set
|
||||||
|
/// @param[out] none
|
||||||
void id(const std::string _id);
|
void id(const std::string _id);
|
||||||
|
|
||||||
|
/// @brief set created time of tweet
|
||||||
|
/// @param[in] const std::string _at : tweet created time to set
|
||||||
|
/// @param[out] none
|
||||||
void createdAt(const std::string _at);
|
void createdAt(const std::string _at);
|
||||||
|
|
||||||
|
/// @brief set tweet text
|
||||||
|
/// @param[in] const std::string _text : text of tweet to set
|
||||||
|
/// @param[out] none
|
||||||
void text(const std::string _text);
|
void text(const std::string _text);
|
||||||
|
|
||||||
|
/// @brief set tweet source
|
||||||
|
/// @param[in] const std::string _source : source information to set
|
||||||
|
/// @param[out] none
|
||||||
void source(const std::string _source);
|
void source(const std::string _source);
|
||||||
|
|
||||||
|
/// @brief get tweet id
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : tweet id
|
||||||
const std::string id() const;
|
const std::string id() const;
|
||||||
|
|
||||||
|
/// @brief get tweet create time
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : time of tweet created time
|
||||||
const std::string createdAt() const;
|
const std::string createdAt() const;
|
||||||
|
|
||||||
|
/// @brief get tweet text
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : tweet text
|
||||||
const std::string text() const;
|
const std::string text() const;
|
||||||
|
|
||||||
|
/// @brief get tweet source information
|
||||||
|
/// @param[in] none
|
||||||
|
/// @param[out] const std::string : source information
|
||||||
const std::string source() const;
|
const std::string source() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user