Browse Source

Expanding URLs on the commandline works

tastytea 7 months ago
commit
db592a67ad
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: 59346E0EA35C67E5
7 changed files with 193 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 29
    0
      CMakeLists.txt
  3. 40
    0
      README.md
  4. 54
    0
      src/expand.cpp
  5. 26
    0
      src/expandurl-mastodon.hpp
  6. 34
    0
      src/main.cpp
  7. 9
    0
      src/version.hpp.in

+ 1
- 0
.gitignore View File

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

+ 29
- 0
CMakeLists.txt View File

@@ -0,0 +1,29 @@
1
+cmake_minimum_required (VERSION 3.7)
2
+project (expandurl-mastodon
3
+         VERSION 0.0.0
4
+         LANGUAGES CXX
5
+)
6
+
7
+include(GNUInstallDirs)
8
+include(FindCURL)
9
+
10
+set(CMAKE_CXX_STANDARD 14)
11
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
12
+set(CMAKE_CXX_EXTENSIONS OFF)
13
+
14
+include_directories(${PROJECT_SOURCE_DIR}/src)
15
+include_directories(${PROJECT_BINARY_DIR})
16
+
17
+# Write version in header
18
+configure_file (
19
+    "${PROJECT_SOURCE_DIR}/src/version.hpp.in"
20
+    "${PROJECT_BINARY_DIR}/version.hpp"
21
+)
22
+
23
+find_package(CURL REQUIRED)
24
+include_directories(${CURL_INCLUDE_DIR})
25
+
26
+file(GLOB sources src/*.cpp)
27
+add_executable(expandurl-mastodon ${sources})
28
+target_link_libraries(expandurl-mastodon mastodon-cpp curl curlpp)
29
+install(TARGETS expandurl-mastodon DESTINATION ${CMAKE_INSTALL_BINDIR})

+ 40
- 0
README.md View File

@@ -0,0 +1,40 @@
1
+**expandurl-mastodon** is a Mastodon bot that expands a shortened URL.
2
+
3
+
4
+# Install
5
+
6
+## Dependencies
7
+
8
+ * Tested OS: Linux
9
+ * C++ compiler (tested: gcc 6.4, clang 5.0)
10
+ * [cmake](https://cmake.org/) (tested: 3.9.6)
11
+ * [curlpp](http://www.curlpp.org/) (tested: 0.8.4)
12
+ * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.12.0)
13
+
14
+## Get sourcecode
15
+
16
+### Development version
17
+
18
+    git clone https://github.com/tastytea/expandurl-mastodon.git
19
+
20
+## Compile
21
+
22
+    mkdir build
23
+    cd build/
24
+    cmake ..
25
+    make
26
+
27
+Install with `make install`.
28
+
29
+# Usage
30
+
31
+## Error codes
32
+
33
+Same as [mastodon-cpp](https://github.com/tastytea/mastodon-cpp/blob/master/README.md#error-codes)
34
+
35
+# Copyright
36
+
37
+    Copyright © 2018 tastytea <tastytea@tastytea.de>.
38
+    License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.
39
+    This program comes with ABSOLUTELY NO WARRANTY. This is free software,
40
+    and you are welcome to redistribute it under certain conditions.

+ 54
- 0
src/expand.cpp View File

@@ -0,0 +1,54 @@
1
+/*  This file is part of expandurl-mastodon.
2
+ *  Copyright © 2018 tastytea <tastytea@tastytea.de>
3
+ *
4
+ *  This program is free software: you can redistribute it and/or modify
5
+ *  it under the terms of the GNU General Public License as published by
6
+ *  the Free Software Foundation, version 3.
7
+ *
8
+ *  This program is distributed in the hope that it will be useful,
9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ *  GNU General Public License for more details.
12
+ *
13
+ *  You should have received a copy of the GNU General Public License
14
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+ */
16
+
17
+#include <iostream>
18
+#include <string>
19
+#include <curlpp/cURLpp.hpp>
20
+#include <curlpp/Options.hpp>
21
+#include <curlpp/Infos.hpp>
22
+#include "version.hpp"
23
+#include "expandurl-mastodon.hpp"
24
+
25
+using std::cerr;
26
+using std::string;
27
+namespace curlopts = curlpp::options;
28
+
29
+const string expand(const string &url)
30
+{
31
+    curlpp::Easy request;
32
+
33
+    request.setOpt<curlopts::CustomRequest>("HEAD");
34
+    request.setOpt<curlopts::Url>(url);
35
+    request.setOpt<curlopts::UserAgent>
36
+        (static_cast<const string>("expandurl-mastodon/") + global::version);
37
+    request.setOpt<curlopts::HttpHeader>(
38
+    {
39
+        "Connection: close",
40
+    });
41
+    request.setOpt<curlopts::FollowLocation>(true);
42
+
43
+    try
44
+    {
45
+        request.perform();
46
+    }
47
+    catch (const std::exception &e)
48
+    {
49
+        cerr << "ERROR: " << e.what() << '\n';
50
+        cerr << "The previous error is ignored.\n";
51
+    }
52
+
53
+    return curlpp::infos::EffectiveUrl::get(request);
54
+}

+ 26
- 0
src/expandurl-mastodon.hpp View File

@@ -0,0 +1,26 @@
1
+/*  This file is part of expandurl-mastodon.
2
+ *  Copyright © 2018 tastytea <tastytea@tastytea.de>
3
+ *
4
+ *  This program is free software: you can redistribute it and/or modify
5
+ *  it under the terms of the GNU General Public License as published by
6
+ *  the Free Software Foundation, version 3.
7
+ *
8
+ *  This program is distributed in the hope that it will be useful,
9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ *  GNU General Public License for more details.
12
+ *
13
+ *  You should have received a copy of the GNU General Public License
14
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+ */
16
+
17
+#ifndef EXPANDURL_MASTODON_HPP
18
+#define EXPANDURL_MASTODON_HPP
19
+
20
+#include <string>
21
+
22
+using std::string;
23
+
24
+const string expand(const string &url);
25
+
26
+#endif  // EXPANDURL_MASTODON_HPP

+ 34
- 0
src/main.cpp View File

@@ -0,0 +1,34 @@
1
+/*  This file is part of expandurl-mastodon.
2
+ *  Copyright © 2018 tastytea <tastytea@tastytea.de>
3
+ *
4
+ *  This program is free software: you can redistribute it and/or modify
5
+ *  it under the terms of the GNU General Public License as published by
6
+ *  the Free Software Foundation, version 3.
7
+ *
8
+ *  This program is distributed in the hope that it will be useful,
9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ *  GNU General Public License for more details.
12
+ *
13
+ *  You should have received a copy of the GNU General Public License
14
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
+ */
16
+
17
+#include <iostream>
18
+#include <string>
19
+#include <curlpp/cURLpp.hpp>
20
+#include "expandurl-mastodon.hpp"
21
+
22
+using std::cout;
23
+using std::string;
24
+
25
+int main(int argc, char *argv[])
26
+{
27
+    curlpp::initialize();
28
+
29
+    cout << expand(argv[1]) << '\n';
30
+
31
+    curlpp::Cleanup();
32
+
33
+    return 0;
34
+}

+ 9
- 0
src/version.hpp.in View File

@@ -0,0 +1,9 @@
1
+#ifndef VERSION_HPP
2
+#define VERSION_HPP
3
+
4
+namespace global
5
+{
6
+    static constexpr char version[] = "@PROJECT_VERSION@";
7
+}
8
+
9
+#endif // VERSION_HPP

Loading…
Cancel
Save