Coupon, v1.1
Posted on

Utility for a getting and a formatting of coupons from the admitad service.
Required campaigns, a limit of a coupons frequency, a skipping of processed coupons and a support of alternative identifiers in a filter script.
Change Log
- filtering of a coupon list:
- disable a filtering of coupons for specified campaigns;
- via a remembering in an SQLite database:
- limit a frequency of coupons with same campaigns;
- skip coupons that were previously processed;
- filter script:
- support lists of alternative identifiers;
- add a complex example;
- outputting a coupon list:
- fix an errors handling;
- little bit improve the rich example of a coupon template.
Features
- log in an admitad account;
- getting of a coupon list for a specific website;
- filter a coupon list:
- filter by a script in the microlanguage (see below for details);
- disable a filtering for specified campaigns;
- via a remembering in an SQLite database:
- limit a frequency of coupons with same campaigns;
- skip coupons that were previously processed;
- output a coupon list:
- format an output:
- format an output with an Jinja2 template;
- Jinja2 template extensions:
- format a timestamp;
- flatten a text (replace line breaks with a specific string);
- extract a cut (first sentence) with a specific cut mark;
- split a text to paragraphs with a specific paragraph format (optionally skip a specific cut mark);
- output target may be:
- separate files;
- stdout.
- format an output:
Usage
$ coupon
Environment variables:
COUPON_ADMITAD_ID— ID of the Admitad app;COUPON_ADMITAD_SECRET— secret of the Admitad app;COUPON_SITE_ID— ID of the site;COUPON_OUTPUT_PATH— path for output coupons (default:./coupons/);COUPON_TEMPLATE— path to the coupon template (default:./docs/rich_coupon_template.example.html);COUPON_LOCALE— locale for timestamp formatting (default:ru_RU);COUPON_SCRIPT— path to the filter script (default:./docs/coupon_script.example.00.coupon);COUPON_OUTPUT_MODE— mode of output coupons (default:FILES|STDOUT);COUPON_CAMPAIGNS— comma-separated list of disabled campaigns (default:Domino's Pizza, Связной, Юлмарт);COUPON_DATABASE— path to the SQLite database (default:./coupon.db);COUPON_NUMBER— allowed count of coupons with same campaigns (default:1);COUPON_INTERVAL— time window for restriction of coupons with same campaigns (default:86400).
Environment variables can be specified in a .env config in the format:
NAME_1=value_1
NAME_2=value_2
...
See details about the format: https://github.com/motdotla/dotenv#rules.
A .env config will never modify any environment variables that have already been set.
Microlanguage of a Filter Script
Grammar in an EBNF:
script = or expression;
or expression = and expression, {"||", and expression};
and expression = not expression, {"&&", not expression};
not expression = ["!"], atom;
atom = filter | ("(", script, ")");
filter = identifier list, "=~", REGULAR EXPRESSION;
identifier list = identifier, {"|", identifier};
identifier = IDENTIFIER, {".", IDENTIFIER};
IDENTIFIER = ? /\w+/ ?;
REGULAR EXPRESSION = ? /\/(?:\\.|[^\/])+\// ?;
SINGLE-LINE COMMENT = ? /\#.*/ ?.
Example: types.0.name =~ /Бесплатная доставка/ && !(short_name|name|description =~ /\d{4,}/).
Repository
Link: https://github.com/thewizardplusplus/coupon/tree/v1.1.
Content: code.
License: MIT.