2019-04-17 00:17:26 +02:00
|
|
|
/* This file is part of gitea2rss.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2019-04-17 01:00:33 +02:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <exception>
|
|
|
|
#include <cstdint>
|
2019-04-17 02:51:40 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <ctime>
|
2019-04-17 01:00:33 +02:00
|
|
|
#include <curlpp/cURLpp.hpp>
|
|
|
|
#include <curlpp/Easy.hpp>
|
|
|
|
#include <curlpp/Options.hpp>
|
|
|
|
#include <curlpp/Exception.hpp>
|
|
|
|
#include <curlpp/Infos.hpp>
|
2019-04-17 01:23:23 +02:00
|
|
|
#include <jsoncpp/json/json.h>
|
2019-04-17 01:00:33 +02:00
|
|
|
#include "version.hpp"
|
2019-04-17 00:17:26 +02:00
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2019-04-17 01:00:33 +02:00
|
|
|
using std::string;
|
|
|
|
using std::ostringstream;
|
2019-04-17 01:23:23 +02:00
|
|
|
using std::stringstream;
|
2019-04-17 01:00:33 +02:00
|
|
|
using std::uint16_t;
|
2019-04-17 02:51:40 +02:00
|
|
|
using std::chrono::system_clock;
|
2019-04-17 01:00:33 +02:00
|
|
|
|
|
|
|
namespace curlopts = curlpp::options;
|
|
|
|
|
2019-04-17 02:51:40 +02:00
|
|
|
// Fetch HTTP document.
|
2019-04-17 01:00:33 +02:00
|
|
|
const string get_http(const string &url)
|
|
|
|
{
|
|
|
|
string answer;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ostringstream oss;
|
|
|
|
curlpp::Easy request;
|
|
|
|
request.setOpt<curlopts::Url>(url);
|
|
|
|
request.setOpt<curlopts::UserAgent>(string("gitea2rss/")
|
|
|
|
+ global::version);
|
|
|
|
request.setOpt<curlopts::HttpHeader>({ "Connection: close" });
|
|
|
|
request.setOpt<curlopts::FollowLocation>(true);
|
|
|
|
request.setOpt<curlopts::WriteStream>(&oss);
|
|
|
|
request.perform();
|
|
|
|
uint16_t ret = curlpp::infos::ResponseCode::get(request);
|
|
|
|
if (ret == 200 || ret == 302 || ret == 307
|
|
|
|
|| ret == 301 || ret == 308)
|
|
|
|
{
|
|
|
|
answer = oss.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "HTTP error: " << std::to_string(ret) << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return answer;
|
|
|
|
}
|
2019-04-17 00:17:26 +02:00
|
|
|
|
2019-04-17 02:51:40 +02:00
|
|
|
// RFC 822 compliant time string.
|
|
|
|
const string strtime(const system_clock::time_point &timepoint)
|
|
|
|
{
|
|
|
|
constexpr uint16_t bufsize = 1024;
|
|
|
|
std::time_t time = system_clock::to_time_t(timepoint);
|
|
|
|
std::tm *timeinfo;
|
|
|
|
timeinfo = std::localtime(&time);
|
|
|
|
char buffer[bufsize];
|
|
|
|
std::strftime(buffer, bufsize, "%a, %d %b %Y %T %z", timeinfo);
|
|
|
|
return static_cast<const string>(buffer);
|
|
|
|
}
|
|
|
|
|
2019-04-17 00:17:26 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
cerr << "usage: " << argv[0] << " URL of Gitea project\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2019-04-17 01:00:33 +02:00
|
|
|
|
|
|
|
curlpp::initialize();
|
|
|
|
|
|
|
|
string url = argv[1];
|
|
|
|
size_t pos_repo = url.find('/', 8) + 1;
|
|
|
|
const string baseurl = url.substr(0, pos_repo - 1);
|
2019-04-17 02:51:40 +02:00
|
|
|
const string domain = baseurl.substr(baseurl.rfind('/') + 1);
|
2019-04-17 01:00:33 +02:00
|
|
|
const string repo = url.substr(pos_repo);
|
2019-04-17 02:51:40 +02:00
|
|
|
const string project = repo.substr(repo.find('/') + 1);
|
|
|
|
const string now = strtime(system_clock::now());
|
2019-04-17 01:23:23 +02:00
|
|
|
stringstream data(get_http(baseurl + "/api/v1/repos/"
|
|
|
|
+ repo + "/releases"));
|
|
|
|
Json::Value json;
|
|
|
|
data >> json;
|
2019-04-17 02:51:40 +02:00
|
|
|
|
|
|
|
cout <<
|
|
|
|
"<rss version=\"2.0\">\n"
|
|
|
|
" <channel>\n"
|
|
|
|
" <title>" << project << " releases</title>\n"
|
|
|
|
" <link>" << url << "</link>\n"
|
|
|
|
" <description>Releases of " << repo << "</description>\n"
|
|
|
|
" <generator>gitea2rss " << global::version << "</generator>\n"
|
|
|
|
" <lastBuildDate>" << now << "</lastBuildDate>\n";
|
|
|
|
|
2019-04-17 01:23:23 +02:00
|
|
|
for (const Json::Value &release : json)
|
|
|
|
{
|
2019-04-17 02:51:40 +02:00
|
|
|
const bool prerelease = release["prerelease"].asBool();
|
|
|
|
const string type = (prerelease ? "Pre-Release" : "Stable");
|
|
|
|
cout <<
|
|
|
|
" <item>\n"
|
|
|
|
" <title>"
|
|
|
|
<< project << ": " << release["name"].asString() << "</title>\n"
|
|
|
|
" <link>"
|
|
|
|
<< baseurl << "/" << repo << "/releases/"
|
|
|
|
<< release["id"].asString() << "</link>\n"
|
|
|
|
" <guid isPermaLink=\"false\">"
|
|
|
|
<< domain << " release " << release["id"].asString() << "</guid>\n"
|
|
|
|
" <description>" << type << "\n\n"
|
|
|
|
<< release["body"].asString() << "</description>\n";
|
|
|
|
|
|
|
|
// for (const Json::Value &file : release["assets"])
|
|
|
|
// {
|
|
|
|
// cout << " <enclosure "
|
|
|
|
// << "url=\"" << file["browser_download_url"].asString()
|
|
|
|
// << "\" length=\"" << file["size"].asString()
|
|
|
|
// << "\" type=\"application/octet-stream\"" << "/>\n";
|
|
|
|
// }
|
|
|
|
|
|
|
|
cout << " </item>\n";
|
2019-04-17 01:23:23 +02:00
|
|
|
}
|
2019-04-17 01:00:33 +02:00
|
|
|
|
2019-04-17 02:51:40 +02:00
|
|
|
cout <<
|
|
|
|
" </channel>\n"
|
|
|
|
"</rss>\n";
|
|
|
|
|
2019-04-17 00:17:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|