-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuzz2weibo.py
executable file
·194 lines (158 loc) · 5.23 KB
/
buzz2weibo.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
# buzz2weibo
# Copyright 2011 Sun Zhigang
# See LICENSE for details.
from weibo import APIClient, APIError
from config import *
from urllib2 import urlopen, URLError
from json import load
from activity import *
from time import sleep
import os, errno, sys
import Image
WEIBO_APP_KEY = '3127127763'
WEIBO_APP_SECRET = '21cc35f55fc8fe73b73162964c0bb415'
# 运行一次最多同步几条。缺省3。连续同步太多会被休息的
WEIBO_MAX_SYNC_COUNT = 3
'''Concatenate multiply images as one from top to down'''
def catimages(image_files):
if len(image_files) <= 1:
return image_files[0]
images = []
max_width = 0
'''Load images and get the max width'''
for f in image_files:
img = Image.open(f)
images.append(img)
width, height = img.size
if width > max_width:
max_width = width
'''resize all images to max_width'''
total_height = 0
resized_images = []
for img in images:
width, height = img.size
new_height = max_width / width * height
resized_images.append(img.resize((max_width, new_height)))
total_height = total_height + new_height
'''concatenate them'''
result_img = Image.new('RGB', (max_width, total_height))
ypos = 0
for img in resized_images:
result_img.paste(img, (0, ypos))
ypos = ypos + img.size[1]
imagefile = IMAGES_PATH + '/' + 'final.jpg'
result_img.save(imagefile)
return imagefile
def post2weibo(client, act):
message = act.content + ' ' + act.link
if APPEND_SHARE_FROM_BUZZ_LINK:
message += u' //转发自%s'.encode('utf-8') % act.origin_link
if message == ' ':
message = act.origin_link
imagefiles = []
for image in act.images:
# 下载图像文件
try:
u = urlopen(image.url);
data = u.read()
u.close()
except URLError:
# 如果下载不下来,表示……,就别试了,当普通消息发吧
pass
else:
filename = IMAGES_PATH + '/' + image.filename
f = open(filename, 'w')
f.write(data)
f.close()
imagefiles.append(filename)
if len(imagefiles) > 0:
imagefile = catimages(imagefiles)
while (True):
try:
if len(imagefiles) > 0:
f = open(imagefile, 'rb');
client.statuses.upload.post(status=message, pic=f, lat=act.geo[0], long=act.geo[1])
f.close()
else:
client.statuses.update.post(status=message, lat=act.geo[0], long=act.geo[1])
except APIError, e:
print e
if e.error_code == 20012:
#20012 : 输入文字太长,请确认不超过140个字符
#微博太长,剪裁且留原始链接。原始链接不会太长,所以不会死循环
message = unicode(message, 'utf-8')[0:80] + u'....更多:'
message = message.encode('utf-8') + act.origin_link
print '内容过长,截断发表:'
print message
else:
break
#raise
else:
break
return True
# 测试config.py文件是否存在
if not os.path.exists(sys.path[0] + os.sep + 'config.py'):
print '找不到配置文件。请先运行setup.py'
sys.exit(1)
# 建图片目录
try:
os.makedirs(IMAGES_PATH)
except OSError, e:
if e.errno == errno.EEXIST:
pass
else:
raise
buzz_url = 'https://www.googleapis.com/plus/v1/people/' + BUZZ_USERID + '/activities/public?key=' + GOOGLE_API_KEY
# 读buzz
fp = urlopen(buzz_url)
#fp = open('buzz.json')
buzz = load(fp)
fp.close()
# 微博认证
client = APIClient(WEIBO_APP_KEY, WEIBO_APP_SECRET, 'https://api.weibo.com/oauth2/default.html')
client.set_access_token(WEIBO_TOKEN, WEIBO_TOKEN_EXPIRES);
# 读已经同步过的activity id
synced_ids = set()
try:
fp = open(HISTORY_FILE, 'r')
for line in fp:
synced_ids.add(line.strip())
fp.close()
except IOError, e:
# 如果文件不存在,就继续;否则,触发异常
if e.errno != errno.ENOENT:
raise
# 开始同步
count = 0
items = buzz['items']
items.reverse() # Buzz是后发的在前,所以翻转一下。感谢王瑞珩的建议
for item in items:
act = GooglePlusActivity(item);
# 同步未同步过的
if act.id not in synced_ids:
print '-----------------------'
print 'syncing ' + act.id
print act.origin_link
if act.content != '':
print act.content
if act.link != '':
print act.link
for image in act.images:
print image.url
if act.geo != '':
print act.geo
if DEBUG:
continue
if post2weibo(client, act):
synced_ids.add(act.id)
# 将同步过的activity id写入历史文件
fp = open(HISTORY_FILE, 'w')
for id in synced_ids:
fp.write(id + '\n')
fp.close()
count = count + 1
if count >= WEIBO_MAX_SYNC_COUNT:
break
sleep(1) # 延时才能让新浪微博按正确顺序显示