55 lines
12 KiB
HTML
55 lines
12 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: example12_easy_laststatus.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.18.2</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&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">example12_easy_laststatus.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 about your last status.</span></div><div class="line"><span class="comment"> */</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"></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 "mastodon-cpp.hpp"</span></div><div class="line"><span class="preprocessor"> #include "easy/all.hpp"</span></div><div class="line"><span class="preprocessor">#else</span></div><div class="line"><span class="preprocessor"> #include <mastodon-cpp/mastodon-cpp.hpp></span></div><div class="line"> <span class="comment">// Include all headers in mastodon-cpp/easy/</span></div><div class="line"><span class="preprocessor"> #include <mastodon-cpp/easy/all.hpp></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"></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 < 3)</div><div class="line"> {</div><div class="line"> std::cerr << <span class="stringliteral">"usage: "</span> << argv[0] << <span class="stringliteral">" <instance> <access token>\n"</span>;</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"> }</div><div class="line"></div><div class="line"> Easy masto(argv[1], argv[2]);</div><div class="line"> std::string answer;</div><div class="line"> std::uint16_t ret;</div><div class="line"></div><div class="line"> <span class="comment">// Get own account in order to obtain account ID</span></div><div class="line"> ret = masto.get(API::v1::accounts_verify_credentials, answer);</div><div class="line"> <span class="keywordflow">if</span> (ret == 0)</div><div class="line"> {</div><div class="line"> <span class="comment">// Construct an Account object using the JSON string from the server</span></div><div class="line"> Easy::Account acc(answer);</div><div class="line"></div><div class="line"> <span class="comment">// Get last status</span></div><div class="line"> ret = masto.get(API::v1::accounts_id_statuses,</div><div class="line"> {</div><div class="line"> { <span class="stringliteral">"id"</span>, { std::to_string(acc.id()) } },</div><div class="line"> { <span class="stringliteral">"limit"</span>, { <span class="stringliteral">"1"</span> } }</div><div class="line"> },</div><div class="line"> answer);</div><div class="line"> <span class="keywordflow">if</span> (ret == 0)</div><div class="line"> {</div><div class="line"> <span class="comment">// answer contains an array with a single object. This works because</span></div><div class="line"> <span class="comment">// Easy::Status will turn that into an object, but an array with</span></div><div class="line"> <span class="comment">// more than 1 object will not work.</span></div><div class="line"> Easy::Status status(answer);</div><div class="line"></div><div class="line"> <span class="comment">// An Entitiy is valid if the JSON was not empty and contained no</span></div><div class="line"> <span class="comment">// "error":-key</span></div><div class="line"> <span class="keywordflow">if</span> (status.valid())</div><div class="line"> {</div><div class="line"> <span class="keywordflow">if</span> (!status.language().empty())</div><div class="line"> cout << <span class="stringliteral">"Language: "</span> << status.language() << <span class="charliteral">'\n'</span>;</div><div class="line"> <span class="keywordflow">if</span> (!status.content().empty())</div><div class="line"> cout << <span class="stringliteral">"Content: "</span></div><div class="line"> << status.content().substr(0, 200) << <span class="stringliteral">"…\n"</span>;</div><div class="line"> <span class="keywordflow">if</span> (!status.application().name().empty())</div><div class="line"> cout << <span class="stringliteral">"Application used: "</span></div><div class="line"> << status.application().name() << <span class="charliteral">'\n'</span>;</div><div class="line"> cout << <span class="stringliteral">"ID: "</span> << status.id() << <span class="charliteral">'\n'</span>;</div><div class="line"></div><div class="line"> <span class="keywordtype">string</span> acct;</div><div class="line"> <span class="keywordtype">string</span> url;</div><div class="line"> std::vector<Easy::Attachment> attachments;</div><div class="line"> std::vector<Easy::Tag> tags;</div><div class="line"> <span class="comment">// If the status is a reblog, print the original author</span></div><div class="line"> <span class="keywordflow">if</span> (status.reblog().valid())</div><div class="line"> {</div><div class="line"> <span class="comment">// status.reblog() is an Easy::Status</span></div><div class="line"> <span class="comment">// status.reblog().account() is an Easy::Account</span></div><div class="line"> cout << <span class="stringliteral">"Original ID: "</span> << status.reblog().id() << <span class="charliteral">'\n'</span>;</div><div class="line"> acct = status.reblog().account().acct();</div><div class="line"> url = status.reblog().account().url();</div><div class="line"> attachments = status.reblog().media_attachments();</div><div class="line"> tags = status.reblog().tags();</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> acct = status.account().acct();</div><div class="line"> url = status.account().url();</div><div class="line"> attachments = status.media_attachments();</div><div class="line"> tags = status.tags();</div><div class="line"> }</div><div class="line"> cout << <span class="stringliteral">"From: "</span> << acct << <span class="stringliteral">" "</span>;</div><div class="line"> cout << <span class="stringliteral">"("</span> << url << <span class="stringliteral">")\n"</span>;</div><div class="line"></div><div class="line"> <span class="comment">// List attachments, if any</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> Easy::Attachment &attachment : attachments)</div><div class="line"> {</div><div class="line"> cout << <span class="stringliteral">"Attachment: "</span> << attachment.text_url()</div><div class="line"> << <span class="stringliteral">" ("</span> << attachment.size() << <span class="stringliteral">")\n"</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// List hashtags, if any</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> Easy::Tag &tag : tags)</div><div class="line"> {</div><div class="line"> cout << <span class="stringliteral">"Hashtag: #"</span> << tag.name()</div><div class="line"> << <span class="stringliteral">" ("</span> << tag.url() << <span class="stringliteral">")\n"</span>;</div><div class="line"> }</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"></div><div class="line"> std::cout << answer << <span class="charliteral">'\n'</span>;</div><div class="line"> std::cerr << <span class="stringliteral">"Error code: "</span> << ret << <span class="charliteral">'\n'</span>;</div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="preprocessor">#else</span></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> 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.14
|
|
</small></address>
|
|
</body>
|
|
</html>
|