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/example02_stream_8cpp-examp...

55 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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.15"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mastodon-cpp: example02_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.110.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.15 -->
<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">example02_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">// Listen to /api/v1/streaming/public and display the events.</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;cstdint&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;mutex&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;chrono&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;vector&gt;</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"></div><div class="line"><span class="keyword">using</span> std::cout;</div><div class="line"><span class="keyword">using</span> std::cerr;</div><div class="line"><span class="keyword">using</span> std::endl;</div><div class="line"><span class="keyword">using</span> std::string;</div><div class="line"><span class="keyword">using</span> std::uint8_t;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespaceMastodon.html">Mastodon</a>;</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; 2)</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;\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"> <span class="comment">// Construct a Mastodon::Easy object without authorization token.</span></div><div class="line"> <a name="_a0"></a><a class="code" href="classMastodon_1_1Easy_1_1API.html">Easy::API</a> masto(argv[1], <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"> <span class="comment">// Prepare a pointer to the http object, to cancel the stream later.</span></div><div class="line"> std::unique_ptr&lt;API::http&gt; ptr;</div><div class="line"> <span class="comment">// This variable is filled with the stream data.</span></div><div class="line"> <span class="keywordtype">string</span> stream;</div><div class="line"></div><div class="line"> <span class="comment">// Get the public timeline. The pointer is set here.</span></div><div class="line"> masto.<a name="a1"></a><a class="code" href="classMastodon_1_1API.html#a699e4557aef3419b05c4231d0b65388b">get_stream</a>(API::v1::streaming_public, ptr, stream);</div><div class="line"></div><div class="line"> <span class="comment">// Listen to the stream for 120 seconds.</span></div><div class="line"> <span class="keywordflow">for</span> (uint8_t counter = 0; counter &lt; 120; ++counter)</div><div class="line"> {</div><div class="line"> std::this_thread::sleep_for(std::chrono::seconds(1));</div><div class="line"></div><div class="line"> <span class="comment">// Acquire lock for the stream variable to avoid simultaneous access.</span></div><div class="line"> std::lock_guard&lt;std::mutex&gt; lock(ptr-&gt;get_mutex());</div><div class="line"> <span class="comment">// Parse event stream into a vector.</span></div><div class="line"> std::vector&lt;Easy::stream_event_type&gt; events</div><div class="line"> = <a name="a2"></a><a class="code" href="namespaceMastodon_1_1Easy.html#a6bc390f957819b961770947c90f4f99b">Easy::parse_stream</a>(stream);</div><div class="line"> <span class="comment">// Clear the stream buffer.</span></div><div class="line"> stream.clear();</div><div class="line"></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> <a name="_a3"></a><a class="code" href="structMastodon_1_1Easy_1_1stream__event__type.html">Easy::stream_event_type</a> &amp;event : events)</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.type)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Update:</div><div class="line"> {</div><div class="line"> <a name="_a4"></a><a class="code" href="classMastodon_1_1Easy_1_1Status.html">Easy::Status</a> status(event.data);</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;[&quot;</span> &lt;&lt; status.<a name="a5"></a><a class="code" href="classMastodon_1_1Easy_1_1Status.html#a5670b8aece1f3774e760482f3cd0ea71">created_at</a>().<a name="a6"></a><a class="code" href="structMastodon_1_1Easy_1_1time__type.html#a50770addeaf2a00e6541393c732d9284">strtime</a>(<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.<a name="a7"></a><a class="code" href="classMastodon_1_1Easy_1_1Status.html#a032164a0170c5217de9e749422a3818e">account</a>().<a name="a8"></a><a class="code" href="classMastodon_1_1Easy_1_1Account.html#ab803e86aa11ef62c03424bc025f8a274">acct</a>()</div><div class="line"> &lt;&lt; <span class="stringliteral">&quot; (&quot;</span> &lt;&lt; status.<a name="a9"></a><a class="code" href="classMastodon_1_1Easy_1_1Status.html#abc7c9670d2bdc2a7bb6ac75acace3694">url</a>() &lt;&lt; <span class="stringliteral">&quot;)\n&quot;</span>;</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Notification:</div><div class="line"> {</div><div class="line"> <a name="_a10"></a><a class="code" href="classMastodon_1_1Easy_1_1Notification.html">Easy::Notification</a> notification(event.data);</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Notification involving: &quot;</span></div><div class="line"> &lt;&lt; notification.<a name="a11"></a><a class="code" href="classMastodon_1_1Easy_1_1Notification.html#a7dc22950886f1d8d7e03980ac32c3974">account</a>().<a class="code" href="classMastodon_1_1Easy_1_1Account.html#ab803e86aa11ef62c03424bc025f8a274">acct</a>()</div><div class="line"> &lt;&lt; <span class="stringliteral">&quot; (&quot;</span> &lt;&lt; notification.<a name="a12"></a><a class="code" href="classMastodon_1_1Easy_1_1Notification.html#af55d0d1f176a6d931215684519318c10">id</a>() &lt;&lt; <span class="stringliteral">&quot;)\n&quot;</span>;</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Delete:</div><div class="line"> {</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Deleted: &quot;</span> &lt;&lt; <span class="keyword">event</span>.data &lt;&lt; endl;</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">case</span> Easy::event_type::Error:</div><div class="line"> {</div><div class="line"> <span class="comment">// Errors are reported in-stream. Print error and exit.</span></div><div class="line"> cerr &lt;&lt; <span class="stringliteral">&quot;Error: &quot;</span> &lt;&lt; <span class="keyword">event</span>.data &lt;&lt; endl;</div><div class="line"> ptr-&gt;cancel_stream();</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">default</span>:</div><div class="line"> {</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Something undefined happened. 😱\n&quot;</span>;</div><div class="line"> cout &lt;&lt; <span class="keyword">event</span>.data &lt;&lt; endl;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// Close connection.</span></div><div class="line"> ptr-&gt;cancel_stream();</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</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"> std::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.15
</small></address>
</body>
</html>