Browse Source

added example 13, fixed other examples

tags/0.8.0
tastytea 1 year ago
parent
commit
88e2846e83
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: 59346E0EA35C67E5

+ 1
- 2
examples/example10_simplify.cpp View File

@@ -43,11 +43,10 @@ int main(int argc, char *argv[])
43 43
         return 1;
44 44
     }
45 45
 
46
-    std::string answer;
47 46
     std::uint16_t ret = 0;
48 47
     EasyToot masto(argv[1], argv[2]);
49 48
 
50
-    masto.toot("Test");
49
+    ret = masto.toot("Test");
51 50
     if (ret != 0)
52 51
     {
53 52
         std::cerr << "Error: " << ret << '\n';

+ 1
- 0
examples/example12_easy_laststatus.cpp View File

@@ -1,4 +1,5 @@
1 1
 /*  This file is part of mastodon-cpp.
2
+ *  Prints some information about your last status.
2 3
  */
3 4
 
4 5
 // Don't compile this if the Easy-interface is turned off

+ 97
- 0
examples/example13_easy_stream.cpp View File

@@ -0,0 +1,97 @@
1
+/*  This file is part of mastodon-cpp.
2
+ *  Prints some information from the public timeline.
3
+ */
4
+
5
+// Don't compile this if the Easy-interface is turned off
6
+#ifndef WITHOUT_EASY
7
+
8
+#include <iostream>
9
+#include <string>
10
+#include <thread>
11
+#include <memory>
12
+#include <chrono>
13
+#include <vector>
14
+
15
+// If we are compiling mastodon-cpp, use another include path
16
+#ifdef MASTODON_CPP
17
+    #include "mastodon-cpp.hpp"
18
+    #include "easy/all.hpp"
19
+#else
20
+    #include <mastodon-cpp/mastodon-cpp.hpp>
21
+    // Include all headers in mastodon-cpp/easy/
22
+    #include <mastodon-cpp/easy/all.hpp>
23
+#endif
24
+
25
+using Mastodon::API;
26
+using Mastodon::Easy;
27
+using std::cout;
28
+
29
+int main(int argc, char *argv[])
30
+{
31
+    if (argc < 3)
32
+    {
33
+        std::cerr << "usage: " << argv[0] << " <instance> <access token>\n";
34
+        return 1;
35
+    }
36
+
37
+    cout << "I'll show you the public timeline. Press CTRL-C to abort\n";
38
+
39
+    // These have to be static in order to use them in- and outside the thread
40
+    static std::string stream;
41
+    // You can abort the stream with this pointer (ptr->abort_stream())
42
+    static std::unique_ptr<Mastodon::API::http> ptr;
43
+
44
+    // Start a new thread for the stream
45
+    std::thread pub_tl([=]
46
+    {
47
+        Easy masto(argv[1], argv[2]);
48
+        masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr);
49
+    });
50
+
51
+    while (true)
52
+    {
53
+        // Parse event stream and clear it afterwards
54
+        std::vector<Easy::stream_event> events = Easy::parse_stream(stream);
55
+        stream.clear();
56
+
57
+        // The contents of the stream are now in a vector of pairs
58
+        // { Easy::event_type, std::string }
59
+        for (const Easy::stream_event &event : events)
60
+        {
61
+            Easy::Status status;
62
+            Easy::Notification notification;
63
+
64
+            // Print out some information about the events
65
+            switch (event.first)
66
+            {
67
+                case Easy::event_type::Update:
68
+                    status.from_string(event.second);
69
+                    cout << "Status from: " << status.account().acct()
70
+                         << " (" << status.url() << ")\n";
71
+                    break;
72
+                case Easy::event_type::Notification:
73
+                    notification.from_string(event.second);
74
+                    cout << "Notification involving: "
75
+                         << notification.account().acct()
76
+                         << " (" << notification.id() << ")\n";
77
+                    break;
78
+                case Easy::event_type::Delete:
79
+                    cout << "Deleted: " << event.second << '\n';
80
+                    break;
81
+                default:
82
+                    cout << "Something undefined happened. 😱\n";
83
+            }
84
+        }
85
+        std::this_thread::sleep_for(std::chrono::seconds(1));
86
+    }
87
+
88
+    pub_tl.join();
89
+}
90
+
91
+#else
92
+int main()
93
+{
94
+    printf("mastodon-cpp was compiled without Easy support.\n");
95
+    return 255;
96
+}
97
+#endif  // WITHOUT_EASY

+ 1
- 0
src/mastodon-cpp.hpp View File

@@ -38,6 +38,7 @@
38 38
  *  @example example10_simplify.cpp
39 39
  *  @example example11_post_media.cpp
40 40
  *  @example example12_easy_laststatus.cpp
41
+ *  @example example13_easy_laststatus.cpp
41 42
  */
42 43
 namespace Mastodon
43 44
 {

Loading…
Cancel
Save