commit fc75ab2e691f31daefffa7241cae7afc40032b57 Author: Cedric Hoelzl Date: Thu Jul 2 20:04:32 2020 +0200 v0 diff --git a/config.js b/config.js new file mode 100644 index 0000000..634fe1a --- /dev/null +++ b/config.js @@ -0,0 +1,83 @@ +const fs = require('fs'); +const Parser = require('rss-parser'); +const parser = new Parser(); + +const Markup = require('telegraf/markup'); +const Extra = require('telegraf/extra'); + +const db_js = require('./db.js'); + + +var db = new db_js("rss_chan"); + +//=== TOOL FUNCTIONS + +const get_rss = function(url,min_id){ + return parser.parseURL(url).then(feed=>feed.items + .sort((a, b)=>(parseInt(a.guid)-parseInt(b.guid))) + .filter((v)=>(parseInt(v.guid)>min_id))); +} + +const article_to_mess = function(art){ + return `*${art.title.replace('\r\n','').trim()}*\n\n${art.content}\n\n[Read More](${art.link})`; +} + +//=== MAIN MODULE CONFIG + +const rssi = [ + { + chat:'CHAT_ID', + url: 'RSS_URL', + } +]; + +module.exports = function(rssi){ + this.rssi = rssi; + return { + name : "RSS-FEED", + key : "rssf", + version : 0.1, + requirements : { + gps : false, + }, + text : [ + + ], + reply : [], + regex : [], + media : [], + callback : [], + inline : [], + new_member : (bot,event)=>{}, + weburl : [], + cron : [ + { + cstr : '0 0,30 */1 * * *', + public : true, + desc: { + 0: "Informs Group using RSS feed", + 1: "Informe le Groupe avec le feed RSS", + 4: "Informiert die Gruppe durch dem RSS feed", + }, + params : [ null, true], + timezone : 'Europe/Zurich', + action : (bot)=> { + this.bot = bot; + return ()=>{ + return this.rssi.forEach(entry=> + db.get_v(entry.chat).then(v=> + get_rss(entry.url,v).then(articles=> + articles.forEach(art=> + db.set_v(entry.url,parseInt(art.guid)).then(rr=> + this.bot.telegram.sendMessage(entry.chat,article_to_mess(art),{"parse_mode":"Markdown"}) + ) + ) + ) + ) + ) + }; + } + } + ] + } +} \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..690e90b --- /dev/null +++ b/db.js @@ -0,0 +1,58 @@ +const low = require('lowdb'); +const FileSync = require('lowdb/adapters/FileSync'); + +var db = null; + +function db_init(db){ + db.defaults({ chats:[]}).write(); +} + +var dbc = function(name){ + const db_file = "./db/"+name+".json"; + var db = low(new FileSync(db_file)); + db_init(db); + return db; +}; + + + +module.exports = function(name){ + this.name = name; + this.db = dbc(name); + + this.get_chat = function(cid){ + + return new Promise((resolve, reject)=>{ + let res = this.db.get("chats").find({id:cid}).value(); + if(res) return resolve(res); + + this.db.get("chats").push({id:cid, tracking:[]}).write(); + res = this.db.get("chats").find({id:cid}).value(); + if(res) return resolve(res); + + return reject({error:true, msg:"Error With DB..."}); + }); + }; + + this.dump_db = function(){ + return new Promise((resolve, reject)=>{ + resolve(this.db.get("chats").value()); + }); + }; + + this.set_v = function(cid,v){ + this.get_chat(cid); + return new Promise((resolve, reject)=>{ + resolve(this.db.get("chats").find({id:cid}).set('tracking',v).write()); + }); + }; + + this.get_v = function(cid){ + this.get_chat(cid); + return new Promise((resolve, reject)=>{ + resolve(this.db.get("chats").find({id:cid}).get('tracking').value()); + }); + } + + return this; +}; diff --git a/manual.js b/manual.js new file mode 100644 index 0000000..671b99d --- /dev/null +++ b/manual.js @@ -0,0 +1,43 @@ +const Parser = require('rss-parser'); +const parser = new Parser(); + +const ORG = { + CH : 1, + BR: 1070, + EDI: 301, + BAG : 317, +}; + + + +const base_url = 'https://www.newsd.admin.ch/newsd/feeds/rss'; +//https://www.news.admin.ch/dienstleistungen/00008/00131/00146/index.html?lang=fr&rss-id=1_35 +const get_url = function(opts){ + const lang = opts.lang || 'de'; + const org_nr = opts.org || '301'; + const offer_nr = opts.offer || ''; + const topic = opts.topic || ''; + const sdate = opts.sdate || opts.since || '-1'; + const edate = opts.edate || ''; + const kind = opts.kind || 'M';//M,R,... + return base_url+'?'+ + '&lang='+lang+ + '&org-nr='+org_nr+ + '&topic='+topic+ + //'&keyword='+ + '&offer-nr='+offer_nr+ + //'&catalogueElement='+ + '&kind='+kind+ + '&start_date='+sdate+ + '&end_date='+edate; +} + +const get_rss = function(url,min_id){ + return parser.parseURL(url).then(feed=>feed.items + .sort((a, b)=>(parseInt(a.guid)-parseInt(b.guid))) + .filter((v)=>(parseInt(v.guid)>min_id))); +} +get_rss(get_url({org:ORG.BR, lang:'de',since:-2}),79710).then(r=>r.forEach(item=>{ + console.log(item) + //console.log(`${item.title} (${item.guid}): ${item.link}`); +}));