55 lines
13 KiB
HTML
55 lines
13 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: example03_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
|
||
 <span id="projectnumber">0.15.1</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">example03_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 <iostream></span></div><div class="line"><span class="preprocessor">#include <vector></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 <cstdlib></span></div><div class="line"><span class="preprocessor">#include <fstream></span></div><div class="line"><span class="preprocessor">#include <sstream></span></div><div class="line"><span class="preprocessor">#include <jsoncpp/json/json.h></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">#else</span></div><div class="line"><span class="preprocessor"> #include <mastodon-cpp/mastodon-cpp.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> 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">"20"</span>;</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> <hashtag> [limit]\n"</span>;</div><div class="line"> std::cerr << <span class="stringliteral">" Default limit is 20, max limit is 40.\n"</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">""</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::stringstream ss;</div><div class="line"> std::uint16_t ret;</div><div class="line"> Json::Value config;</div><div class="line"> <span class="keywordtype">string</span> lastid = <span class="stringliteral">"0"</span>;</div><div class="line"> <span class="keywordtype">string</span> filename = string(getenv(<span class="stringliteral">"HOME"</span>)) + <span class="stringliteral">"/.config/mastocron.json"</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"> std::ifstream file(filename, std::ifstream::binary);</div><div class="line"> file >> config;</div><div class="line"> lastid = config.get(hashtag, <span class="stringliteral">"0"</span>).asString();</div><div class="line"> }</div><div class="line"> <span class="keywordflow">catch</span> (std::exception &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't seen yet</span></div><div class="line"> API::parametermap params =</div><div class="line"> {</div><div class="line"> { <span class="stringliteral">"hashtag"</span>, { hashtag } },</div><div class="line"> { <span class="stringliteral">"limit"</span>, { limit } },</div><div class="line"> { <span class="stringliteral">"since_id"</span>, { lastid } }</div><div class="line"> };</div><div class="line"> ret = masto.<a name="a1"></a><a class="code" href="classMastodon_1_1API.html#ac0a88999575530651306f10c0a5d4abb">get</a>(API::v1::timelines_tag_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 "[]" there are none at all</span></div><div class="line"> <span class="keywordflow">if</span> (answer != <span class="stringliteral">""</span> && answer != <span class="stringliteral">"[]"</span>)</div><div class="line"> {</div><div class="line"> <span class="keywordtype">string</span> ornament = <span class="stringliteral">" +++++"</span>;</div><div class="line"> <span class="keywordflow">for</span> (std::uint8_t i = hashtag.size(); i > 0; --i)</div><div class="line"> {</div><div class="line"> ornament += <span class="stringliteral">"+"</span>;</div><div class="line"> }</div><div class="line"> cout << ornament << <span class="charliteral">'\n'</span>;</div><div class="line"> cout << <span class="stringliteral">" + "</span> << hashtag << <span class="stringliteral">": +\n"</span>;</div><div class="line"> cout << ornament << <span class="charliteral">'\n'</span>;</div><div class="line"></div><div class="line"> Json::Value tree;</div><div class="line"> ss.str(answer);</div><div class="line"> ss >> tree;</div><div class="line"> </div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span> &toot : tree)</div><div class="line"> {</div><div class="line"> <span class="keywordtype">string</span> content = toot[<span class="stringliteral">"content"</span>].asString();</div><div class="line"> std::regex reparagraph(<span class="stringliteral">"</p><p>"</span>);</div><div class="line"> std::regex restrip(<span class="stringliteral">"<[^>]*>"</span>);</div><div class="line"></div><div class="line"> cout << <span class="stringliteral">"++++++++\n"</span>;</div><div class="line"> content = std::regex_replace(content, reparagraph, <span class="stringliteral">"\n\n"</span>);</div><div class="line"> cout << std::regex_replace(content, restrip, <span class="stringliteral">""</span>) << <span class="charliteral">'\n'</span>;</div><div class="line"> cout << <span class="stringliteral">" – "</span>;</div><div class="line"> cout << toot[<span class="stringliteral">"account"</span>][<span class="stringliteral">"display_name"</span>].asString()</div><div class="line"> << <span class="stringliteral">" ("</span> << toot[<span class="stringliteral">"account"</span>][<span class="stringliteral">"acct"</span>] << <span class="stringliteral">") at "</span></div><div class="line"> << toot[<span class="stringliteral">"created_at"</span>].asString() << <span class="stringliteral">"\n"</span>;</div><div class="line"> cout << <span class="stringliteral">" "</span> << toot[<span class="stringliteral">"url"</span>].asString() << <span class="charliteral">'\n'</span>;</div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span> &media : toot[<span class="stringliteral">"media_attachments"</span>])</div><div class="line"> {</div><div class="line"> cout << <span class="stringliteral">"Attachment: <"</span> << media[<span class="stringliteral">"url"</span>].asString() << <span class="stringliteral">">\n"</span>;</div><div class="line"> }</div><div class="line"> cout << <span class="stringliteral">"++++++++\n"</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[0][<span class="stringliteral">"id"</span>].asString();</div><div class="line"> config[hashtag] = lastid;</div><div class="line"></div><div class="line"> Json::StreamWriterBuilder wbuilder;</div><div class="line"> <span class="keyword">const</span> <span class="keywordtype">string</span> output = Json::writeString(wbuilder, config);</div><div class="line"> std::ofstream outfile(filename);</div><div class="line"> <span class="keywordflow">if</span> (outfile.is_open())</div><div class="line"> {</div><div class="line"> outfile.write(output.c_str(), output.length());</div><div class="line"> outfile.close();</div><div class="line"> }</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 << <span class="stringliteral">"The URL has permanently changed.\n"</span> <<</div><div class="line"> <span class="stringliteral">"New URL: "</span> << answer << <span class="charliteral">'\n'</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 << <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="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  <a href="http://www.doxygen.org/index.html">
|
||
<img class="footer" src="doxygen.png" alt="doxygen"/>
|
||
</a> 1.8.14
|
||
</small></address>
|
||
</body>
|
||
</html>
|