-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
175 lines (149 loc) · 6.72 KB
/
main.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author : B1ain
# Action : 微博
# Desc : 微博主模块
import requests, json, sys
# import getpic
import content
import htmljiexi
from urllib.parse import quote
import urlencode
import traceback
class weiboMonitor():
def __init__(self, ):
self.reqHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'https://passport.weibo.cn/signin/login',
'Connection': 'close',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'
}
# self.uid = ['6395178860', '1906286443']
aa = urlencode.urlencode('社会队树洞')
bb = urlencode.urlencode('社会队小道消息')
cc = urlencode.urlencode('社会人衣装')
self.uid = [aa, bb,cc]
# 获取访问连接
def getweiboInfo(self):
try:
self.weiboInfo = []
self.itemIds = []
for i in self.uid:
# userInfo = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=%s' % (i)
userInfo = 'https://m.weibo.cn/api/container/getIndex?containerid=231522type%s&page_type=searchall' % (i)
res = requests.get(userInfo, headers=self.reqHeaders,stream=True, verify=False,timeout=30)
# testt=res.text
d = res.json()['data']['cards']
# d=str(d)
# print(testt)
# print(type(testt))
#
# print(type(self.weiboInfo))
with open('wbIds.txt', 'a') as f:
for j in d:
if j['card_type'] == 9:
f.write(j['mblog']['id'] + '\n')
self.itemIds.append(j['mblog']['id'])
self.echoMsg('Info', '微博数目获取成功')
self.echoMsg('Info', '目前有 %s 条微博' % len(self.itemIds))
except Exception as e:
print(traceback.format_exc())
pass
# self.echoMsg('Error', e)
# sys.exit()
#
# # 收集已经发布动态的id
# def getWBQueue(self):
# try:
# self.itemIds = []
#
# for i in self.weiboInfo:
#
# res = [i][self.weiboInfo]
# print(res)
#
# with open('wbIds.txt', 'a') as f:
# for j in res:
# print(j)
# # if j['card_type'] == 9:
# # f.write(j['mblog']['id'] + '\n')
# # self.itemIds.append(j['mblog']['id'])
# self.echoMsg('Info', '微博数目获取成功')
# self.echoMsg('Info', '目前有 %s 条微博' % len(self.itemIds))
# except Exception as e:
# self.echoMsg('Error', e)
# sys.exit()
# 开始监控
def startmonitor(self, ):
returnDict = {} # 获取微博相关内容
try:
itemIds = []
with open('wbIds.txt', 'r') as f:
for line in f.readlines():
line = line.strip('\n')
itemIds.append(line)
for i in self.uid:
# userInfo = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=%s' % (i)
userInfo = 'https://m.weibo.cn/api/container/getIndex?containerid=231522type%s&page_type=searchall' % (
i)
res = requests.get(userInfo, headers=self.reqHeaders,stream=True, verify=False,timeout=30)
# testt=res.text
d = res.json()['data']['cards']
for j in d:
if j['card_type'] == 9:
if str(j['mblog']['id']) not in itemIds:
with open('wbIds.txt', 'a') as f:
f.write(j['mblog']['id'] + '\n')
self.echoMsg('Info', '发微博!')
self.echoMsg('Info', '目前有 %s 条微博' % (len(itemIds) + 1))
print("最新的是id:"+str(j['mblog']['id']))
dayin="https://m.weibo.cn/status/"
print("最新的微博链接是:"+dayin+str(j['mblog']['id']))
idd = str(j['mblog']['id'])
# 以下输出微博内容
txt = j
createtime = j['mblog']['created_at']
sourcel = j['mblog']['source']
fasname = j['mblog']['user']['screen_name']
# 推送到iPhonepushdeer
htmljiexi.iphonepushdeer(fasname,idd)
try:
deit = j['mblog']['edit_config']['edited']
except:
deit = ''
reposts = j['mblog']['reposts_count']
attitudes = j['mblog']['attitudes_count']
comments = j['mblog']['comments_count']
picnum = j['mblog']['pic_num']
content.wbcontent(txt, createtime, sourcel, fasname, deit, reposts, attitudes, comments,
idd)
# 以下输出微博图片
htmljiexi.getpiclast(idd)
# urll = i
# getpic.getweibopic(idd, urll)
# print("这是微博id" + str(j['mblog']['id'])) # 这是微博id
# print("这是微博url的链接" + i) # 这是微博url的链接
# print(j) # 这是微博的【】内容是list
returnDict['created_at'] = j['mblog']['created_at']
returnDict['text'] = j['mblog']['text']
returnDict['source'] = j['mblog']['source']
returnDict['nickName'] = j['mblog']['user']['screen_name']
return returnDict
except Exception as e:
self.echoMsg('Error', e)
sys.exit()
# 格式化输出
def echoMsg(self, level, msg):
if level == 'Info':
print('[Info] %s' % msg)
elif level == 'Error':
print('[Error] %s' % msg)
if __name__ == '__main__':
w = weiboMonitor()
# w.getweiboInfo()
with open('wbIds.txt', 'r') as f:
text = f.read()
if text == '':
w.getweiboInfo()
newWB = w.startmonitor()