/usr/share/doc/feed2exec-doc/html/plugins.html is in feed2exec-doc 0.11.0.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plugins — feed2exec 0.11.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="feed2exec 0.11.0 documentation" href="index.html"/>
<link rel="next" title="Support" href="support.html"/>
<link rel="prev" title="API documentation" href="api.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> feed2exec
</a>
<div class="version">
0.11.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="usage.html">Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="design.html">Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API documentation</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Plugins</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#output-plugins">Output plugins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.archive">Archive</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.echo">Echo</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.error">Error</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.exec">Exec</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.maildir">Maildir</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.mbox">Mbox</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.null">Null</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.transmission">Transmission</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.wayback">Wayback</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#filter-plugins">Filter plugins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.droptitle">Droptitle</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.emptysummary">Emptysummary</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.html2text">Html2text</a></li>
<li class="toctree-l3"><a class="reference internal" href="#module-feed2exec.plugins.ikiwiki_recentchanges">Ikiwiki Recentchanges</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#writing-new-plugins">Writing new plugins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#basic-plugin-principles">Basic plugin principles</a></li>
<li class="toctree-l3"><a class="reference internal" href="#filters">Filters</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id2">Output plugins</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#basic-arguments">Basic arguments</a></li>
<li class="toctree-l4"><a class="reference internal" href="#sanitizing-contents">Sanitizing contents</a></li>
<li class="toctree-l4"><a class="reference internal" href="#user-configuration">User configuration</a></li>
<li class="toctree-l4"><a class="reference internal" href="#making-http-requests">Making HTTP requests</a></li>
<li class="toctree-l4"><a class="reference internal" href="#plugin-return-values">Plugin return values</a></li>
<li class="toctree-l4"><a class="reference internal" href="#catchup">Catchup</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#writing-tests">Writing tests</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#see-also">See also</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="support.html">Support</a></li>
<li class="toctree-l1"><a class="reference internal" href="contribute.html">Contribute</a></li>
<li class="toctree-l1"><a class="reference internal" href="code.html">Code of Conduct</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li>
<li class="toctree-l1"><a class="reference internal" href="contact.html">Contact</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">feed2exec</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Plugins</li>
<li class="wy-breadcrumbs-aside">
<a href="https://gitlab.com/anarcat/feed2exec/blob/HEAD/doc/plugins.rst">View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="plugins">
<h1>Plugins<a class="headerlink" href="#plugins" title="Permalink to this headline">¶</a></h1>
<p>This is a quick overview of the available plugins.</p>
<div class="section" id="output-plugins">
<h2>Output plugins<a class="headerlink" href="#output-plugins" title="Permalink to this headline">¶</a></h2>
<div class="section" id="module-feed2exec.plugins.archive">
<span id="archive"></span><h3>Archive<a class="headerlink" href="#module-feed2exec.plugins.archive" title="Permalink to this headline">¶</a></h3>
<dl class="data">
<dt id="feed2exec.plugins.archive.DEFAULT_ARCHIVE_DIR">
<code class="descclassname">feed2exec.plugins.archive.</code><code class="descname">DEFAULT_ARCHIVE_DIR</code><em class="property"> = '/run/user/1000/'</em><a class="headerlink" href="#feed2exec.plugins.archive.DEFAULT_ARCHIVE_DIR" title="Permalink to this definition">¶</a></dt>
<dd><p>default archive directory</p>
</dd></dl>
<dl class="function">
<dt id="feed2exec.plugins.archive.output">
<code class="descclassname">feed2exec.plugins.archive.</code><code class="descname">output</code><span class="sig-paren">(</span><em>*args</em>, <em>feed=None</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/archive.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.archive.output" title="Permalink to this definition">¶</a></dt>
<dd><p>The archive plugin saves the feed’s item.link URLs into a
directory, specified by DEFAULT_ARCHIVE_DIR or through the output
<cite>args</cite> value.</p>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span> <span class="n">breaking</span> <span class="n">news</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">breaking_news</span><span class="o">.</span><span class="n">rss</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">archive</span>
<span class="n">args</span> <span class="o">=</span> <span class="o">/</span><span class="n">srv</span><span class="o">/</span><span class="n">archive</span><span class="o">/</span><span class="n">nasa</span><span class="o">/</span>
</pre></div>
</div>
<p>The above will save the “NASA breaking news” into the
<code class="docutils literal"><span class="pre">/srv/archive/nasa</span></code> directory. Do <em>not</em> use interpolation here
as the feed’s variable could be used to mount a directory
transversal attack.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.echo">
<span id="echo"></span><h3>Echo<a class="headerlink" href="#module-feed2exec.plugins.echo" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="feed2exec.plugins.echo.output">
<em class="property">class </em><code class="descclassname">feed2exec.plugins.echo.</code><code class="descname">output</code><span class="sig-paren">(</span><em>*args</em>, <em>feed=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/echo.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.echo.output" title="Permalink to this definition">¶</a></dt>
<dd><p>This plugin outputs, to standard output, the arguments it receives. It
can be useful to test your configuration. It also creates a side
effect for the test suite to determine if the plugin was called.</p>
<p>This plugin does a similar thing when acting as a filter.</p>
</dd></dl>
<dl class="attribute">
<dt id="feed2exec.plugins.echo.filter">
<code class="descclassname">feed2exec.plugins.echo.</code><code class="descname">filter</code><a class="headerlink" href="#feed2exec.plugins.echo.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>This filter just keeps the feed unmodified. It is just there for testing
purposes.</p>
<p>alias of <a class="reference internal" href="#feed2exec.plugins.echo.output" title="feed2exec.plugins.echo.output"><code class="xref py py-class docutils literal"><span class="pre">output</span></code></a></p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.error">
<span id="error"></span><h3>Error<a class="headerlink" href="#module-feed2exec.plugins.error" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.error.output">
<code class="descclassname">feed2exec.plugins.error.</code><code class="descname">output</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/error.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.error.output" title="Permalink to this definition">¶</a></dt>
<dd><p>The error plugin is a simple plugin which raises an exception when
called. It is designed for use in the test suite and should generally
not be used elsewhere.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.exec">
<span id="exec"></span><h3>Exec<a class="headerlink" href="#module-feed2exec.plugins.exec" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.exec.output">
<code class="descclassname">feed2exec.plugins.exec.</code><code class="descname">output</code><span class="sig-paren">(</span><em>command</em>, <em>*args</em>, <em>feed=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/exec.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.exec.output" title="Permalink to this definition">¶</a></dt>
<dd><p>The exec plugin is the ultimate security disaster. It simply
executes whatever you feed it without any sort of sanitization. It
does avoid to call to the shell and executes the command directly,
however. Feed contents are also somewhat sanitized by the
feedparser module, see the <a class="reference external" href="https://pythonhosted.org/feedparser/html-sanitization.html">Sanitization</a>
documentation for more information in that regard. That is limited
to stripping out hostile HTML tags, however.</p>
<p>You should be careful when sending arbitrary parameters to other
programs. Even if we do not use the shell to execute the program,
an hostile feed could still inject commandline flags to change the
program behavior without injecting shell commands themselves.</p>
<p>For example, if a program can write files with the <code class="docutils literal"><span class="pre">-o</span></code> option,
a feed could set their title to <code class="docutils literal"><span class="pre">-oevil</span></code> to overwrite the
<code class="docutils literal"><span class="pre">evil</span></code> file. The only way to workaround that issue is to
carefully craft the commandline so that this cannot happen.</p>
<p>Alternatively, writing a Python plugin is much safer as you can
sanitize the arguments yourself.</p>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>[NASA Whats up?]
url = https://www.nasa.gov/rss/dyn/whats_up.rss
output = feed2exec.plugins.exec
args = wget -P /srv/archives/nasa/ {item.link}
</pre></div>
</div>
<p>The above is the equivalent of the archive plugin: it will save
feed item links to the given directory.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.maildir">
<span id="maildir"></span><h3>Maildir<a class="headerlink" href="#module-feed2exec.plugins.maildir" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="feed2exec.plugins.maildir.output">
<em class="property">class </em><code class="descclassname">feed2exec.plugins.maildir.</code><code class="descname">output</code><span class="sig-paren">(</span><em>to_addr=None</em>, <em>feed=None</em>, <em>item=None</em>, <em>lock=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/maildir.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.maildir.output" title="Permalink to this definition">¶</a></dt>
<dd><p>The maildir plugin will save a feed item into a Maildir folder.</p>
<p>The configuration is a little clunky, but it should be safe
against hostile feeds.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>to_addr</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.6)"><em>str</em></a>) – the email to use as “to” (defaults to
<a class="reference external" href="mailto:USER%40localdomain">USER<span>@</span>localdomain</a>)</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span> <span class="n">breaking</span> <span class="n">news</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">breaking_news</span><span class="o">.</span><span class="n">rss</span>
<span class="n">mailbox</span> <span class="o">=</span> <span class="o">~/</span><span class="n">Maildir</span><span class="o">/</span>
<span class="n">folder</span> <span class="o">=</span> <span class="n">nasa</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">me</span><span class="nd">@example</span><span class="o">.</span><span class="n">com</span>
</pre></div>
</div>
<p>The above will save new feed items from the NASA feed into the
~/Maildir/nasa/ maildir folder, and will set the <cite>To</cite> field of the
email to <cite>me@example.com</cite>.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.mbox">
<span id="mbox"></span><h3>Mbox<a class="headerlink" href="#module-feed2exec.plugins.mbox" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="feed2exec.plugins.mbox.output">
<em class="property">class </em><code class="descclassname">feed2exec.plugins.mbox.</code><code class="descname">output</code><span class="sig-paren">(</span><em>to_addr=None</em>, <em>feed=None</em>, <em>item=None</em>, <em>lock=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/mbox.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.mbox.output" title="Permalink to this definition">¶</a></dt>
<dd><p>The mbox plugin will save a feed item in a Mbox mailbox.</p>
<p>This is mostly for testing purposes, but can of course be used in
the unlikely event where you prefer mbox folders over the
<a class="reference internal" href="#module-feed2exec.plugins.maildir" title="feed2exec.plugins.maildir"><code class="xref py py-mod docutils literal"><span class="pre">feed2exec.plugins.maildir</span></code></a> plugin.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>to_addr</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.6)"><em>str</em></a>) – the email to use as “to” (defaults to
<a class="reference external" href="mailto:USER%40localdomain">USER<span>@</span>localdomain</a>)</td>
</tr>
<tr class="field-even field"><th class="field-name">Todo:</th><td class="field-body">There is some overlap between the code here and the maildir
implementation. Refactoring may be in order, particularly
if we add another mailbox format, though that is unlikely.</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.null">
<span id="null"></span><h3>Null<a class="headerlink" href="#module-feed2exec.plugins.null" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.null.output">
<code class="descclassname">feed2exec.plugins.null.</code><code class="descname">output</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/null.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.null.output" title="Permalink to this definition">¶</a></dt>
<dd><p>This plugin does nothing. It can be useful in cases where you want
to catchup with imported feeds.</p>
</dd></dl>
<dl class="function">
<dt id="feed2exec.plugins.null.filter">
<code class="descclassname">feed2exec.plugins.null.</code><code class="descname">filter</code><span class="sig-paren">(</span><em>item=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/null.html#filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.null.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>The null filter removes all elements from a feed item</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.transmission">
<span id="transmission"></span><h3>Transmission<a class="headerlink" href="#module-feed2exec.plugins.transmission" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.transmission.sanitize">
<code class="descclassname">feed2exec.plugins.transmission.</code><code class="descname">sanitize</code><span class="sig-paren">(</span><em>text</em>, <em>repl='-'</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/transmission.html#sanitize"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.transmission.sanitize" title="Permalink to this definition">¶</a></dt>
<dd><p>like utils.slug, but without lowercase and allow custom replacement</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sanitize</span><span class="p">(</span><span class="s1">'test'</span><span class="p">)</span>
<span class="go">'test'</span>
<span class="gp">>>> </span><span class="n">sanitize</span><span class="p">(</span><span class="s1">'../../../etc/password'</span><span class="p">)</span>
<span class="go">'etc-password'</span>
<span class="gp">>>> </span><span class="n">sanitize</span><span class="p">(</span><span class="s1">'Foo./.bar'</span><span class="p">,</span> <span class="n">repl</span><span class="o">=</span><span class="s1">'.'</span><span class="p">)</span>
<span class="go">'Foo.bar'</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="feed2exec.plugins.transmission.output">
<code class="descclassname">feed2exec.plugins.transmission.</code><code class="descname">output</code><span class="sig-paren">(</span><em>hostname='localhost'</em>, <em>*args</em>, <em>feed=None</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/transmission.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.transmission.output" title="Permalink to this definition">¶</a></dt>
<dd><p>the transmission plugin will send feed items to a <a class="reference external" href="http://www.transmissionbt.com/">transmission</a> instance</p>
<p>it assumes the <code class="docutils literal"><span class="pre">transmission-remote</span></code> command is already
installed and configured to talk with transmission.</p>
<p>the hostname is passed in the <code class="docutils literal"><span class="pre">args</span></code> configuration and defaults
to localhost. the <code class="docutils literal"><span class="pre">folder</span></code> parameter is also used to determine
where to save the actual torrents files.</p>
<p>note that this will also append a sanitized version of the item
title, if a folder is provided. this is to allow saving series in
the same folder.</p>
<p>if the title is unique for each torrent, you may use a filter to
set the title to the right location.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.wayback">
<span id="wayback"></span><h3>Wayback<a class="headerlink" href="#module-feed2exec.plugins.wayback" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.wayback.output">
<code class="descclassname">feed2exec.plugins.wayback.</code><code class="descname">output</code><span class="sig-paren">(</span><em>*args</em>, <em>feed=None</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/wayback.html#output"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.wayback.output" title="Permalink to this definition">¶</a></dt>
<dd><p>This plugin saves the feed items <cite>link</cite> element to the wayback
machine. It will retry URLs that fail, so it may be necessary to
manually catchup feeds if they have broken <cite>link</cite> fields.</p>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span> <span class="n">IOTD</span> <span class="n">wayback</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">lg_image_of_the_day</span><span class="o">.</span><span class="n">rss</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">feed2exec</span><span class="o">.</span><span class="n">plugins</span><span class="o">.</span><span class="n">wayback</span>
</pre></div>
</div>
<p>The above will save the Image of the day updates to the wayback
machine.</p>
</dd></dl>
</div>
</div>
<div class="section" id="filter-plugins">
<h2>Filter plugins<a class="headerlink" href="#filter-plugins" title="Permalink to this headline">¶</a></h2>
<div class="section" id="module-feed2exec.plugins.droptitle">
<span id="droptitle"></span><h3>Droptitle<a class="headerlink" href="#module-feed2exec.plugins.droptitle" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.droptitle.filter">
<code class="descclassname">feed2exec.plugins.droptitle.</code><code class="descname">filter</code><span class="sig-paren">(</span><em>*args</em>, <em>feed=None</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/droptitle.html#filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.droptitle.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>the droptitle filter will drop any feed item with a title matching
the given args.</p>
<p>Example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span> <span class="n">breaking</span> <span class="n">news</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">breaking_news</span><span class="o">.</span><span class="n">rss</span>
<span class="nb">filter</span> <span class="o">=</span> <span class="n">feed2exec</span><span class="o">.</span><span class="n">plugins</span><span class="o">.</span><span class="n">droptitle</span>
<span class="n">filter_args</span> <span class="o">=</span> <span class="n">Trump</span>
</pre></div>
</div>
<p>The above will process the feed items according to the global
configuration, but will skip any item that has the word “Trump”
anywhere in the title field.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.emptysummary">
<span id="emptysummary"></span><h3>Emptysummary<a class="headerlink" href="#module-feed2exec.plugins.emptysummary" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.emptysummary.filter">
<code class="descclassname">feed2exec.plugins.emptysummary.</code><code class="descname">filter</code><span class="sig-paren">(</span><em>*args</em>, <em>feed=None</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/emptysummary.html#filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.emptysummary.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>example of fixes for a broken feed, in this case, the GitHub
release feed which (sometimes) sends empty contents, in which case
the item link field is used as a summary instead.</p>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.html2text">
<span id="html2text"></span><h3>Html2text<a class="headerlink" href="#module-feed2exec.plugins.html2text" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="feed2exec.plugins.html2text.filter">
<em class="property">class </em><code class="descclassname">feed2exec.plugins.html2text.</code><code class="descname">filter</code><span class="sig-paren">(</span><em>*args</em>, <em>feed=None</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/html2text.html#filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.html2text.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>This filter plugin takes a given feed item and adds a
<code class="docutils literal"><span class="pre">content_plain</span></code> field with the HTML parsed as text.</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">the html2text plugin is called automatically from
the email output plugins and should normally not be
called directly.</p>
</div>
<dl class="staticmethod">
<dt id="feed2exec.plugins.html2text.filter.parse">
<em class="property">static </em><code class="descname">parse</code><span class="sig-paren">(</span><em>html=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/html2text.html#filter.parse"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.html2text.filter.parse" title="Permalink to this definition">¶</a></dt>
<dd><p>parse html to text according to our preferences. this is where
subclasses can override the HTML2Text settings or use a
completely different parser</p>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="module-feed2exec.plugins.ikiwiki_recentchanges">
<span id="ikiwiki-recentchanges"></span><h3>Ikiwiki Recentchanges<a class="headerlink" href="#module-feed2exec.plugins.ikiwiki_recentchanges" title="Permalink to this headline">¶</a></h3>
<dl class="function">
<dt id="feed2exec.plugins.ikiwiki_recentchanges.filter">
<code class="descclassname">feed2exec.plugins.ikiwiki_recentchanges.</code><code class="descname">filter</code><span class="sig-paren">(</span><em>*args</em>, <em>item=None</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/feed2exec/plugins/ikiwiki_recentchanges.html#filter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#feed2exec.plugins.ikiwiki_recentchanges.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>the ikiwiki_recentchanges plugin fixes links in ikiwiki feeds</p>
<p>Ikiwiki recent changes show all the recent edits to pages, but the
<code class="docutils literal"><span class="pre"><link></span></code> element doesn’t point to the edit page: it points to
the recent changes page itself, which make them useless for link
checking or archival purposes.</p>
<p>This parses the recent changes entries and extracts the relevant
links from it.</p>
<p>An alternative to this is to use the following entry to generate a
special feed in Ikiwiki:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>[[!inline pages="*" feeds=yes feedonly=yes feedfile=archive show=10]]
</pre></div>
</div>
<p>This generates a feed with proper <code class="docutils literal"><span class="pre"><link></span></code> elements but requires
write access to the wiki.</p>
</dd></dl>
</div>
</div>
<div class="section" id="writing-new-plugins">
<span id="writing-plugins"></span><h2>Writing new plugins<a class="headerlink" href="#writing-new-plugins" title="Permalink to this headline">¶</a></h2>
<p>Most of the actual work in the program is performed by plugins. A
plugin is a simple Python module that has a <code class="docutils literal"><span class="pre">output</span></code> or <code class="docutils literal"><span class="pre">filter</span></code>
“callable” (function or class) with a predefined interface.</p>
<div class="section" id="basic-plugin-principles">
<h3>Basic plugin principles<a class="headerlink" href="#basic-plugin-principles" title="Permalink to this headline">¶</a></h3>
<p>To write a new plugin, you should start by creating a simple Python
module, in your <a class="reference external" href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH">PYTHONPATH</a>. You
can find which directories are in the path by calling:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python3 -c "import sys; print(sys.path)"
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
</pre></div>
</div>
<p>In the above example, a good location would be
<code class="docutils literal"><span class="pre">/usr/local/lib/python3.5/dist-packages</span></code>. The naming convention is
loose: as long as the plugin matches the expected API, it should just
work. For the purpose of this demonstration, we’ll call our plugin
<a class="reference external" href="https://en.wiktionary.org/wiki/trumpery">trumpery</a>, so we will
create the plugin code like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">touch</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="o">.</span><span class="mi">5</span><span class="o">/</span><span class="n">dist</span><span class="o">-</span><span class="n">packages</span><span class="o">/</span><span class="n">trumpery</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>Naturally, if you are going to write multiple plugins, you may want to
regroup your multiple plugins in a package, see the <a class="reference external" href="https://docs.python.org/3/tutorial/modules.html">module
documentation</a> for
more information about this concept in Python.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">There is a rudimentary plugin resolution process that looks
for plugins first in the <cite>feed2exec.plugins</cite> namespace but
then globally. This is done in
<a class="reference internal" href="api.html#feed2exec.plugins.resolve" title="feed2exec.plugins.resolve"><code class="xref py py-func docutils literal"><span class="pre">feed2exec.plugins.resolve()</span></code></a>, called from the <code class="docutils literal"><span class="pre">add</span></code>
and <code class="docutils literal"><span class="pre">parse</span></code> commands. This means that the absolute path is
expected to be used in the configuration file and
internally.</p>
</div>
<p>You are welcome to distribute plugins separately or send them as merge
requests, see <a class="reference internal" href="contribute.html"><span class="doc">Contribution guide</span></a> for more information on how to
participate in this project. We of course welcome contributions to
this documentation as well!</p>
</div>
<div class="section" id="filters">
<h3>Filters<a class="headerlink" href="#filters" title="Permalink to this headline">¶</a></h3>
<p>Now, you need your plugin to do something. In our case, let’s say we’d
like to skip any feed entry that has the word <a class="reference external" href="https://en.wikipedia.org/wiki/Trump">Trump</a> in it. For that purpose, we’ll
create a plugin similar to the already existing
<a class="reference internal" href="#module-feed2exec.plugins.droptitle" title="feed2exec.plugins.droptitle"><code class="xref py py-mod docutils literal"><span class="pre">feed2exec.plugins.droptitle</span></code></a> plugin, but that operates on the
<em>body</em> of the feed, but that also hardcodes the word, because this is
just a demonstration and we want to keep it simple. Let’s look at the
title plugin to see how it works:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="sd">'''the droptitle filter will drop any feed item with a title matching</span>
<span class="sd"> the given args.</span>
<span class="sd"> Example::</span>
<span class="sd"> [NASA breaking news]</span>
<span class="sd"> url = https://www.nasa.gov/rss/dyn/breaking_news.rss</span>
<span class="sd"> filter = feed2exec.plugins.droptitle</span>
<span class="sd"> filter_args = Trump</span>
<span class="sd"> The above will process the feed items according to the global</span>
<span class="sd"> configuration, but will skip any item that has the word "Trump"</span>
<span class="sd"> anywhere in the title field.</span>
<span class="sd"> '''</span>
<span class="n">item</span><span class="p">[</span><span class="s1">'skip'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">' '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="ow">in</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
</pre></div>
</div>
<p>That may look like complete gibberish to you if you are not familiar
with programming or with Python programming in particular. But let’s
take this from the top and copy that in our own plugin. The first line
declares a <a class="reference external" href="https://en.wikibooks.org/wiki/Python_Programming/Functions">function</a> that
takes at least a <code class="docutils literal"><span class="pre">feed</span></code> and a <code class="docutils literal"><span class="pre">item</span></code> argument, but can also accept
any other arbitrary argument. This is important because we want to
have the plugin keep on working if the plugin API changes in the
future. This is called “forward-compatibility”. So let’s copy that in
our plugin and add a <code class="docutils literal"><span class="pre">pass</span></code> statement to make sure the plugin works
(even if it does nothing for now):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>We can already test our plugin by adding it to our configuration, in
<code class="docutils literal"><span class="pre">~/.config/feed2exec/feed2exec.ini</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">breaking_news</span><span class="o">.</span><span class="n">rss</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">feed2exec</span><span class="o">.</span><span class="n">plugins</span><span class="o">.</span><span class="n">echo</span>
<span class="n">args</span> <span class="o">=</span> <span class="p">{</span><span class="n">item</span><span class="o">.</span><span class="n">title</span><span class="p">}</span>
<span class="nb">filter</span> <span class="o">=</span> <span class="n">trumpery</span>
</pre></div>
</div>
<p>Notice how we use the <code class="docutils literal"><span class="pre">output</span></code> plugin to show the title of feed
items selected, as a debugging tool. Let’s fetch this feed in
debugging mode to see what happens:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
arguments received: ('President Trump Welcomes Home Record-breaking NASA Astronaut Peggy Whitson',)
arguments received: ('Three International Space Station Crewmates Safely Return to Earth',)
arguments received: ('NASA Statement on Nomination for Agency Administrator',)
arguments received: ('NASA Television to Air Return of Three International Space Station Crew Members',)
arguments received: ('NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary',)
arguments received: ('NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey',)
arguments received: ('NASA Cancels Planned Media Availabilities with Astronauts',)
arguments received: ('NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition',)
arguments received: ('NASA Awards Contract for Center Protective Services for Glenn Research Center',)
arguments received: ('NASA Announces Cassini End-of-Mission Media Activities',)
1 feeds processed
</pre></div>
</div>
<p>Good! The feed is fetched and items are displayed. It means our filter
didn’t interfere, but now it’s time to make it <em>do</em> something. To skip
items, we need to set the <code class="docutils literal"><span class="pre">skip</span></code> attribute for the feed item to
<cite>True</cite> if we want to skip it and <cite>False</cite> otherwise. So we’ll use a
simple recipe, a bit like <cite>droptitle</cite> does, but simpler, to look at
the feed content to look for our evil word. The <code class="xref py py-mod docutils literal"><span class="pre">feedparser</span></code>
documentation tells us feed items have a <a class="reference external" href="https://pythonhosted.org/feedparser/reference-entry-summary.html">summary</a>
field which we can inspect. There’s also a <a class="reference external" href="https://pythonhosted.org/feedparser/reference-entry-content.html#reference-entry-content">content</a>
list, but that’s a little more complicated so we’ll skip that for
now. So, let’s set the <code class="docutils literal"><span class="pre">skip</span></code> parameter to match if there is the
evil word in our feed item, like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">item</span><span class="p">[</span><span class="s1">'skip'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Trump'</span> <span class="ow">in</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'summary'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
</pre></div>
</div>
<p>And let’s see the result (note that we use the <code class="docutils literal"><span class="pre">--force</span></code> argument
here otherwise we would just skip all items because of the cache):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
item President Trump Welcomes Home Record-breaking NASA Astronaut Peggy Whitson of feed NASA filtered out
arguments received: ('Three International Space Station Crewmates Safely Return to Earth',)
item NASA Statement on Nomination for Agency Administrator of feed NASA filtered out
arguments received: ('NASA Television to Air Return of Three International Space Station Crew Members',)
arguments received: ('NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary',)
arguments received: ('NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey',)
arguments received: ('NASA Cancels Planned Media Availabilities with Astronauts',)
arguments received: ('NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition',)
arguments received: ('NASA Awards Contract for Center Protective Services for Glenn Research Center',)
arguments received: ('NASA Announces Cassini End-of-Mission Media Activities',)
1 feeds processed
</pre></div>
</div>
<p>Successs! We have skipped the two items that contain the fraud we
wanted to remove from the world. Notice how we were able to <em>modify</em>
the feed item: we can also use that to <em>change</em> the feed
content. Normally, we would use this to fix malformed feeds, but let’s
have some fun instead and <a class="reference external" href="https://en.wikipedia.org/wiki/Donald_Trump_(Last_Week_Tonight)">rename Trump to Drumpf</a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">item</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'Trump'</span><span class="p">,</span> <span class="s1">'Drumpf'</span><span class="p">)</span>
</pre></div>
</div>
<p>And the result:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
arguments received: ('President Drumpf Welcomes Home Record-breaking NASA Astronaut Peggy Whitson',)
arguments received: ('Three International Space Station Crewmates Safely Return to Earth',)
arguments received: ('NASA Statement on Nomination for Agency Administrator',)
arguments received: ('NASA Television to Air Return of Three International Space Station Crew Members',)
arguments received: ('NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary',)
arguments received: ('NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey',)
arguments received: ('NASA Cancels Planned Media Availabilities with Astronauts',)
arguments received: ('NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition',)
arguments received: ('NASA Awards Contract for Center Protective Services for Glenn Research Center',)
arguments received: ('NASA Announces Cassini End-of-Mission Media Activities',)
1 feeds processed
</pre></div>
</div>
<p>I know, absolutely hilarious, right? More seriously, this is also how
the <a class="reference internal" href="#module-feed2exec.plugins.html2text" title="feed2exec.plugins.html2text"><code class="xref py py-class docutils literal"><span class="pre">feed2exec.plugins.html2text</span></code></a> filter works, which is
enabled by default and helps the email output plugin do its job by
turning HTML into text. At this point, the only limit is your
knowledge of Python programming and your imagination!</p>
</div>
<div class="section" id="id2">
<h3>Output plugins<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<p>Output plugins are another beast entirely. While they operate with the
same principle than filter plugins (search path and function signature
are similar), they are designed to actually output something for each
new feed item found. This can be anything: a file, email, HTTP
request, whatever. If there is a commandline tool that does what you
need, it is probably simpler to just call the <code class="docutils literal"><span class="pre">exec</span></code> plugin and
there are numerous examples of this in the sample configuration
file. For more complex things, however, it may be easier to actually
write this as a Python.</p>
<div class="section" id="basic-arguments">
<h4>Basic arguments<a class="headerlink" href="#basic-arguments" title="Permalink to this headline">¶</a></h4>
<p>For our example, we’ll write an archival plugin which writes each new
entry to a file hierarchy. First, we start with the same simple
function signature as filters, except we name it output:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">output</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>This is the equivalent of the <code class="docutils literal"><span class="pre">null</span></code> plugin and basically outputs
nothing at all. To archive the feed items, we’ll need to look at the
<a class="reference external" href="https://pythonhosted.org/feedparser/reference-entry-link.html">link</a>
element feedparser gives us. Let’s see what that looks like for the
NASA feed:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">output</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># only operate on items that actually have a link</span>
<span class="k">if</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">,</span> <span class="s1">''</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'no link for feed item </span><span class="si">%s</span><span class="s1">, not archiving'</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">))</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Note that we try to make plugins silent in general. You can
use <a class="reference external" href="https://docs.python.org/3/library/logging.html#logging.info" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">logging.info()</span></code></a> to have things show up in
<code class="docutils literal"><span class="pre">--verbose</span></code> and <a class="reference external" href="https://docs.python.org/3/library/logging.html#logging.debug" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">logging.debug()</span></code></a> for <code class="docutils literal"><span class="pre">--debug</span></code> but
by default, your plugin should be silent unless there’s an
error that requires the user’s intervention, in which case
you should use <a class="reference external" href="https://docs.python.org/3/library/logging.html#logging.warning" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">logging.warning()</span></code></a> for transient errors
that may be automatically recovered and
<a class="reference external" href="https://docs.python.org/3/library/logging.html#logging.error" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">logging.error()</span></code></a> for errors that require user
intervention. This is to allow users to ignore warnings
safely.</p>
</div>
<p>Note that here we first check to see if the feed item actually <em>has</em> a
link - not all feeds do! After adding the above to our <code class="docutils literal"><span class="pre">trumpery</span></code>
plugin and adding it as an output plugin:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">breaking_news</span><span class="o">.</span><span class="n">rss</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">trumpery</span>
<span class="nb">filter</span> <span class="o">=</span> <span class="n">trumpery</span>
</pre></div>
</div>
<p>We can try to see what happens when we call it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python3 -m feed2exec --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
http://www.nasa.gov/press-release/president-trump-welcomes-home-record-breaking-nasa-astronaut-peggy-whitson
http://www.nasa.gov/press-release/three-international-space-station-crewmates-safely-return-to-earth
http://www.nasa.gov/press-release/nasa-statement-on-nomination-for-agency-administrator
http://www.nasa.gov/press-release/nasa-television-to-air-return-of-three-international-space-station-crew-members
http://www.nasa.gov/press-release/nasa-and-iconic-museum-honor-voyager-spacecraft-40th-anniversary
http://www.nasa.gov/press-release/nasa-s-johnson-space-center-closes-through-labor-day-for-tropical-storm-harvey
http://www.nasa.gov/press-release/nasa-cancels-planned-media-availabilities-with-astronauts
http://www.nasa.gov/press-release/nasa-awards-400000-to-top-teams-at-second-phase-of-3d-printing-competition
http://www.nasa.gov/press-release/nasa-awards-contract-for-center-protective-services-for-glenn-research-center
http://www.nasa.gov/press-release/nasa-announces-cassini-end-of-mission-media-activities
1 feeds processed
</pre></div>
</div>
</div>
<div class="section" id="sanitizing-contents">
<h4>Sanitizing contents<a class="headerlink" href="#sanitizing-contents" title="Permalink to this headline">¶</a></h4>
<p>Good. Those are the URLs we want to save to disk. Let’s start by just
writing those to a file. We will also use a simple <cite>slug</cite> function to
make a filesystem-safe name from the feed title and save those files
in a pre-determined location:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">from</span> <span class="nn">feed2exec.utils</span> <span class="k">import</span> <span class="n">slug</span>
<span class="n">ARCHIVE_DIR</span><span class="o">=</span><span class="s1">'/run/user/1000/feed-archives/'</span>
<span class="k">def</span> <span class="nf">output</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># make a safe path from the item name</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">slug</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'no-name'</span><span class="p">))</span>
<span class="c1"># put the file in the archive directory</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ARCHIVE_DIR</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
<span class="c1"># only operate on items that actually have a link</span>
<span class="k">if</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">):</span>
<span class="c1"># tell the user what's going on, if verbose</span>
<span class="c1"># otherwise, we try to stay silent if all goes well</span>
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'saving feed item </span><span class="si">%s</span><span class="s1"> to </span><span class="si">%s</span><span class="s1"> from </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span>
<span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">),</span> <span class="n">path</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">))</span>
<span class="c1"># open the file</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">archive</span><span class="p">:</span>
<span class="c1"># write the response</span>
<span class="n">archive</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'no link for feed item </span><span class="si">%s</span><span class="s1">, not archiving'</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">))</span>
</pre></div>
</div>
<p>Now I know this may look like a <a class="reference external" href="http://knowyourmeme.com/memes/how-to-draw-an-owl">huge step from the previous one</a> but I’m sorry, I
couldn’t find a simpler second step. :) The output now looks like
this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python3 -m feed2exec --config ./doc/ --verbose fetch --force
opening local file /home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml
parsing feed file:///home/anarcat/src/feed2exec/feed2exec/tests/files/breaking_news.xml (10355 bytes)
connecting to database at ./doc/feed2exec.db
saving feed item President Drumpf Welcomes Home Record-breaking NASA Astronaut Peggy Whitson to /run/user/1000/president-drumpf-welcomes-home-record-breaking-nasa-astronaut-peggy-whitson from http://www.nasa.gov/press-release/president-trump-welcomes-home-record-breaking-nasa-astronaut-peggy-whitson
saving feed item Three International Space Station Crewmates Safely Return to Earth to /run/user/1000/three-international-space-station-crewmates-safely-return-to-earth from http://www.nasa.gov/press-release/three-international-space-station-crewmates-safely-return-to-earth
saving feed item NASA Statement on Nomination for Agency Administrator to /run/user/1000/nasa-statement-on-nomination-for-agency-administrator from http://www.nasa.gov/press-release/nasa-statement-on-nomination-for-agency-administrator
saving feed item NASA Television to Air Return of Three International Space Station Crew Members to /run/user/1000/nasa-television-to-air-return-of-three-international-space-station-crew-members from http://www.nasa.gov/press-release/nasa-television-to-air-return-of-three-international-space-station-crew-members
saving feed item NASA and Iconic Museum Honor Voyager Spacecraft 40th Anniversary to /run/user/1000/nasa-and-iconic-museum-honor-voyager-spacecraft-40th-anniversary from http://www.nasa.gov/press-release/nasa-and-iconic-museum-honor-voyager-spacecraft-40th-anniversary
saving feed item NASA’s Johnson Space Center Closes Through Labor Day for Tropical Storm Harvey to /run/user/1000/nasa-s-johnson-space-center-closes-through-labor-day-for-tropical-storm-harvey from http://www.nasa.gov/press-release/nasa-s-johnson-space-center-closes-through-labor-day-for-tropical-storm-harvey
saving feed item NASA Cancels Planned Media Availabilities with Astronauts to /run/user/1000/nasa-cancels-planned-media-availabilities-with-astronauts from http://www.nasa.gov/press-release/nasa-cancels-planned-media-availabilities-with-astronauts
saving feed item NASA Awards $400,000 to Top Teams at Second Phase of 3D-Printing Competition to /run/user/1000/nasa-awards-400-000-to-top-teams-at-second-phase-of-3d-printing-competition from http://www.nasa.gov/press-release/nasa-awards-400000-to-top-teams-at-second-phase-of-3d-printing-competition
saving feed item NASA Awards Contract for Center Protective Services for Glenn Research Center to /run/user/1000/nasa-awards-contract-for-center-protective-services-for-glenn-research-center from http://www.nasa.gov/press-release/nasa-awards-contract-for-center-protective-services-for-glenn-research-center
saving feed item NASA Announces Cassini End-of-Mission Media Activities to /run/user/1000/nasa-announces-cassini-end-of-mission-media-activities from http://www.nasa.gov/press-release/nasa-announces-cassini-end-of-mission-media-activities
</pre></div>
</div>
<p>Sweet! Now it’s not really nice to save this in <code class="docutils literal"><span class="pre">/run/user/1000</span></code>. I
just chose this directory because it was a safe place to write but
it’s not a persistent directory. Best make that configurable, which is
where plugin arguments come in.</p>
</div>
<div class="section" id="user-configuration">
<h4>User configuration<a class="headerlink" href="#user-configuration" title="Permalink to this headline">¶</a></h4>
<p>You see that <code class="docutils literal"><span class="pre">*args</span></code> parameter? That comes straight from the
configuration file. So you could set the path in the configuration
file, like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">NASA</span><span class="p">]</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">www</span><span class="o">.</span><span class="n">nasa</span><span class="o">.</span><span class="n">gov</span><span class="o">/</span><span class="n">rss</span><span class="o">/</span><span class="n">dyn</span><span class="o">/</span><span class="n">breaking_news</span><span class="o">.</span><span class="n">rss</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">trumpery</span>
<span class="n">args</span> <span class="o">=</span> <span class="o">/</span><span class="n">srv</span><span class="o">/</span><span class="n">archives</span><span class="o">/</span><span class="n">nasa</span><span class="o">/</span>
<span class="nb">filter</span> <span class="o">=</span> <span class="n">trumpery</span>
</pre></div>
</div>
<p>We also need to modify the plugin to fetch that configuration, like
this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">output</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">feed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># make a safe path from the item name</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">slug</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'no-name'</span><span class="p">))</span>
<span class="c1"># take the archive dir from the user or use the default</span>
<span class="n">archive_dir</span> <span class="o">=</span> <span class="s1">' '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="k">if</span> <span class="n">args</span> <span class="k">else</span> <span class="n">DEFAULT_ARCHIVE_DIR</span>
<span class="c1"># put the file in the archive directory</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">archive_dir</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
<span class="c1"># [...]</span>
<span class="c1"># rest of the function unchanged</span>
</pre></div>
</div>
</div>
<div class="section" id="making-http-requests">
<h4>Making HTTP requests<a class="headerlink" href="#making-http-requests" title="Permalink to this headline">¶</a></h4>
<p>And now obviously, we only saved the link itself, not the link
<em>content</em>. For that we need some help from the <code class="xref py py-mod docutils literal"><span class="pre">requests</span></code>
module, and do something like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># fetch the URL in memory</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">))</span>
<span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">status_code</span> <span class="o">!=</span> <span class="n">requests</span><span class="o">.</span><span class="n">codes</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'failed to fetch link </span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span>
<span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">),</span> <span class="n">result</span><span class="o">.</span><span class="n">status_code</span><span class="p">)</span>
<span class="c1"># make sure we retry next time</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="c1"># open the file</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">archive</span><span class="p">:</span>
<span class="c1"># write the response</span>
<span class="n">archive</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
</pre></div>
</div>
<p>This will save the actual link content (<code class="docutils literal"><span class="pre">result.text</span></code>) to the
file. The important statement here is:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># fetch the URL in memory</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">))</span>
</pre></div>
</div>
<p>which fetches the URL in memory and checks for errors. The other
change in the final plugin is simply:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">archive</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
</pre></div>
</div>
<p>which writes the article content instead of the link.</p>
</div>
<div class="section" id="plugin-return-values">
<h4>Plugin return values<a class="headerlink" href="#plugin-return-values" title="Permalink to this headline">¶</a></h4>
<p>Notice how we <code class="docutils literal"><span class="pre">return</span> <span class="pre">False</span></code> here: this makes the plugin system
avoid adding the item to the cache, so it is retried on the next
run. If the plugin returns <code class="docutils literal"><span class="pre">True</span></code> or nothing (<code class="docutils literal"><span class="pre">None</span></code>), the plugin
is considered to have succeeded and the entry is added to the
cache. That logic is defined in <code class="xref py py-func docutils literal"><span class="pre">feed2exec.feeds.parse()</span></code>.</p>
</div>
<div class="section" id="catchup">
<h4>Catchup<a class="headerlink" href="#catchup" title="Permalink to this headline">¶</a></h4>
<p>A final thing that is missing that is critical in all plugins is
to respect the <code class="docutils literal"><span class="pre">catchup</span></code> setting. It is propagated up from the
commandline or configuration all the way down to plugins, through the
<code class="docutils literal"><span class="pre">feed</span></code> parameters. How you handle it varies from plugin to plugin,
but the basic idea is to give feedback (when verbose) of activity when
the plugin is run <em>but</em> to not actually <em>do</em> anything. In our case, we
simply return success, right before we fetch the URL:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">feed</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'catchup'</span><span class="p">):</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="c1"># fetch the URL in memory</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'link'</span><span class="p">))</span>
</pre></div>
</div>
<p>Notice how we still fetch the actual feed content but stop before
doing any permanent operation. That is the spirit of the “catchup”
operation: we not only skip “write” operation, but also any operation
which could slow down the “catchup”: fetching stuff over the network
takes time and while it can be considered a “readonly” operation as
far as the local machine is concerned, we are effectively <em>writing</em> to
the network so that operation shouldn’t occur.</p>
<p>Hopefully that should get you going with most of the plugins you are
thinking of writing!</p>
</div>
</div>
<div class="section" id="writing-tests">
<span id="id3"></span><h3>Writing tests<a class="headerlink" href="#writing-tests" title="Permalink to this headline">¶</a></h3>
<p>Writing tests is essential in ensuring that the code will stay
maintainable in the future. It allows for easy refactoring and can
find bugs that manual testing may not, especially when you get
complete coverage (although that is no garantee either).</p>
<p>We’ll take our <cite>archive</cite> plugin as an example. The first step is to
edit the <code class="docutils literal"><span class="pre">tests/test/test_plugins.py</span></code> file, where other plugins are
tests as well. We start by creating a function named <code class="docutils literal"><span class="pre">test_archive</span></code>
so that <a class="reference external" href="https://pytest.org/">Pytest</a>, our test bed, will find
it:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_archive</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">,</span> <span class="n">betamax</span><span class="p">):</span> <span class="c1"># noqa</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>Notice the two arguments named <code class="docutils literal"><span class="pre">tmpdir</span></code> and <code class="docutils literal"><span class="pre">betamax</span></code>. Both
of those are <a class="reference external" href="https://docs.pytest.org/en/latest/fixture.html">fixtures</a>, a pytest concept
that allows to simulate an environment. In particular, the <code class="docutils literal"><span class="pre">tmpdir</span></code>
fixture, shipped with pytest, allows you to easily manage (and
automatically remove) temporary directories. The <code class="docutils literal"><span class="pre">betamax</span></code> fixtures
is a uses the <a class="reference external" href="https://betamax.readthedocs.io/">betamax</a> module to
record then replay HTTP requests.</p>
<p>Then we need to do something. We need to create a feed and a feed item
that we can then send into the plugin. We could also directly parse an
existing feed and indeed some plugins do exactly that. But our plugin
is simple and we can afford to skip full feed parsing and just
synthesize what we need:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">feed</span> <span class="o">=</span> <span class="n">Feed</span><span class="p">(</span><span class="s1">'test archive'</span><span class="p">,</span> <span class="n">test_sample</span><span class="p">)</span>
<span class="n">item</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">FeedParserDict</span><span class="p">({</span><span class="s1">'link'</span><span class="p">:</span> <span class="s1">'http://example.com/'</span><span class="p">,</span>
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'example site'</span><span class="p">})</span>
</pre></div>
</div>
<p>This creates a new feed based on the <code class="docutils literal"><span class="pre">test_sample</span></code> feed. This is
necessary so that the <code class="docutils literal"><span class="pre">session</span></code> is properly re-initialized in the
feed item (otherwise the <code class="docutils literal"><span class="pre">betamax</span></code> fixture will not work). Then it
creates a fake feed entry simply with one link and a title. Then we
can call our plugin, and verify that it saves the file as we
expected. The test for the most common case looks like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_archive</span><span class="p">(</span><span class="n">tmpdir</span><span class="p">,</span> <span class="n">betamax</span><span class="p">):</span> <span class="c1"># noqa</span>
<span class="n">dest</span> <span class="o">=</span> <span class="n">tmpdir</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">'archive'</span><span class="p">)</span>
<span class="n">feed</span> <span class="o">=</span> <span class="n">Feed</span><span class="p">(</span><span class="s1">'test archive'</span><span class="p">,</span> <span class="n">test_sample</span><span class="p">)</span>
<span class="n">item</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">FeedParserDict</span><span class="p">({</span><span class="s1">'link'</span><span class="p">:</span> <span class="s1">'http://example.com/'</span><span class="p">,</span>
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'example site'</span><span class="p">})</span>
<span class="k">assert</span> <span class="n">archive_plugin</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="n">feed</span><span class="o">=</span><span class="n">feed</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="n">item</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">dest</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">'example-site'</span><span class="p">)</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>
</pre></div>
</div>
<p>Then we can try to run this with <code class="docutils literal"><span class="pre">pytest-3</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>[1084]anarcat@curie:feed2exec$ pytest-3
=============================== test session starts ===============================
platform linux -- Python 3.5.3, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: /home/anarcat/src/feed2exec, inifile: setup.cfg
plugins: profiling-1.2.11, cov-2.4.0, betamax-0.8.0
collected 26 items
feed2exec/utils.py ..
feed2exec/plugins/transmission.py .
feed2exec/tests/test_feeds.py ........
feed2exec/tests/test_main.py .....
feed2exec/tests/test_opml.py .
feed2exec/tests/test_plugins.py .........
----------- coverage: platform linux, python 3.5.3-final-0 -----------
Name Stmts Miss Cover
----------------------------------------------------------------
feed2exec/__init__.py 12 0 100%
feed2exec/__main__.py 87 1 99%
feed2exec/_version.py 1 0 100%
feed2exec/email.py 81 7 91%
feed2exec/feeds.py 243 8 97%
feed2exec/logging.py 31 11 65%
feed2exec/plugins/__init__.py 47 6 87%
feed2exec/plugins/archive.py 23 5 78%
feed2exec/plugins/droptitle.py 2 0 100%
feed2exec/plugins/echo.py 8 0 100%
feed2exec/plugins/emptysummary.py 5 0 100%
feed2exec/plugins/error.py 2 0 100%
feed2exec/plugins/exec.py 7 0 100%
feed2exec/plugins/html2text.py 20 4 80%
feed2exec/plugins/ikiwiki_recentchanges.py 9 5 44%
feed2exec/plugins/maildir.py 28 0 100%
feed2exec/plugins/mbox.py 29 1 97%
feed2exec/plugins/null.py 5 1 80%
feed2exec/plugins/transmission.py 20 0 100%
feed2exec/plugins/wayback.py 20 0 100%
feed2exec/tests/__init__.py 0 0 100%
feed2exec/tests/conftest.py 3 0 100%
feed2exec/tests/fixtures.py 19 0 100%
feed2exec/tests/test_feeds.py 124 0 100%
feed2exec/tests/test_main.py 90 0 100%
feed2exec/tests/test_opml.py 17 0 100%
feed2exec/tests/test_plugins.py 162 0 100%
feed2exec/utils.py 41 12 71%
----------------------------------------------------------------
TOTAL 1136 61 95%
=========================== 26 passed in 10.83 seconds ============================
</pre></div>
</div>
<p>Notice the test coverage: we only have 78% test coverage for our
plugin. This means that some branches of the code were not executed at
all! Let’s see if we can improve that. Looking at the code, I see
there are some conditionals for error handling. So let’s simulate an
error, and make sure that we don’t create a file on error:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">dest</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
<span class="n">item</span> <span class="o">=</span> <span class="n">feedparser</span><span class="o">.</span><span class="n">FeedParserDict</span><span class="p">({</span><span class="s1">'link'</span><span class="p">:</span> <span class="s1">'http://example.com/404'</span><span class="p">,</span>
<span class="s1">'title'</span><span class="p">:</span> <span class="s1">'example site'</span><span class="p">})</span>
<span class="k">assert</span> <span class="ow">not</span> <span class="n">archive_plugin</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="n">feed</span><span class="o">=</span><span class="n">feed</span><span class="p">,</span> <span class="n">item</span><span class="o">=</span><span class="n">item</span><span class="p">)</span>
<span class="k">assert</span> <span class="ow">not</span> <span class="n">dest</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">'example-site'</span><span class="p">)</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>
</pre></div>
</div>
<p>There. Let’s see the effect on the test coverage:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>[1085]anarcat@curie:feed2exec2$ pytest-3 feed2exec/tests/test_plugins.py::test_archive
=============================== test session starts ===============================
platform linux -- Python 3.5.3, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
rootdir: /home/anarcat/src/feed2exec, inifile: setup.cfg
plugins: profiling-1.2.11, cov-2.4.0, betamax-0.8.0
collected 10 items
feed2exec/tests/test_plugins.py .
----------- coverage: platform linux, python 3.5.3-final-0 -----------
Name Stmts Miss Cover
----------------------------------------------------------------
feed2exec/__init__.py 12 0 100%
feed2exec/__main__.py 87 87 0%
feed2exec/_version.py 1 0 100%
feed2exec/email.py 81 64 21%
feed2exec/feeds.py 243 172 29%
feed2exec/logging.py 31 31 0%
feed2exec/plugins/__init__.py 47 38 19%
feed2exec/plugins/archive.py 23 3 87%
feed2exec/plugins/droptitle.py 2 2 0%
feed2exec/plugins/echo.py 8 3 62%
feed2exec/plugins/emptysummary.py 5 5 0%
feed2exec/plugins/error.py 2 2 0%
feed2exec/plugins/exec.py 7 7 0%
feed2exec/plugins/html2text.py 20 13 35%
feed2exec/plugins/ikiwiki_recentchanges.py 9 9 0%
feed2exec/plugins/maildir.py 28 19 32%
feed2exec/plugins/mbox.py 29 29 0%
feed2exec/plugins/null.py 5 5 0%
feed2exec/plugins/transmission.py 20 12 40%
feed2exec/plugins/wayback.py 20 20 0%
feed2exec/tests/__init__.py 0 0 100%
feed2exec/tests/conftest.py 3 0 100%
feed2exec/tests/fixtures.py 19 6 68%
feed2exec/tests/test_feeds.py 124 101 19%
feed2exec/tests/test_main.py 90 90 0%
feed2exec/tests/test_opml.py 17 17 0%
feed2exec/tests/test_plugins.py 166 123 26%
feed2exec/utils.py 41 16 61%
----------------------------------------------------------------
TOTAL 1140 874 23%
============================ 1 passed in 2.46 seconds =============================
</pre></div>
</div>
<p>Much better! Only 3 lines left to cover!</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Notice how I explicitly provided a path to my test. This is
entirely optional. You can just run <code class="docutils literal"><span class="pre">pytest-3</span></code> and it will
run the whole test suite: this method is just faster. Notice
also how the coverage ratio is very low: this is normal; we
are testing, after all, only <em>one</em> plugin here.</p>
</div>
<p>The only branches left to test in the code is the other possible error
(“no link in the feed”) and to test the “catchup” mode. You can see
this in the actual <code class="docutils literal"><span class="pre">test_plugins.py</span></code> file distributed with this
documentation.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you discover a bug associated with a single feed, you can
use the betamax session and the
<a class="reference internal" href="api.html#feed2exec.feeds.Feed.parse" title="feed2exec.feeds.Feed.parse"><code class="xref py py-func docutils literal"><span class="pre">feed2exec.feeds.Feed.parse()</span></code></a> function to manually
parse a feed and fire your plugin. This is how email
functionality is tested: see the
<code class="xref py py-func docutils literal"><span class="pre">feed2exec.tests.test_plugins.test_email()</span></code> function for
an example.</p>
</div>
</div>
</div>
<div class="section" id="see-also">
<h2>See also<a class="headerlink" href="#see-also" title="Permalink to this headline">¶</a></h2>
<p><em class="manpage">feed2exec(1)</em></p>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="support.html" class="btn btn-neutral float-right" title="Support" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="api.html" class="btn btn-neutral" title="API documentation" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright Copyright (C) 2016 Antoine Beaupré.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'0.11.0',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|