forked from 786662216/weibo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentWeiBo_Console.py
132 lines (119 loc) · 6.2 KB
/
SentWeiBo_Console.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
# coding:utf-8
import optparse
import random
from weibo import APIClient
import datetime
import time
import os
#################################################################################
u'''
一个可以定时发送微博的小脚本
-h, --help show this help message and exit
-t T, --time=T -t 设定每天几点发。如 -t 8;默认4点发,24小时制。
-p, --picture -p 随机发送图片
-y, --yingyingying -y 报时+卖萌
-r, --random -r 每天从以上内容中随机选择一种发送
例如 python SentWeiBo_Console.py -h 19 -p 就是每天19点发送一次图片
'''
################################################################################
#新浪微博API的验证信息
APP_KEY = '2333333' # app key
APP_SECRET = '*^$%^&%$*%*%&*^' # app secret
CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html' # callback url
access_token = "suzhisanlianC"
expires_in = 1660225135
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
client.set_access_token(access_token, expires_in)
#################################################################################
#命令行
parser = optparse.OptionParser()
parser.add_option('-t','--time',dest='t',default="4",type='string',help = u'-t 设定每天几点发。如 -t 8;默认4点发,24小时制。')
# parser.add_option('-f','--frequency',dest='f',default="days=1",type='string',help = u'-t 设定发送频率,默认一天一次')
parser.add_option('-p','--picture',action="store_true",dest='p',help = u'-p 随机发送图片')
parser.add_option('-y','--yingyingying',action="store_true",dest='y',help = u'-y 报时+卖萌')
parser.add_option('-r','--random',action="store_true",dest='r',help = u'-r 每天从以上内容中随机选择一种发送')
(option, args) = parser.parse_args()
c = int(option.t)
pic = option.p
ying = option.y
ran = option.r
# f = option.f
aaa = ['pic','ying']
##########################################################################################
#功能函数
def yingyingying():#报时+颜表情卖萌
TimeSecound = time.time()
date = time.localtime(TimeSecound) # 现在的具体时间
DateMday = date[2]
DateHour = date[3]
str = u'''咚~咚~咚~%s,现在是%d年%d月%d日%d时%d分%d秒,
现在我有其他功能了哦~ http://www.google.com 404喽%s''' % (random.choice(
[u'(๑•̀ㅂ•́)و✧', u'ヾ(≧▽≦*)o ', u'o(*≧▽≦)ツ ', u'(o゜▽゜)o☆', u'<( ̄︶ ̄)>', u'o(* ̄▽ ̄*)o ', u'(。・∀・)ノ゙',
u'ヾ(≧∇≦*)ゝ',
u'Hi~ o(* ̄▽ ̄*)ブ ', u'(≧∀≦)ゞ', u'ε(*´・∀・`)з゙', u'(~ ̄▽ ̄)~ ', u'︿( ̄︶ ̄)︿', u'(/≧▽≦)/', u'(ノ*・ω・)ノ',
u'o(〃\'▽\'〃)o ', u'o( ̄▽ ̄)d ', u'o(^▽^)o']), date[0], date[1], DateMday, DateHour, date[4], date[5],
random.choice(
[u'w(゚Д゚)w', u'O(≧口≦)O', u'Σ(`д′*ノ)ノ', u'ヽ(*。>Д<)o゜',
u'┻━┻︵╰(‵□′)╯︵┻━┻', u'φ(-ω-*)', u'(σ`д′)σ',
u'(#`O′)', u'( >ρ < ”)', u'o(一︿一+)o', u'(#`O′) ',
u'(>﹏<)', u'(;′⌒`) ', u'(;´д`)ゞ', u'Σ( ° △ °|||)︴',
u'(lll¬ω¬)', u'…(⊙_⊙;)…', u'_〆(´Д` ) ',
u'Σ(っ °Д °;)っ', u'(・-・*)', u'(°ー°〃) ',
u'(((φ(◎ロ◎;)φ)))', u'o((⊙﹏⊙))o.', u'ヽ(*。>Д<)o゜ ', ]))
client.statuses.share.post(status=str) # 这是有返回值的
def picture():#随机发送图片
with open('./haha.txt', 'r') as f:
a = f.read()
a = a.split('\r\n\r\n')
haha = random.choice(a)
a.remove(haha)
with open('./haha.txt', 'w') as f:
for i in a:
if i == '' or i == '\n':
a.remove(i)
continue
f.write(i)
f.write('\r\n')
f.write('\r\n')
picture = random.choice(piclist)
piclist.remove(picture)
str = '%s\nhttp://www.google.com图文无关' % haha
pic = open('./pic/%s' % picture, 'rb')
client.statuses.share.post(status=str, pic=pic)
pic.close()
os.remove('./pic/' + picture)
def jiangzemin(f):#判断时间到没到,到了就执行f()
while True:
t = datetime.datetime.now()
schedtime = datetime.datetime(t.year, t.month, t.day, c, c, c) # 要执行的时间
time.sleep(1) # 循环执行的速度太快,虽然只有一秒但也会执行很多次,所以要等1s
now = datetime.datetime.now() # 返回值里面有微秒可把我坑惨了
if now.date() == schedtime.date(): # 先判断日期相不相同
if (now.hour == schedtime.hour) and (now.minute == schedtime.minute) and (now.second == schedtime.second): # 在判断时间相不相同,主要是为了避开微秒的比较
f()
# schedtime = schedtime + Frequency
print now, u"sent successfully"
else:
pass
else:
pass
def randomm():#随机选一个函数执行
r = random.choice(aaa)
if r == 'pic':
picture()
if r == 'ying':
yingyingying()
################################################################################
if __name__ == '__main__':
piclist = os.listdir('./pic/')#放图片的目录
Frequency = datetime.timedelta(days=1)# 频率
if ran == None:
if (pic == None and ying == None) or (not pic == None and not ying == None):
print u'参数有误'
exit(0)
if not pic == None:
jiangzemin(picture)
if not ying == None:
jiangzemin(yingyingying)
if ran == True:
jiangzemin(randomm)