This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/docs/example13_easy_stream_8cpp-...

55 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mastodon-cpp: example13_easy_stream.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">mastodon-cpp
&#160;<span id="projectnumber">0.30.1</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',false,false,'search.php','Search');
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">example13_easy_stream.cpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/* This file is part of mastodon-cpp.</span></div><div class="line"><span class="comment"> * Prints some information from the public timeline.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="comment">// Don&#39;t compile this if the Easy-interface is turned off</span></div><div class="line"><span class="preprocessor">#ifndef WITHOUT_EASY</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;memory&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;chrono&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;mutex&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// If we are compiling mastodon-cpp, use another include path</span></div><div class="line"><span class="preprocessor">#ifdef MASTODON_CPP</span></div><div class="line"><span class="preprocessor"> #include &quot;mastodon-cpp.hpp&quot;</span></div><div class="line"><span class="preprocessor"> #include &quot;easy/all.hpp&quot;</span></div><div class="line"><span class="preprocessor">#else</span></div><div class="line"><span class="preprocessor"> #include &lt;mastodon-cpp/mastodon-cpp.hpp&gt;</span></div><div class="line"> <span class="comment">// Include all headers in mastodon-cpp/easy/</span></div><div class="line"><span class="preprocessor"> #include &lt;mastodon-cpp/easy/all.hpp&gt;</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="keyword">using</span> <a name="_a0"></a><a class="code" href="classMastodon_1_1API.html">Mastodon::API</a>;</div><div class="line"><span class="keyword">using</span> <a name="_a1"></a><a class="code" href="classMastodon_1_1Easy.html">Mastodon::Easy</a>;</div><div class="line"><span class="keyword">using</span> std::cout;</div><div class="line"><span class="keyword">using</span> std::chrono::system_clock;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> (argc &lt; 3)</div><div class="line"> {</div><div class="line"> std::cerr &lt;&lt; <span class="stringliteral">&quot;usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; &lt;instance&gt; &lt;access token&gt;\n&quot;</span>;</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"> }</div><div class="line"></div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;I&#39;ll show you the public timeline. Press CTRL-C to cancel\n&quot;</span>;</div><div class="line"></div><div class="line"> <span class="comment">// These have to be static in order to use them in- and outside the thread</span></div><div class="line"> <span class="keyword">static</span> std::string stream;</div><div class="line"> <span class="comment">// You can cancel the stream with this pointer (ptr-&gt;cancel_stream())</span></div><div class="line"> <span class="keyword">static</span> std::unique_ptr&lt;Mastodon::API::http&gt; ptr;</div><div class="line"></div><div class="line"> <span class="comment">// Start a new thread for the stream</span></div><div class="line"> std::thread pub_tl([=]</div><div class="line"> {</div><div class="line"> Easy masto(argv[1], argv[2]);</div><div class="line"> masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr);</div><div class="line"> });</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span> (<span class="keyword">true</span>)</div><div class="line"> {</div><div class="line"> std::this_thread::sleep_for(std::chrono::seconds(1));</div><div class="line"> <span class="comment">// Skip iteration if ptr points not to the Mastodon::API::http object</span></div><div class="line"> <span class="keywordflow">if</span> (ptr == <span class="keyword">nullptr</span>)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">continue</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// Acquire lock for the stream variable</span></div><div class="line"> std::lock_guard&lt;std::mutex&gt; lock(ptr-&gt;get_mutex());</div><div class="line"></div><div class="line"> <span class="comment">// Parse event stream and clear it afterwards</span></div><div class="line"> std::vector&lt;Easy::stream_event&gt; events = Easy::parse_stream(stream);</div><div class="line"> stream.clear();</div><div class="line"></div><div class="line"> <span class="comment">// The contents of the stream are now in a vector of pairs</span></div><div class="line"> <span class="comment">// { Easy::event_type, std::string }</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> Easy::stream_event &amp;event : events)</div><div class="line"> {</div><div class="line"> Easy::Status status;</div><div class="line"> Easy::Notification notification;</div><div class="line"></div><div class="line"> <span class="comment">// Print out some information about the events</span></div><div class="line"> <span class="keywordflow">switch</span> (event.first)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Update:</div><div class="line"> status.from_string(event.second);</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[&quot;</span> &lt;&lt;</div><div class="line"> Easy::strtime_local(status.created_at(), <span class="stringliteral">&quot;%T&quot;</span>) &lt;&lt; <span class="stringliteral">&quot;] &quot;</span>;</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Status from: &quot;</span> &lt;&lt; status.account().acct()</div><div class="line"> &lt;&lt; <span class="stringliteral">&quot; (&quot;</span> &lt;&lt; status.url() &lt;&lt; <span class="stringliteral">&quot;)\n&quot;</span>;</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Notification:</div><div class="line"> notification.from_string(event.second);</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Notification involving: &quot;</span></div><div class="line"> &lt;&lt; notification.account().acct()</div><div class="line"> &lt;&lt; <span class="stringliteral">&quot; (&quot;</span> &lt;&lt; notification.id() &lt;&lt; <span class="stringliteral">&quot;)\n&quot;</span>;</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Delete:</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Deleted: &quot;</span> &lt;&lt; <span class="keyword">event</span>.second &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">default</span>:</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Something undefined happened. 😱\n&quot;</span>;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> pub_tl.join();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="preprocessor">#else</span></div><div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">&quot;mastodon-cpp was compiled without Easy support.\n&quot;</span>);</div><div class="line"> <span class="keywordflow">return</span> 255;</div><div class="line">}</div><div class="line"><span class="preprocessor">#endif // WITHOUT_EASY</span></div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.14
</small></address>
</body>
</html>