Expanding URLs on the commandline works

This commit is contained in:
tastytea 2018-05-11 02:21:44 +02:00
當前提交 db592a67ad
簽署人: tastytea
GPG Key ID: 59346E0EA35C67E5
共有 7 個文件被更改,包括 193 次插入0 次删除

1
.gitignore vendored Normal file
查看文件

@ -0,0 +1 @@
/build/

29
CMakeLists.txt Normal file
查看文件

@ -0,0 +1,29 @@
cmake_minimum_required (VERSION 3.7)
project (expandurl-mastodon
VERSION 0.0.0
LANGUAGES CXX
)
include(GNUInstallDirs)
include(FindCURL)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_BINARY_DIR})
# Write version in header
configure_file (
"${PROJECT_SOURCE_DIR}/src/version.hpp.in"
"${PROJECT_BINARY_DIR}/version.hpp"
)
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})
file(GLOB sources src/*.cpp)
add_executable(expandurl-mastodon ${sources})
target_link_libraries(expandurl-mastodon mastodon-cpp curl curlpp)
install(TARGETS expandurl-mastodon DESTINATION ${CMAKE_INSTALL_BINDIR})

40
README.md Normal file
查看文件

@ -0,0 +1,40 @@
**expandurl-mastodon** is a Mastodon bot that expands a shortened URL.
# Install
## Dependencies
* Tested OS: Linux
* C++ compiler (tested: gcc 6.4, clang 5.0)
* [cmake](https://cmake.org/) (tested: 3.9.6)
* [curlpp](http://www.curlpp.org/) (tested: 0.8.4)
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.12.0)
## Get sourcecode
### Development version
git clone https://github.com/tastytea/expandurl-mastodon.git
## Compile
mkdir build
cd build/
cmake ..
make
Install with `make install`.
# Usage
## Error codes
Same as [mastodon-cpp](https://github.com/tastytea/mastodon-cpp/blob/master/README.md#error-codes)
# Copyright
Copyright © 2018 tastytea <tastytea@tastytea.de>.
License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.

54
src/expand.cpp Normal file
查看文件

@ -0,0 +1,54 @@
/* This file is part of expandurl-mastodon.
* Copyright © 2018 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>
#include <string>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Infos.hpp>
#include "version.hpp"
#include "expandurl-mastodon.hpp"
using std::cerr;
using std::string;
namespace curlopts = curlpp::options;
const string expand(const string &url)
{
curlpp::Easy request;
request.setOpt<curlopts::CustomRequest>("HEAD");
request.setOpt<curlopts::Url>(url);
request.setOpt<curlopts::UserAgent>
(static_cast<const string>("expandurl-mastodon/") + global::version);
request.setOpt<curlopts::HttpHeader>(
{
"Connection: close",
});
request.setOpt<curlopts::FollowLocation>(true);
try
{
request.perform();
}
catch (const std::exception &e)
{
cerr << "ERROR: " << e.what() << '\n';
cerr << "The previous error is ignored.\n";
}
return curlpp::infos::EffectiveUrl::get(request);
}

查看文件

@ -0,0 +1,26 @@
/* This file is part of expandurl-mastodon.
* Copyright © 2018 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/>.
*/
#ifndef EXPANDURL_MASTODON_HPP
#define EXPANDURL_MASTODON_HPP
#include <string>
using std::string;
const string expand(const string &url);
#endif // EXPANDURL_MASTODON_HPP

34
src/main.cpp Normal file
查看文件

@ -0,0 +1,34 @@
/* This file is part of expandurl-mastodon.
* Copyright © 2018 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>
#include <string>
#include <curlpp/cURLpp.hpp>
#include "expandurl-mastodon.hpp"
using std::cout;
using std::string;
int main(int argc, char *argv[])
{
curlpp::initialize();
cout << expand(argv[1]) << '\n';
curlpp::Cleanup();
return 0;
}

9
src/version.hpp.in Normal file
查看文件

@ -0,0 +1,9 @@
#ifndef VERSION_HPP
#define VERSION_HPP
namespace global
{
static constexpr char version[] = "@PROJECT_VERSION@";
}
#endif // VERSION_HPP