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/example3_mastocron_8cpp-exa...

54 lines
13 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mastodon-cpp: example3_mastocron.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.5.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">example3_mastocron.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 the new toots under a hashtag, designed to be used in cronjobs</span></div><div class="line"><span class="comment"> */</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;vector&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;sstream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;regex&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;cstdlib&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;../mastodon-cpp.hpp&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;boost/property_tree/ptree.hpp&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;boost/property_tree/json_parser.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">namespace </span>pt = boost::property_tree;</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> std::cout;</div><div class="line"><span class="keyword">using</span> std::string;</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="keywordtype">string</span> limit = <span class="stringliteral">&quot;20&quot;</span>;</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;hashtag&gt; [limit]\n&quot;</span>;</div><div class="line"> std::cerr &lt;&lt; <span class="stringliteral">&quot; Default limit is 20, max limit is 40.\n&quot;</span>;</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argc == 4)</div><div class="line"> {</div><div class="line"> limit = argv[3];</div><div class="line"> }</div><div class="line"></div><div class="line"> <a class="code" href="classMastodon_1_1API.html">Mastodon::API</a> masto(argv[1], <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"> <span class="keywordtype">string</span> hashtag = argv[2];</div><div class="line"> <span class="keywordtype">string</span> answer;</div><div class="line"> std::uint16_t ret;</div><div class="line"> pt::ptree config;</div><div class="line"> <span class="keywordtype">string</span> lastid = <span class="stringliteral">&quot;0&quot;</span>;</div><div class="line"> <span class="keywordtype">string</span> filename = string(getenv(<span class="stringliteral">&quot;HOME&quot;</span>)) + <span class="stringliteral">&quot;/.config/mastocron.json&quot;</span>;</div><div class="line"></div><div class="line"> <span class="comment">// Read config file, get last seen toot-id</span></div><div class="line"> <span class="keywordflow">try</span> {</div><div class="line"> pt::read_json(filename, config);</div><div class="line"> lastid = config.get(hashtag, <span class="stringliteral">&quot;0&quot;</span>);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">catch</span> (std::exception &amp;e)</div><div class="line"> {</div><div class="line"> <span class="comment">// most likely no config file found, ignore</span></div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// Only get toots we haven&#39;t seen yet</span></div><div class="line"> API::parametermap params =</div><div class="line"> {</div><div class="line"> { <span class="stringliteral">&quot;limit&quot;</span>, { limit } },</div><div class="line"> { <span class="stringliteral">&quot;since_id&quot;</span>, { lastid } }</div><div class="line"> };</div><div class="line"> ret = masto.<a name="a1"></a><a class="code" href="classMastodon_1_1API.html#a1d4ace331be12e5234246bc274c0dddd">get</a>(API::v1::timelines_tag_hashtag, hashtag, params, answer);</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (ret == 0)</div><div class="line"> {</div><div class="line"> <span class="comment">// If answer is empty, there are no new toots,</span></div><div class="line"> <span class="comment">// if answer is &quot;[]&quot; there are none at all</span></div><div class="line"> <span class="keywordflow">if</span> (answer != <span class="stringliteral">&quot;&quot;</span> &amp;&amp; answer != <span class="stringliteral">&quot;[]&quot;</span>)</div><div class="line"> {</div><div class="line"> <span class="keywordtype">string</span> ornament = <span class="stringliteral">&quot; +++++&quot;</span>;</div><div class="line"> <span class="keywordflow">for</span> (std::uint8_t i = hashtag.size(); i &gt; 0; --i)</div><div class="line"> {</div><div class="line"> ornament += <span class="stringliteral">&quot;+&quot;</span>;</div><div class="line"> }</div><div class="line"> cout &lt;&lt; ornament &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; + &quot;</span> &lt;&lt; hashtag &lt;&lt; <span class="stringliteral">&quot;: +\n&quot;</span>;</div><div class="line"> cout &lt;&lt; ornament &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"></div><div class="line"> std::istringstream iss(answer);</div><div class="line"> pt::ptree tree;</div><div class="line"> </div><div class="line"> pt::read_json(iss, tree);</div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> pt::ptree::value_type &amp;toot : tree.get_child(<span class="stringliteral">&quot;&quot;</span>))</div><div class="line"> {</div><div class="line"> <span class="keywordtype">string</span> content = toot.second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;content&quot;</span>);</div><div class="line"> std::regex reparagraph(<span class="stringliteral">&quot;&lt;/p&gt;&lt;p&gt;&quot;</span>);</div><div class="line"> std::regex restrip(<span class="stringliteral">&quot;&lt;[^&gt;]*&gt;&quot;</span>);</div><div class="line"></div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;++++++++\n&quot;</span>;</div><div class="line"> content = std::regex_replace(content, reparagraph, <span class="stringliteral">&quot;\n\n&quot;</span>);</div><div class="line"> cout &lt;&lt; std::regex_replace(content, restrip, <span class="stringliteral">&quot;&quot;</span>) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div><div class="line"> cout &lt;&lt; toot.second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;account.display_name&quot;</span>)</div><div class="line"> &lt;&lt; <span class="stringliteral">&quot; (&quot;</span> &lt;&lt; toot.second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;account.acct&quot;</span>) &lt;&lt; <span class="stringliteral">&quot;) at &quot;</span></div><div class="line"> &lt;&lt; toot.second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;created_at&quot;</span>) &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; toot.second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;url&quot;</span>) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> pt::ptree::value_type &amp;media : toot.second.get_child(<span class="stringliteral">&quot;media_attachments&quot;</span>))</div><div class="line"> {</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;Attachment: &lt;&quot;</span> &lt;&lt; media.second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;url&quot;</span>) &lt;&lt; <span class="stringliteral">&quot;&gt;\n&quot;</span>;</div><div class="line"> }</div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot;++++++++\n&quot;</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// Write the id of the newest toot in the config file</span></div><div class="line"> lastid = tree.front().second.get&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;id&quot;</span>);</div><div class="line"> config.put(hashtag, lastid);</div><div class="line"> pt::write_json(filename, config);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret == 13)</div><div class="line"> {</div><div class="line"> std::cerr &lt;&lt; <span class="stringliteral">&quot;The URL has permanently changed.\n&quot;</span> &lt;&lt;</div><div class="line"> <span class="stringliteral">&quot;New URL: &quot;</span> &lt;&lt; answer &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> std::cerr &lt;&lt; <span class="stringliteral">&quot;Error code: &quot;</span> &lt;&lt; ret &lt;&lt; <span class="charliteral">&#39;\n&#39;</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="keywordflow">return</span> 0;</div><div class="line">}</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.13
</small></address>
</body>
</html>