def update(uid, status="0"): # uid = "6355968578" name, result = dispose(getRawJson(uid)) for blog in result: # print(blog) timestamp = blog.get("timestamp") times = blog.get("time") content = blog.get("content") res = writeSQL(uid, timestamp, times, content) # 数据初始化 if status == "1": continue if res == "1": # 数据更新推送 print("[NewWeiBo]%s发现新数据! UID:%s(%s) content:%s" % (times, name, uid, content)) return times, name, uid, content elif res == "0": continue else: log.error("出现错误!") return if status == "1": print("[Weibo]%s UID:%s 初始化完成!" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), uid)) else: print("[Weibo]%s UID:%s(%s)无更新" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), name, uid))
def pushGoup(self, content): url = "https://push.xuthus.cc/group/%s" % (self.token) data = { "c": content, } try: resp = requests.get(url, headers=self.headers, params=data) if resp.json()["code"] != 200: log.error("[CoolPush]推送出现异常,响应:%s" % (resp.text)) else: log.info("[CoolPush]推送成功") except: log.error("[CoolPush]推送失败!")
def getRawJson(uid): headers = { 'authority': 'm.weibo.cn', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'accept': 'application/json, text/plain, */*', 'mweibo-pwa': '1', 'x-xsrf-token': 'fb7304', 'x-requested-with': 'XMLHttpRequest', 'sec-fetch-dest': 'empty', 'user-agent': UserAgent().safari, 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'cors', 'referer': r'https://m.weibo.cn/u/{}?uid={}&t=0&luicode=10000011&lfid=100103type%3D1%26q%3D%E5%85%89%E9%81%87'.format(uid, uid), 'accept-language': 'zh-CN,zh;q=0.9', } params = { "page": "0", 'uid': '%s' % uid, 't': '0', 'luicode': '10000011', 'lfid': '100103', 'type': '1', 'type': 'uid', 'value': '%s' % uid, # weibo用户标识符 'containerid': '107603%s' % uid, } try: response = requests.get( 'https://m.weibo.cn/api/container/getIndex', headers=headers, params=params, verify=False).json() except Exception as e: log.error("请求时出现错误(%s)60s后将重试" % e) time.sleep(60) getRawJson(uid) return response
def dispose(data): if data["ok"] != 1: log.error("数据不正常!60s后重试") time.sleep(60) dispose(getRawJson(uid)) else: try: get_id = data["data"]["cards"][0]["mblog"]["user"]["id"] get_name = data["data"]["cards"][0]["mblog"]["user"]["screen_name"] except: log.error("用户信息获取失败!请检查UID") else: log.info("用户信息如下: UID:%s 名字:%s" % (get_id, get_name)) # 获取微博 blogs = data["data"]["cards"] blogsList = [] for blog in blogs: html = blog["mblog"]["text"] blogId = blog["mblog"]["id"] created_at = blog["mblog"]["created_at"] created_at = datetime.datetime.strptime( created_at, '%a %b %d %H:%M:%S +0800 %Y') timestamp = int(created_at.timestamp()) created_at = created_at.strftime("%Y-%m-%d %H:%M:%S") # print(html) # 处理全文缺省内容 if "全文" in str(html): # https://m.weibo.cn/statuses/extend?id=4602297956709241 # print(blogId) url = "https://m.weibo.cn/statuses/extend?id=%s" % blogId headers = { "Referer": "https://m.weibo.cn/status/%s" % blogId, "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Mobile Safari/537.36" } try: resp = requests.get( url, headers=headers, verify=False).json() html = resp["data"]["longTextContent"] except Exception as e: log.error("获取全文失败!") htm = etree.HTML(html) # 敲黑板: string(.) 可以得到标签下的所有text() text = htm.xpath("string(.)").replace( "\n", "").replace("\r", "") # print(created_at, text) # print("----------------------------------") content = { "time": created_at, "timestamp": timestamp, "content": text, } blogsList.append(content) # print(blogsList) return get_name, blogsList
# "flashPic": "false" # 闪照:仅群聊可用 # } # resp = requests.post(self.api, params=params, # data=json.dumps(data), headers=self.header) # print(resp.json()) ''' if __name__ == "__main__": # uid = "6355968578" try: weiboList, updateTime, CoolPushToken = getConfig() except Exception as e: log.error("请检查配置文件config.ini") # 实例化酷推 cp = CoolPush(CoolPushToken) # opqbot bot = OPQBot("http://192.168.101.4:8888", 2593923636) # bot.getGroupList() # 初始化 for uid in weiboList: update(uid, "1") time.sleep(10) while True:
# -*- coding: utf-8 -*- from function import log __author__ = 'Administrator' log.set_logger( filename = 'mylog.log', mode='a',level='ERROR:INFO') log.debug('hello, world') log.info('hello, world') log.warning('hello, world') log.error('hello, world') log.critical('hello, world')