profile

前段时间看到Riusksk大佬分享的RSS源,然后又在gayhub看到一个项目CyberSecurityRSS

于是,就有了这个“懒人feedlybot”的诞生~

feedlybot

# coding:utf-8
import time
from datetime import datetime
import requests
import pymongo

# feedly api url
base = 'https://cloud.feedly.com/v3/'
# api auth token
# https://feedly.com/v3/auth/dev 获取api token
header = {'Authorization': 'OAuth <your token>'}



def getCategories(Total):
    # 获取category
    category = requests.get(base + 'collections', headers=header)
    for each in category.json():
        # 刷新并获取所有feed订阅
        # each['id']为feed所对应的唯一id
        # each['label']为你feedly所对应的category
        # Total为所存入的collection
        refresh(each['id'], each['label'], Total)

def refresh(feedId, label, collection):
    # 根据feedapi提示,要将某些字符进行转义
    feedId = feedId.replace("/", "%2F")
    feedId = feedId.replace(":", "%3A")
    # 每日0点
    zero_time = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp()
    # 获取当天0点之后该feedid所有的文章
    result = requests.get(base + "streams/contents?streamId={0}".format(feedId), headers=header, params={'newerThan' : int(zero_time * 1000)})
    # 获取当天api已使用次数
    count = result.headers['X-RateLimit-Count']
    result = result.json()


    for each in result['items']:
        try:
            # 源title
            origin = each['origin']['title']

            # 文章链接
            if(re.match('^http.*', each['originId']) != None):
                href = each['originId']
            else:
                href = each['alternate'][0]['href']
            # 文章标题
            title = each['title']
            # 发布时间
            pubtime = timeStamp(each['published'])
            pubtimestamp = each['published']


            data = {
                'category' : label,
                'feedtitle' : origin,
                'articletitle' :title,
                'pubtime' : pubtime,
                'pubtimestamp' : pubtimestamp,
                'lastcheck' : int(round(time.time() * 1000))
            }
            #  去重插入
            r = collection.update({'articleurl' : href}, {'$setOnInsert' : data}, upsert=True)
            # print(r)
            # 打印结果
            print("\n[+] " + count + " " + str(pubtime) + " " + origin + " " + title + " " + href)
        except Exception as e:
        print(db['error'].update({'articleurl' : each['originId']}, {'$setOnInsert' : {'errortime' : time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), 'feed' : each['origin']['streamId']}}, upsert=True))

def timeStamp(timeNum):
    # feedlyapi默认返回13位时间戳,这里格式化便于查看
    timeStamp = float(timeNum/1000)
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return otherStyleTime

if __name__ == '__main__':
    # 将所有获得的数据入库
    client = pymongo.MongoClient(host='localhost', port=27017)
    db = client['feedly']
    Total = db['Total']
    times = 1
    while 1:
        getCategories(Total)
        time.sleep(21600)

Integrate with Coolq

接下来就是懒人时刻了…… 由于feedly每日api请求只有250个,如果你一天中午晚上各更新一次,基本上不用将数据入库,只需要将print返回的结果发送到coolq的http api接口,就可以直接发送给你了。

因为我后期还可能会给Coolq Bot加一些其他功能,所有我这里是选择了入库,然后根据Http Api的接口数据,返回数据库查询数据即可。

我这里用的是Flask的api,可以根据自己的需要选择coolq-http-api

# -*- coding: utf-8 -*-
# filename: coolq.py
# author: 0aKarmA

from flask import Flask, request
from cqhttp import CQHttp
import os
import time
import re

import mongo

bot = CQHttp(api_root='http://127.0.0.1:5700/',
             access_token='token',
             secret='secrect')

def funcs(context):
    if(context['message'].split(" ")[0] == '/news'):
        try:
            num  = context['message'].split(" ")[1]
            bot.send(context, message=mongo.getdata(num.split(",")[0], num.split(",")[1]), auto_escape=True)
        except Exception as e:
            bot.send(context, message='Usage: /news <from>,<to>')
    elif(context['message'] == '/help'):
        bot.send(context, message='Usage: /command \nCommands:\n    /search <one keyword> <from>,<to> 查询关键字相关文章\n    /news 查询当天最新资讯\n?', auto_escaoe=True)
    elif(context['message'].split(" ")[0] == '/search'):
        try:
            num  = context['message'].split(" ")[2]
            bot.send(context, message=mongo.searchdata(context['message'].split(" ")[1], num.split(",")[0], num.split(",")[1]), auto_escape=True)
        except Exception as e:

            bot.send(context, message='Usage: /search <one keyword> <from>,<to>')

    else:
        bot.send(context, message="D0g3的小萌宠提醒您: \n  Run '/help' for more information on a command.", auto_escape=True)


@bot.on_message()
def handle_msg(context):
    funcs(context)

@bot.on_notice('group_increase')  # 如果插件版本是 3.x,这里需要使用 @bot.on_event
def handle_group_increase(context):
    bot.send(context, message='欢迎新人~', auto_escape=True)  # 发送欢迎新人


@bot.on_request('group', 'friend')
def handle_request(context):
    return {'approve': True}  # 同意所有加群、加好友请求


bot.run(host='127.0.0.1', port=8080,debug=0)

基本实现了几个常用功能 /news <from>,<to> * 0F1D441E5F0F48A548406AB3B25C3FAF /search <keyword> <from>,<to> * IMG_4032-20190420-165824-

实现了这两个功能之后,拉到群里发现。。他好像不支持像coolq原生小i机器人一样,在群里面需要@一下才会出来。因为在群里如果消息是以@D0g3 Daily开头,他是收不到消息的。不知道为什么。如果消息是/help @D0g3 Daily这样的话,他会获取到一个CQ码,但是这样对数据处理造成负担,所以我暂时放弃了。先这样用着吧。

Integrate with server酱 / push bear

这个就更简单了,只需要像coolq一样,将你要发送的消息发送到指定接口即可。

Reference

The feedly Cloud API Unofficial Feedly API Document