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/example01_get_public_timeli...

55 lines
9.3 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: example01_get_public_timeline.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">example01_get_public_timeline.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">// Get the last 4 local posts from the public timeline and display them.</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;regex&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::uint16_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">// Retrieve the last 4 local statuses.</span></div><div class="line"> <a name="_a1"></a><a class="code" href="structMastodon_1_1return__call.html">return_call</a> ret = masto.<a name="a2"></a><a class="code" href="classMastodon_1_1API.html#aaa9c68543d4778ba55d67f295fecdf5e">get</a>(API::v1::timelines_public,</div><div class="line"> {</div><div class="line"> { <span class="stringliteral">&quot;local&quot;</span>, { <span class="stringliteral">&quot;true&quot;</span> }},</div><div class="line"> { <span class="stringliteral">&quot;limit&quot;</span>, { <span class="stringliteral">&quot;4&quot;</span> }}</div><div class="line"> });</div><div class="line"> <span class="comment">// If no error was returned.</span></div><div class="line"> <span class="keywordflow">if</span> (ret)</div><div class="line"> {</div><div class="line"> <span class="comment">// Convert answer to vector of strings, loop through vector.</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;strstatus : <a name="a3"></a><a class="code" href="namespaceMastodon_1_1Easy.html#a1c5feb60d638df25a185f1bbc61cc691">Easy::json_array_to_vector</a>(ret.<a name="a4"></a><a class="code" href="structMastodon_1_1return__call.html#a8d9ac799e26fc5ff54592083a8a83404">answer</a>))</div><div class="line"> {</div><div class="line"> <span class="comment">// Construct mastodon::Easy::Status from string.</span></div><div class="line"> <a name="_a5"></a><a class="code" href="classMastodon_1_1Easy_1_1Status.html">Easy::Status</a> status(strstatus);</div><div class="line"> <span class="keywordtype">string</span> content = status.<a name="a6"></a><a class="code" href="classMastodon_1_1Easy_1_1Status.html#a08baa55aeca1317f5beef330b9e7ecf0">content</a>();</div><div class="line"> std::regex re_html(<span class="stringliteral">&quot;&lt;[^&gt;]+&gt;&quot;</span>);</div><div class="line"></div><div class="line"> <span class="comment">// Remove HTML tags from status content.</span></div><div class="line"> content = std::regex_replace(content, re_html, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"> <span class="comment">// Print account and content.</span></div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; &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>() &lt;&lt; <span class="stringliteral">&quot; wrote:\n&quot;</span>;</div><div class="line"> cout &lt;&lt; content &lt;&lt; endl &lt;&lt; endl;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> <span class="comment">// Print error message.</span></div><div class="line"> cerr &lt;&lt; ret.<a name="a9"></a><a class="code" href="structMastodon_1_1return__base.html#ab902d5ecac698fadbdcabcd99d0ce754">error_message</a> &lt;&lt; endl;</div><div class="line"> <span class="comment">// Print HTTP status code, if there is one.</span></div><div class="line"> <span class="keywordflow">if</span> (ret.<a name="a10"></a><a class="code" href="structMastodon_1_1return__base.html#af62546b8b4bb03899ddbec770065e0b6">http_error_code</a> != 0)</div><div class="line"> {</div><div class="line"> cerr &lt;&lt; <span class="stringliteral">&quot;HTTP status code: &quot;</span></div><div class="line"> &lt;&lt; std::to_string(ret.<a class="code" href="structMastodon_1_1return__base.html#af62546b8b4bb03899ddbec770065e0b6">http_error_code</a>) &lt;&lt; endl;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">return</span> ret.<a name="a11"></a><a class="code" href="structMastodon_1_1return__base.html#a12cebffae19249cac4c9e4ae9f95391a">error_code</a>;</div><div class="line"> }</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>