55 lines
9.3 KiB
HTML
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
|
|
 <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&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'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 <iostream></span></div><div class="line"><span class="preprocessor">#include <string></span></div><div class="line"><span class="preprocessor">#include <cstdint></span></div><div class="line"><span class="preprocessor">#include <regex></span></div><div class="line"><span class="preprocessor">#include "mastodon-cpp.hpp"</span></div><div class="line"><span class="preprocessor">#include "easy/all.hpp"</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 < 2)</div><div class="line"> {</div><div class="line"> std::cerr << <span class="stringliteral">"usage: "</span> << argv[0] << <span class="stringliteral">" <instance>\n"</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">""</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">"local"</span>, { <span class="stringliteral">"true"</span> }},</div><div class="line"> { <span class="stringliteral">"limit"</span>, { <span class="stringliteral">"4"</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> &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">"<[^>]+>"</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">""</span>);</div><div class="line"> <span class="comment">// Print account and content.</span></div><div class="line"> cout << <span class="stringliteral">" "</span> << 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>() << <span class="stringliteral">" wrote:\n"</span>;</div><div class="line"> cout << content << endl << 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 << ret.<a name="a9"></a><a class="code" href="structMastodon_1_1return__base.html#ab902d5ecac698fadbdcabcd99d0ce754">error_message</a> << 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 << <span class="stringliteral">"HTTP status code: "</span></div><div class="line"> << std::to_string(ret.<a class="code" href="structMastodon_1_1return__base.html#af62546b8b4bb03899ddbec770065e0b6">http_error_code</a>) << 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 <cstdio></span></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> std::printf(<span class="stringliteral">"mastodon-cpp was compiled without Easy support.\n"</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  <a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/>
|
|
</a> 1.8.15
|
|
</small></address>
|
|
</body>
|
|
</html>
|