forked from JMSCHKU/Social
/
sinaweibo.oauth.py
executable file
·933 lines (895 loc) · 30.8 KB
/
sinaweibo.oauth.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import pg
import mypass
import httplib
import simplejson
import time
import string
import oauth2 as oauth
import pprint
import datetime
import csv
import types
import weibopy # we use our own slightly-extended version of weibopy
import unittest
import socket
#from weibopy.auth import OAuthHandler, BasicAuthHandler
#from weibopy.api import API
import lucene
import sinaweibolucene
class SinaWeiboOauth():
sinaweiboOauth = mypass.getSinaWeiboOauth()
pgconn = None
toleranceNotToBeginning = 5 # in fetching timelines, break from loop when toleranceNotToBeginning consecutive statuses already exist in the DB
toleranceNotToBeginningLong = 150 # for reposts
max_gotall_count = 3
api_wait_secs = 5
max_api_misses_half = 3
max_api_misses = 6
max_reposts_pages = max_comments_pages = 1000
max_reposts_blanks = max_comments_blanks = 3
max_reposts_tries = max_comments_tries = 3
usage = "sinaweibo.oauth.py [id or file with ids] [primary opts] [sec opts]"
rp_dir = "/var/data/sinaweibo/rp"
comments_dir = "/var/data/sinaweibo/comments"
reposts_dir = "/var/data/sinaweibo/reposts"
verbose = False
getall = False
force_screenname = False
checkonly = False
doupdate = False
saveRP = False
index = False
indexer = None
def __init__(self):
socket.setdefaulttimeout(300)
def getAtt(self, obj, key):
try:
return obj.__getattribute__(key)
except Exception, e:
return None
def setAtt(self, obj, key, value):
try:
return obj.__setattribute__(key, value)
except Exception, e:
return None
def setToken(self, token, tokenSecret):
self.auth = weibopy.auth.OAuthHandler(self.sinaweiboOauth["consumer_key"], self.sinaweiboOauth["consumer_secret"])
self.auth.setToken(token, tokenSecret)
self.api = weibopy.API(self.auth)
def auth(self):
self.auth = weibopy.auth.OAuthHandler(self.sinaweiboOauth["consumer_key"], self.sinaweiboOauth["consumer_secret"])
auth_url = self.auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
self.auth.get_access_token(verifier)
self.api = weibopy.API(self.auth)
def status_to_row(self, status):
x = dict()
x["created_at"] = self.getAtt(status, "created_at").strftime("%Y-%m-%d %H:%M:%S")
for a in ["text", "source", "location", "thumbnail_pic", "bmiddle_pic", "original_pic", "screen_name", "in_reply_to_screen_name"]:
try:
att = self.getAtt(status, a)
x[a] = att.encode("utf8")
except:
x[a] = att
for a in ["id", "in_reply_to_user_id", "in_reply_to_status_id", "truncated"]:
x[a] = self.getAtt(status, a)
try:
rts = self.getAtt(self.getAtt(status, "retweeted_status"), "id")
except:
rts = None
if rts is not None:
x["retweeted_status"] = rts
try:
user_id = self.getAtt(self.getAtt(status, "user"), "id")
except:
user_id = None
try:
screen_name = self.getAtt(self.getAtt(status, "user"), "screen_name").encode("utf8")
except:
screen_name = None
if user_id is not None:
x["user_id"] = user_id
if screen_name is not None:
x["screen_name"] = screen_name
try:
geo = self.getAtt(status, "geo")
coord = self.getAtt(geo, "coordinates")
except:
geo = None
if geo is not None and coord is not None and len(coord) > 0:
#print coord
lat = coord[0]
lng = coord[1]
wkt_point = "POINT(" + str(lng) + " " + str(lat) + ")"
#print wkt_point
x["geo"] = "SRID=4326;" + wkt_point
'''
sql = "UPDATE %(table_name)s SET geo = ST_GeomFromText('%(wkt_point)s', 4326) WHERE id = %(id)d " % {"table_name": "sinaweibo", "wkt_point": wkt_point, "id": x["id"]}
try:
self.pgconn.query(sql)
except:
print sql
print "geo error: " + wkt_point
'''
return x
def user_to_row(self, user):
x = dict()
x["created_at"] = self.getAtt(user, "created_at").strftime("%Y-%m-%d %H:%M:%S")
x["retrieved"] = "NOW()"
for a in ["name", "screen_name", "location", "description", "profile_image_url", "url"]:
try:
att = self.getAtt(user, a)
x[a] = att.encode("utf8")
except:
x[a] = att
for a in ["id", "province", "city", "domain", "gender", "followers_count", "friends_count", "favourites_count", \
"time_zone", "profile_background_image_url", "profile_use_background_image", "allow_all_act_msg", "geo_enabled", "verified", "following", "statuses_count"]:
x[a] = self.getAtt(user, a)
return x
def comment_to_row(self, comment):
x = dict()
x["created_at"] = self.getAtt(comment, "created_at").strftime("%Y-%m-%d %H:%M:%S")
try:
text = self.getAtt(comment, "text")
if len(text) > 0:
x["text"] = text.encode("utf8")
except:
x["text"] = text
x["id"] = self.getAtt(comment, "id")
x["user_id"] = self.getAtt(self.getAtt(comment, "user"), "id")
return x
def get_status(self, id, getUser=False, toDB=True):
time_db = 0
time_db_u = 0
start_time_api = time.time()
api_misses = 0
while api_misses < self.max_api_misses:
try:
status = self.api.get_status(id=id)
break
except weibopy.error.WeibopError as e:
print e.reason
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": e.reason }
if e.reason.find("Error: target weibo does not exist!") >= 0:
try:
rps = self.getRangePartitionByIds([id])
for x in rps:
yw = x.split(",")
year = int(yw[0])
week = int(yw[1])
sql_deleted = "UPDATE rp_sinaweibo_y%(year)dw%(week)d SET deleted = NOW() WHERE id = %(id)d AND deleted IS NULL " % { "year": year, "week": week, "id": id }
if self.pgconn is None:
self.pgconn = mypass.getConn()
res = self.pgconn.query(sql_deleted)
except pg.ProgrammingError, pg.InternalError:
print self.pgconn.error
return { "msg": e.reason }
time.sleep(self.api_wait_secs)
time_api = time.time() - start_time_api
if self.saveRP:
self.saveRangePartitionByIdDate(id, self.getAtt(status, "created_at"))
row = self.status_to_row(status)
r = dict()
if toDB and not self.checkonly:
start_time_db = time.time()
tablename = self.getRangePartitionByDate(self.getAtt(status, "created_at"))
#tablename = "rp_sinaweibo"
resp = self.toDB(tablename, row, self.doupdate)
time_db += time.time() - start_time_db
r = resp
resp["time_db"] = time_db
if getUser:
timeline_user = self.getAtt(status, "user")
start_time_db_u = time.time()
u = self.user_to_row(timeline_user)
resp_u = self.toDB("sinaweibo_users", u)
time_db_u += time.time() - start_time_db_u
r["time_db_u"] = time_db_u
else:
print row
r["time_api"] = time_api
return r
def user_timeline(self, user_id, count=200, page=1):
start_time_api = time.time()
api_misses = 0
while api_misses < self.max_api_misses:
try:
timeline = self.api.user_timeline(count=count, page=page, user_id=user_id)
break
except httplib.IncompleteRead as h:
print h
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": h }
time.sleep(self.api_wait_secs)
except weibopy.error.WeibopError as e:
print e.reason
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": e.reason }
time.sleep(self.api_wait_secs)
if string.find(e.reason, "requests out of rate limit") >= 0:
self.waitRateLimit()
except socket.error as e:
print e
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": e.message }
time.sleep(self.api_wait_secs)
'''
except ValueError as e:
print user_id
print e
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": e.message }
time.sleep(self.api_wait_secs)
'''
time_api = time.time() - start_time_api
r = self.status_timeline(timeline, toBeginning=False)
if "count" in r and r["count"] == 0:
if self.pgconn is None:
self.pgconn = mypass.getConn()
#self.pgconn.query("UPDATE sinaweibo_users SET posts_updated = NOW() WHERE id = %d" % user_id)
r["time_api"] = time_api
r["page"] = page
return r
def reposts(self, status_id, count=200, page=1):
already_exists_count = 0
start_time_api = time.time()
api_misses = 0
while api_misses < self.max_api_misses:
try:
timeline = self.api.repost_timeline(count=count, page=page, id=status_id)
break
except weibopy.error.WeibopError as e:
print e.reason
api_misses += 1
if api_misses == self.max_api_misses:
return { "msg": e.reason }
if e.reason.find("Error: target weibo does not exist!") > 0:
try:
rps = self.getRangePartitionByIds([id])
for x in rps:
yw = x.split(",")
year = int(yw[0])
week = int(yw[1])
sql_deleted = "UPDATE rp_sinaweibo_y%(year)dw%(week)d SET deleted = NOW() WHERE id = %(id)d AND deleted IS NULL " % { "year": year, "week": week, "id": id }
if self.pgconn is None:
self.pgconn = mypass.getConn()
res = self.pgconn.query(sql_deleted)
except pg.ProgrammingError, pg.InternalError:
print self.pgconn.error
return { "msg": e.reason }
time.sleep(self.api_wait_secs)
if e.reason.find("requests out of rate limit") >= 0:
if e.reason.find("IP") >= 0 and api_misses <= self.max_api_misses_half:
time.sleep(60) # to consider rolling IPs
else:
self.waitRateLimit()
time_api = time.time() - start_time_api
r = self.status_timeline(timeline, False)
r["time_api"] = time_api
r["page"] = page
return r
# common function to go through a timeline and optionally put in the DB
def status_timeline(self, statuses, isSingleUser=True, toDB=True, toBeginning=True):
already_exists_count = 0
time_db = 0
time_db_u = 0
if self.index:
time_index = 0
deleted_count = 0
timeline_users_ids = list()
toleranceNotToBeginningCount = 0
newlyadded = 0
for l in statuses:
x = self.status_to_row(l)
if toDB:
start_time_db = time.time()
# handle deleted statuses
if "deleted" in x and x["deleted"] is not None and (x["deleted"] == "1" or x["deleted"] == 1 or x["deleted"] is True):
deleted_count += 1
if self.pgconn is None:
self.pgconn = mypass.getConn()
try:
rps = self.getRangePartitionByIds([x["id"]])
for x in rps:
yw = x.split(",")
year = int(yw[0])
week = int(yw[1])
sql_deleted = "UPDATE rp_sinaweibo_y%(year)dw%(week)d SET deleted = NOW() WHERE id = %(id)d AND deleted IS NULL " % { "year": year, "week": week, "id": x["id"] }
res = self.pgconn.query(sql_deleted)
except pg.ProgrammingError, pg.InternalError:
print self.pgconn.error
continue
tablename = self.getRangePartitionByDate(self.getAtt(l, "created_at"))
#tablename = "rp_sinaweibo"
resp = self.toDB(tablename, x)
time_db += time.time() - start_time_db
if not resp["already_exists"] and resp["success"] and not isSingleUser:
timeline_user = self.getAtt(l, "user")
timeline_user_id = self.getAtt(timeline_user, "id")
#print resp
if not timeline_user_id in timeline_users_ids:
start_time_db_u = time.time()
u = self.user_to_row(timeline_user)
resp_u = self.toDB("sinaweibo_users", u)
time_db_u += time.time() - start_time_db_u
if resp_u["already_exists"] or resp_u["success"]:
timeline_users_ids.append(timeline_user_id)
if resp["already_exists"]:
if toBeginning:
already_exists_count += 1
else:
toleranceNotToBeginningCount += 1
if isSingleUser:
if self.verbose:
print "already exists, tolerance: " + str(toleranceNotToBeginningCount)
if toleranceNotToBeginningCount >= self.toleranceNotToBeginning:
break
else:
if toleranceNotToBeginningCount >= self.toleranceNotToBeginningLong:
break
else:
newlyadded += 1
if not toBeginning:
toleranceNotToBeginningCount = 0
if self.index: # index if the row doesn't already exist
time_index_start = time.time()
try:
t = time.strptime(x["created_at"],"%Y-%m-%d %H:%M:%S")
created_at_secs = int(time.mktime(t))
self.indexer.indexWeibo(x["id"], x["text"], x["user_id"], created_at_secs)
except Exception as e:
print e
time_index += time.time() - time_index_start
else:
print x
r = { "count": len(statuses), "deleted_count": deleted_count }
if isSingleUser and toDB and len(statuses) > 0:
u = self.user_to_row(self.getAtt(statuses[0], "user"))
u["last_post_date"] = self.getAtt(statuses[0], "created_at").strftime("%Y-%m-%d %H:%M:%S")
u["posts_updated"] = "NOW()"
start_time_db_u = time.time()
resp_u = self.toDB("sinaweibo_users", u, doupdate=True)
print resp_u
time_db_u += time.time() - start_time_db_u
r["user_id"] = u["id"]
if toDB:
if toBeginning:
r["already_exists_count"] = already_exists_count
r["newly_added"] = newlyadded
r["time_db"] = time_db
r["time_db_u"] = time_db_u
if self.index:
r["time_index"] = time_index
return r
def comments(self, status_id, count=200, page=1, toDB=True, toBeginning=True):
already_exists_count = 0
start_time_api = time.time()
api_misses = 0
while api_misses < self.max_api_misses:
try:
comments = self.api.comments(id=status_id, count=count, page=page)
break
except weibopy.error.WeibopError as e:
print e.reason
api_misses += 1
if api_misses == self.max_api_misses:
return { "msg": e.reason }
if e.reason.find("Error: target weibo does not exist!") > 0:
try:
rps = self.getRangePartitionByIds([status_id])
for x in rps:
yw = x.split(",")
year = int(yw[0])
week = int(yw[1])
sql_deleted = "UPDATE rp_sinaweibo_y%(year)dw%(week)d SET deleted = NOW() WHERE id = %(id)d AND deleted IS NULL " % { "year": year, "week": week, "id": status_id }
if self.pgconn is None:
self.pgconn = mypass.getConn()
res = self.pgconn.query(sql_deleted)
except pg.ProgrammingError, pg.InternalError:
print self.pgconn.error
return { "msg": e.reason }
time.sleep(self.api_wait_secs)
if e.reason.find("requests out of rate limit") >= 0:
if e.reason.find("IP") >= 0 and api_misses <= self.max_api_misses_half:
time.sleep(60) # to consider rolling IPs
else:
self.waitRateLimit()
time_api = time.time() - start_time_api
time_db = 0
time_db_u = 0
comments_users_ids = list()
for l in comments:
x = self.comment_to_row(l)
x["status_id"] = status_id
if toDB:
start_time_db = time.time()
resp = self.toDB("sinaweibo_comments", x)
time_db += time.time() - start_time_db
# user information
comment_user = self.getAtt(l, "user")
comment_user_id = self.getAtt(comment_user, "id")
if not resp["already_exists"] and resp["success"] and not comment_user_id in comments_users_ids: # update a user once per cycle
start_time_db_u = time.time()
u = self.user_to_row(comment_user)
resp_u = self.toDB("sinaweibo_users", u)
time_db_u += time.time() - start_time_db_u
if resp_u["already_exists"] or resp_u["success"]:
comments_users_ids.append(comment_user_id)
#print resp
if resp["already_exists"]:
if toBeginning:
already_exists_count += 1
else:
break
else:
print x
r = { "count": len(comments), "time_api": time_api, "page": page }
if toDB:
if toBeginning:
r["already_exists_count"] = already_exists_count
r["time_db"] = time_db
r["time_db_u"] = time_db_u
return r
def user(self, user_id, screen_name=None, toDB=True):
start_time_api = time.time()
try:
if screen_name is not None:
user = self.api.get_user(screen_name=screen_name)
else:
user = self.api.get_user(user_id=user_id)
except weibopy.error.WeibopError as e:
if e.reason.find("User does not exists") >= 0:
if self.pgconn is None:
self.pgconn = mypass.getConn()
try:
if not self.force_screenname and not user_id is None:
sql_deleted = "UPDATE sinaweibo_users SET deleted = NOW() WHERE id = %d AND deleted IS NULL " % user_id
res = self.pgconn.query(sql_deleted)
except pg.ProgrammingError, pg.InternalError:
print self.pgconn.error
return { "msg": e.reason }
time_api = time.time() - start_time_api
time_db = 0
x = self.user_to_row(user)
if toDB:
start_time_db = time.time()
resp = self.toDB("sinaweibo_users", x, doupdate=True)
time_db = time.time() - start_time_db
if resp["already_exists"]:
already_exists = True
else:
already_exists = False
resp["time_db"] = time_db
resp["already_exists"] = already_exists
resp["newdata"] = x
#print resp
else:
print x
if toDB:
r = resp
else:
r = dict()
r["time_api"] = time_api
return r
def socialgraph(self, user_id, rel="followers", cursor=-1, count=5000, toDB=True):
already_exists_count = 0
time_db = 0
start_time_api = time.time()
api_misses = 0
while api_misses < self.max_api_misses:
try:
if rel == "friends":
relations = self.api.friends_ids(id=user_id, cursor=cursor, count=count)
else:
relations = self.api.followers_ids(id=user_id, cursor=cursor, count=count)
except httplib.IncompleteRead as h:
print h
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": h }
time.sleep(self.api_wait_secs)
except weibopy.error.WeibopError as e:
api_misses += 1
if api_misses == self.max_api_misses:
return { "msg": e.reason }
if e.reason.find("requests out of rate limit") >= 0:
self.waitRateLimit()
else:
time.sleep(self.api_wait_secs)
continue
except socket.error as e:
print e
api_misses += 1
if api_misses >= self.max_api_misses:
return { "msg": e.message }
time.sleep(self.api_wait_secs)
fids = self.getAtt(relations, "ids")
time_api = time.time() - start_time_api
r = dict()
for fid in fids:
x = { "source_id": user_id, "target_id": fid, "retrieved": "NOW()" }
start_time_db = time.time()
resp = self.toDB("sinaweibo_" + rel, x, doupdate=False)
time_db += time.time() - start_time_db
if resp["already_exists"]:
already_exists_count += 1
r = resp
r["ids"] = fids
r["time_api"] = time_api
r["count"] = len(fids)
if toDB:
r["time_db"] = time_db
r["already_exists_count"] = already_exists_count
return r
def waitRateLimit(self):
rls = self.api.rate_limit_status()
ratelimstatus = { "remaining_hits": self.getAtt(rls, "remaining_hits"), "hourly_limit": self.getAtt(rls, "hourly_limit"), "reset_time_in_seconds": self.getAtt(rls, "reset_time_in_seconds"), "reset_time": self.getAtt(rls, "reset_time") }
reset_time = int(self.getAtt(rls, "reset_time_in_seconds")) + self.api_wait_secs
if self.verbose:
print "Reset time in %(secs)d seconds (now: %(now)s) " % { "secs": reset_time, "now": datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d %H:%M:%S")}
if reset_time > 2700 and reset_time < 3600:
time.sleep(120)
elif reset_time > 120:
time.sleep(reset_time + 30)
else:
time.sleep(self.api_wait_secs * 5)
def saveRangePartitionByIdDate(self, id, date):
isocal = date.isocalendar()
f = open(self.rp_dir + "/ids/" + str(id), "w")
f.write(str(isocal[0]) + "," + str(isocal[1]))
f.close()
def getRangePartitionByIds(self, ids=list()):
# Prepare the minmax file for Python
f_minmax = open(self.rp_dir + "/rp-minmaxids-sw.csv", "r")
cr = csv.DictReader(f_minmax)
minmaxids = dict()
for csvrow in cr:
if not csvrow["year"] in minmaxids: # put year
minmaxids[csvrow["year"]] = dict()
if not csvrow["week"] in minmaxids[csvrow["year"]]: # put week
minmaxids[csvrow["year"]][csvrow["week"]] = dict()
if len(csvrow["min"]) > 0 and len(csvrow["max"]) > 0:
minmaxids[csvrow["year"]][csvrow["week"]]["min"] = long(csvrow["min"])
minmaxids[csvrow["year"]][csvrow["week"]]["max"] = long(csvrow["max"])
years_sorted = sorted(minmaxids.keys())
yearsweeks_sorted = dict()
for yeariso in years_sorted: # get a dict per year of sorted weeks
yearsweeks_sorted[yeariso] = sorted(minmaxids[yeariso].keys())
if self.verbose:
print yearsweeks_sorted
# Mark range partitions to check and keep IDs of weibos
rp_tocheck = list()
for thisid in ids:
thisid = long(thisid)
thismin = None
thismax = None
donethisid = False
last_within_minmax = False
# we go in the min-max
for year in years_sorted:
if donethisid:
break
for week in yearsweeks_sorted[year]:
minmax = minmaxids[year][week] # the minmax we are checking currently
lastmin = thismin # not used
lastmax = thismax # not used
if "min" not in minmax or "max" not in minmax: # don't check empty partitions
continue
thismin = minmax["min"]
thismax = minmax["max"]
if thismin is None or thismax is None: # don't check empty partitions
continue
if thisid >= thismin and thisid <= thismax: # within this partition
yw_str = str(year) + "," + str(week)
if yw_str not in rp_tocheck:
rp_tocheck.append(yw_str)
if last_within_minmax:
donethisid = True
break
else:
last_within_minmax = True
continue
#return { "rp": rp_tocheck, "ids": ids_tocheck, "ids_str": ids_str_tocheck }
return rp_tocheck
def toDB(self, tablename, data, doupdate=False, updatefirst=False):
if self.pgconn is None:
self.pgconn = mypass.getConn()
resp = { "success": False, "already_exists": False }
if updatefirst:
if doupdate:
try:
r = self.pgconn.update(tablename, data)
resp["success"] = True
except pg.DatabaseError:#, pg.ProgrammingError, pg.InternalError:
if self.pgconn.error.find('No such record in') > 0:
try:
r = self.pgconn.insert(tablename, data)
resp["success"] = True
except:
if self.pgconn.error.find('duplicate key value violates unique constraint') > 0:
resp["already_exists"] = True
else:
try:
#print data
r = self.pgconn.insert(tablename, data)
resp["success"] = True
except:
if self.pgconn.error.find('duplicate key value violates unique constraint') > 0:
resp["already_exists"] = True
else:
try:
#print data
self.pgconn.insert(tablename, data)
resp["success"] = True
except pg.ProgrammingError, pg.InternalError:
if self.pgconn.error.find('duplicate key value violates unique constraint') > 0:
resp["already_exists"] = True
try:
if doupdate:
self.pgconn.update(tablename, data)
resp["success"] = True
except:
resp["reason"] = self.pgconn.error
pass
#pgconn.close()
return resp
def getRangePartitionByDate(self, dt_created_at):
isocal = dt_created_at.isocalendar()
return "rp_sinaweibo_y" + str(isocal[0]) + "w" + str(isocal[1])
def getRangePartitionSQL(self, date_start, date_end=datetime.datetime.now(), select_list="*"):
isostart = date_start.isocalendar()
yearisostart = isostart[0]
weekisostart = isostart[1]
isoend = date_end.isocalendar()
yearisoend = isoend[0]
weekisoend = isoend[1]
sw_tables_arr = list()
for x in range(yearisostart,yearisoend+1):
if yearisostart == yearisoend:
weekisorange = range(weekisostart, weekisoend+1)
elif yearisostart == x:
weekisorange = range(weekisostart, 54)
elif yearisoend == x:
weekisorange = range(1, weekisoend)
for y in weekisorange:
sw_tables_arr.append("SELECT %(select_list)s FROM rp_sinaweibo_y%(year)dw%(week)d y%(year)dw%(week)d" % { "select_list": select_list, "year": x, "week": y })
sw_tables = " UNION ".join(sw_tables_arr)
return sw_tables
# Sends from command-line to the appropriate function
def dispatch(self, opt, id, output_counts=False):
if opt == 1: # user timeline
out = self.user_timeline(id)
if "count" in out and out["count"] == 0: # see if the user was just deleted
out_user = self.user(id)
elif opt == 2: # user
if self.force_screenname:
out = self.user(None, id)
else:
out = self.user(id)
elif opt == 3: # friends
out = self.socialgraph(id, "friends")
if "count" in out and out["count"] == 5000:
out = self.socialgraph(id, "friends", 4999)
elif opt == 4: # followers
out = self.socialgraph(id, "followers")
if "count" in out and out["count"] == 5000:
out = self.socialgraph(id, "followers", 4999)
elif opt == 7: # reposts
blanks_count = 0
gotall_count = 0
for i in range(self.max_reposts_pages):
items_count = 0
misses_count = 0
trial = 0
while items_count == 0 and misses_count <= self.max_reposts_tries:
time.sleep(5)
out = self.reposts(id, 200, i+1)
if not "count" in out:
misses_count += 1
rls = self.api.rate_limit_status()
ratelimstatus = { "remaining_hits": self.getAtt(rls, "remaining_hits"), "hourly_limit": self.getAtt(rls, "hourly_limit"), "reset_time_in_seconds": self.getAtt(rls, "reset_time_in_seconds"), "reset_time": self.getAtt(rls, "reset_time") }
if self.verbose:
print ratelimstatus
continue
elif out["count"] == 0:
out["msg"] = "Too many blanks: probably reached the end"
blanks_count += 1
gotall_count += 1
break
else:
blanks_count = 0
items_count = out["count"]
if self.verbose:
out["timestamp"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print out
if misses_count >= self.max_reposts_tries:
print out["msg"]
time.sleep(60)
break
elif blanks_count >= self.max_reposts_blanks:
print out["msg"]
break
if items_count == out["already_exists_count"]: # already got everything
gotall_count += 1
#continue # still continue
if not self.getall and gotall_count >= self.max_gotall_count:
out["msg"] = "Already full " + str(self.max_gotall_count) + " times: we're breaking here"
break
continue
if output_counts:
if self.pgconn is None:
self.pgconn = mypass.getConn()
rps = self.getRangePartitionByIds([id])
rps_count = 0
for x in rps:
yw = x.split(",")
year = int(yw[0])
week = int(yw[1])
sql_count = "SELECT COUNT(*) FROM rp_sinaweibo_y%(year)dw%(week)d WHERE retweeted_status = %(id)d " % { "year": year, "week": week, "id": id }
res_count = self.pgconn.query(sql_count).getresult()
rps_count += int(res_count[0][0])
fo = open(self.reposts_dir + "/counts/" + str(id), "w")
fo.write(str(rps_count))
fo.close()
elif opt == 8: # comments
blanks_count = 0
gotall_count = 0
for i in range(self.max_comments_pages):
items_count = 0
misses_count = 0
trial = 0
while items_count == 0 and misses_count <= self.max_comments_tries:
time.sleep(5)
out = self.comments(id, 200, i+1)
if not "count" in out:
misses_count += 1
rls = self.api.rate_limit_status()
ratelimstatus = { "remaining_hits": self.getAtt(rls, "remaining_hits"), "hourly_limit": self.getAtt(rls, "hourly_limit"), "reset_time_in_seconds": self.getAtt(rls, "reset_time_in_seconds"), "reset_time": self.getAtt(rls, "reset_time") }
if self.verbose:
print ratelimstatus
continue
elif out["count"] == 0:
out["msg"] = "Too many blanks: probably reached the end"
blanks_count += 1
gotall_count += 1
break
else:
blanks_count = 0
items_count = out["count"]
if self.verbose:
out["timestamp"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print out
if misses_count >= self.max_comments_tries:
print out["msg"]
time.sleep(60)
break
elif blanks_count >= self.max_comments_blanks:
print out["msg"]
break
if items_count == out["already_exists_count"]: # already got everything
gotall_count += 1
if not self.getall and gotall_count >= self.max_gotall_count:
break
continue #break
if output_counts:
if self.pgconn is None:
self.pgconn = mypass.getConn()
sql_count = "SELECT COUNT(*) FROM sinaweibo_comments WHERE status_id = %d " % id
res_count = self.pgconn.query(sql_count).getresult()
fo = open(self.comments_dir + "/counts/" + str(id), "w")
fo.write(str(res_count[0][0]))
fo.close()
elif opt == 9: # single status
out = self.get_status(id)
else:
out = None
return out
if __name__ == "__main__":
sw = SinaWeiboOauth()
output_counts = False
# prepare args
if len(sys.argv) <= 2:
print sw.usage
sys.exit()
else:
try:
id = long(sys.argv[1])
except ValueError:
id = 0
fname = str(sys.argv[1])
# primary option
if len(sys.argv) > 2:
opt = sys.argv[2]
if opt == "-ut" or opt == "--user-timeline":
opt = 1 # user timeline
elif opt == "-u" or opt == "--users":
opt = 2 # users
elif opt == "-fr" or opt == "--friends":
opt = 3 # friends
elif opt == "-fl" or opt == "--followers":
opt = 4 # followers
elif opt == "-rp" or opt == "--reposts":
opt = 7 # reposts
elif opt == "-cm" or opt == "--comments":
opt = 8 # comments
elif opt == "-ss" or opt == "--single-status":
opt = 9 # single status
else:
print self.usage # single status
sys.exit()
# secondary options
for i in range(3,len(sys.argv)):
if sys.argv[i] == "-v" or sys.argv[i] == "--verbose":
sw.verbose = True
elif sys.argv[i] == "-c" or sys.argv[i] == "--counts":
output_counts = True
elif sys.argv[i] == "-a" or sys.argv[i] == "--get-all":
sw.getall = True
elif sys.argv[i] == "-fs" or sys.argv[i] == "--force-screenname":
sw.force_screenname = True
fname = str(sys.argv[1])
elif sys.argv[i] == "-co" or sys.argv[i] == "--check-only":
sw.checkonly = True
elif sys.argv[i] == "-u" or sys.argv[i] == "--update":
sw.doupdate = True
elif sys.argv[i] == "-srp" or sys.argv[i] == "--save-range-partition":
sw.saveRP = True
elif sys.argv[i] == "-i" or sys.argv[i] == "--index":
sw.index = True
# bind token and set the api
sw.setToken(sw.sinaweiboOauth["oauth_token"], sw.sinaweiboOauth["oauth_token_secret"])
# initialize the indexer, if needed
if sw.index:
lucene.initVM(lucene.CLASSPATH)
sw.indexer = sinaweibolucene.IndexSinaWeibo()
# dispatch
if sw.force_screenname:
out = sw.dispatch(opt, fname, output_counts)
out = [out]
elif id > 0:
out = sw.dispatch(opt, id, output_counts)
out["id"] = id
out = [out] # put in an array for consistency with list of ids
else:
try:
f = open(fname, "r")
except IOError:
print sw.usage
sys.exit()
out = list()
for l in f:
try:
id = long(l) # try with the whole line
except ValueError:
try:
id = long(l.split(",")[0]) # try on the first comma-sep value
except ValueError:
continue
thisout = sw.dispatch(opt, id, output_counts)
thisout["id"] = id
out.append(thisout)
output = { "data": out, "opt": opt, "count": len(out), "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") }
if sw.verbose:
print output
if sw.index:
sw.indexer.writer.optimize()
sw.indexer.writer.close()
sw.indexer.ticker.tick = False
#sw.auth()
#sw.setToken("24910039392f0acf7b83b8cb91ee9191", "1363bae989c232bf5db6aefb3e6460c7")
#print sw.user_timeline(1801443400)
#print sw.comments(10741815655)
#print sw.reposts(10713727974)
#print sw.user(1801443400)
#print sw.socialgraph(1801443400)
#sw.auth()