-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.py
More file actions
1105 lines (1026 loc) · 46 KB
/
pubsub.py
File metadata and controls
1105 lines (1026 loc) · 46 KB
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#coding=utf-8
#!/usr/bin/env python
#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext.webapp import template
from django.utils import simplejson as json
import models
from models import GRUser
from models import SomeUpdate
import feedparser
import twitter_oauth_handler
import sina_oauth_handler
import bitly
import hashlib
import logging
from google.appengine.ext import ereporter
import random
import urllib
import urllib2
import base64
import time
import datetime
import re
import uuid
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from google.appengine.api import memcache
from google.appengine.ext import deferred
from google.appengine.api import urlfetch
from google.appengine.api.urlfetch_errors import *
UPDATE_CLEANUP_BATCH_SIZE = 100
UPDATE_LIVE_TIME = 97
consumer_key = '1935597329' # 设置你申请的appkey
consumer_secret = '7008f8920d5a7e92101d408ac468b0e7' # 设置你申请的appkey对于的secret
class UpdateStat(db.Model):
"""UPDATE STATISTICS.
Key name will be a hash of the feed source and item ID.
"""
counter = db.IntegerProperty(default = 0)
@staticmethod
def update_today_stat(type,counter):
"""update today's stat"""
today = str(datetime.date.today())
tdkeyname = "%s%s" % (type,today)
today_stat = UpdateStat.get_or_insert(tdkeyname)
today_stat.increase(counter)
@staticmethod
def recent_stats_graph_url():
"""using chart api to show recent 7 days stat graph"""
grs = UpdateStat.recent_stats("GoogleReader",7)
gbs = UpdateStat.recent_stats("GoogleBuzz",7)
grs_list = ','.join([str(gr.counter) for gr in grs])
gbs_list = ','.join([str(gb.counter) for gb in gbs])
return 'http://chart.apis.google.com/chart?cht=bvs&chs=400x250&chd=t:%s|%s&chco=4d89f9,c6d9fd&chbh=20&chds=0,10000&chm=D,76A4FB,1,0,3|N,FF0000,-1,-1,10|N,000000,0,,12,,c|N,000000,1,,12,,c' % (grs_list,gbs_list)
@staticmethod
def recent_stats(type,number):
"""#number past stat before today"""
return [UpdateStat.get_stat(type,i) for i in reversed(range(0,number))]
@staticmethod
def get_stat(type,offset):
"""get #offset day stat"""
return UpdateStat.get_or_insert("%s%s" % (type,str(datetime.date.today()+datetime.timedelta(days=-offset))))
def increase(self,counter):
"""Increase the counter by given counter"""
db.run_in_transaction(increment_counter, self.key(), 1)
def increment_counter(key, amount):
obj = db.get(key)
obj.counter += amount
obj.put()
class PubTwitter(webapp.RequestHandler):
"""Handles feed input and subscription"""
def get(self):
username = self.request.get("username")
message = self.request.get("message").encode('utf-8')
service = self.request.get("service")
logging.info('Task added: repub '+username+"'s tweet in "+service)
logging.info(message)
try:
if username == "yishake" or username == 'P3t3rU5':
return
gruser = is_user(username)
if not gruser:
return
if gruser.oauth:
token = get_token_by_username(gruser.username)
client = twitter_oauth_handler.OAuthClient('twitter', self)
client.token = token
client.post("/statuses/update",status = message)
else:
basestring = gruser.basestring
pub_to_twitter_by_basestring(basestring,message)
self.response.set_status(200)
self.response.out.write("OK")
except Exception,e:
if "Could not authenticate you." in str(e):
logging.info("twitter auth is invalid")
if service == 'GR2T':
gruser.topic = ''
gruser.unsubscribe = True
gruser.put()
logging.info('Stop %s % service' % (username,service))
elif service == 'GB2T':
gruser.buzztopic = ''
gruser.put()
logging.info('Stop %s % service' % (username,service))
return
self.response.set_status(200)
self.response.out.write("OK")
logging.debug(e)
post = get
def logging_now():
now = datetime.datetime.now()+datetime.timedelta(hours=8)
logging.info(now)
class Feedr(webapp.RequestHandler):
"""Handles feed input and subscription"""
def get(self):
# Just subscribe to everything.
self.response.out.write(self.request.get('hub.challenge'))
self.response.set_status(200)
logging.info("subscribed %s" % self.request.get('hub.topic'))
def post(self):
logging_now()
body = self.request.body.decode('utf-8')
logging.info('Post body is %d characters', len(body))
logging.info(self.request.body)
data = feedparser.parse(self.request.body)
for entry in data.entries:
if hasattr(entry, 'content'):
entry_id = entry.id
if entry.has_key("summary_detail"):
content = entry.summary_detail.value
else:
content = entry.content[0].value
link = entry.get('link', '')
title = entry.get('title', '')
else:
content = entry.get('description', '')
title = entry.get('title', '')
link = entry.get('link', '')
entry_id = (entry.get('id', '') or link)
logging.info(entry_id)
logging.info(title)
logging.info(link)
def buzz_date(e):
if hasattr(e,"published"):
return e.published
elif hasattr(e,"updated"):
return e.updated
else:
return
class BuzzPuSH(webapp.RequestHandler):
"""Handles feed input and subscription"""
def get(self):
# Just subscribe to everything.
self.response.out.write(self.request.get('hub.challenge'))
self.response.set_status(200)
logging.info("subscribed %s" % self.request.get('hub.topic'))
def post(self):
"""handle notification of buzz update from hub"""
body = self.request.body
logging.debug(body)
body_len = len(body)
logging.info(body_len)
if body_len < 0:
logging.debug('Added to queue for quick response.')
buzz_queue = taskqueue.Queue("buzz2twitter")
task = taskqueue.Task(url = '/buzzpush-queue', payload = urllib.quote(body))
buzz_queue.add(task)
self.response.set_status(200)
else:
data = feedparser.parse(body)
buzztopic = data.feed.links[0]["href"]
if '?' in buzztopic:
buzztopic = buzztopic[:buzztopic.find('?')]
logging.info(buzztopic)
pubsub_topic = models.PubSubTopic.get_or_insert(key_name = buzztopic)
"""
if not memcache.get(buzztopic):
memcache.add(buzztopic,"1",3)
else:
logging.info("hub notify me two duplicates")
return
"""
grusers = get_user_by_buzztopic(buzztopic) #need to change the datastore structure
if not grusers:
return
logging.info(grusers[0].username)
token = ''
username = ''
counter = 0
logging.info('Found %d entries', len(data.entries))
logging.debug(body)
logging.debug(data)
for entry in data.entries:
if hasattr(entry, 'content'):
entry_id = entry.id
if entry.has_key("summary_detail"):
content = entry.summary_detail.value
else:
content = entry.content[0].value
link = entry.get('link', '')
title = entry.get('title', '')
else:
content = entry.get('description', '')
title = entry.get('title', '')
link = entry.get('link', '')
entry_id = (entry.get('id', '') or link)
logging.info('Found entry with title = "%s", id = "%s"',title, entry_id)
if hasattr(entry, 'source'):
source = entry.source.title
logging.debug(source)
else:
source = ''
if hasattr(entry, 'point'):
logging.debug('point: %s' % entry.point)
#if "from Buzz" in title or "from Mobile" in title or "Google Maps for Mobile" in title or "via buzz@gmail" in title:
if source == 'Buzz' or source == 'Mobile' or source == 'Posted via buzz@gmail' or "from Buzz" in title or "from Mobile" in title or "Google Maps for Mobile" in title or "via buzz@gmail" in title:
logging.info(entry)
#content = content+">"
logging.debug(link)
logging.debug(buzztopic)
my_key_name = 'key_' + hashlib.sha1(buzz_date(entry) + '\n' + buzztopic).hexdigest()
logging.debug(my_key_name)
if models.UpdateItem.get_by_key_name(key_names = my_key_name, parent = pubsub_topic):
logging.debug("Dupilcates!!!")
continue
else:
counter += 1
#Use hashed key_name to save query time
#someupdate = SomeUpdate(key_name=my_key_name)
#someupdate.put()
db.put(models.UpdateItem(parent = pubsub_topic, key_name=my_key_name))
if counter>2:
continue
else:
content = re.sub("<[^>]*>","",content)
logging.info(buzz_date(entry))
for gruser in grusers:
logging.info(gruser.username)
try:
link = shorten(link,gruser.bitlylogin,gruser.bitlykey)
except:
link = link
logging.debug("debug link")
logging.debug(link)
if gruser.buzzlink == 'enable':
message = content+" "+link
if len(message)>139:
if "http://" in content:
message = content[:content.find("http://")]+"... "+link
else:
message = content[:100]+"... "+link
else:
message = content
if len(message)>139:
if "http://" in content:
message = content[:content.find("http://")]+"... "+link
else:
message = content[:100]+"... "+link
elif hasattr(entry, 'enclosures'):
logging.info("enclosures")
for enclosure in entry.enclosures:
# logging.info(enclosure.type)
if enclosure.type == "image/jpeg" and enclosure.href.find("http://picasaweb.google.com/")==0:
message = content[:100]+" [pic] "+link
break
elif enclosure.type == "text/html" or enclosure.type == "application/x-shockwave-flash":
link = shorten(enclosure.href,gruser.bitlylogin,gruser.bitlykey)
message = content[:100]+"... "+link
break
message = message.encode("utf-8")
if not gruser.oauth:
basestring = gruser.basestring
try:
logging.info(message)
pub_to_twitter_by_basestring(basestring,message)
except:
return
else:
client = twitter_oauth_handler.OAuthClient('twitter', self)
client.token = get_token_by_username(gruser.username)
try:
logging.info(message)
client.post("/statuses/update",status = message)
except Exception,e:
logging.debug(e)
self.response.set_status(200)
UpdateStat.update_today_stat("GoogleBuzz",counter)
logging.info("Published %d item(s) to Twitter" % counter)
def buzz_date(e):
if hasattr(e,"published"):
return e.published
elif hasattr(e,"updated"):
return e.updated
else:
return
class InputHandler(webapp.RequestHandler):
"""Handles feed input and subscription"""
def get(self):
# Just subscribe to everything.
self.response.out.write(self.request.get('hub.challenge'))
self.response.set_status(200)
logging.info("subscribed %s" % self.request.get('hub.topic'))
def post(self):
"""handle the update from hub and then pub it to twitter"""
body = self.request.body
logging.debug(body)
data = feedparser.parse(body)
logging.debug(data)
try:
topic = data.feed.links[1]["href"] #for hub is the 2nd property
except:
topic = data.feed.links[0]["href"]
pubsub_topic = models.PubSubTopic.get_or_insert(key_name = topic)
grusers = get_user_by_topic(topic) #we can use "twitter_username" as the key_name
if not grusers:
logging.debug("No user have this topic %s" % topic)
return #maybe unsubscribe if no user have subscribed the topic
format = ''
token = ''
username = ''
counter = 0
logging.info('Found %d entries', len(data.entries))
for entry in data.entries:
if hasattr(entry, 'content'):
# This is Atom.
entry_id = entry.id
if entry.has_key("summary_detail"):
content = entry.summary_detail.value
else:
content = entry.content[0].value
link = entry.get('link', '')
title = entry.get('title', '')
else:
content = entry.get('description', '')
title = entry.get('title', '')
link = entry.get('link', '')
entry_id = entry.get('id', '')
logging.info('Found entry with title = "%s", id = "%s"',title, entry_id)
#my_key_name is a unique key_name for a update
my_key_name = 'key_' + entry_id#hashlib.sha1(link + '\n' + entry_id +topic).hexdigest()
logging.debug(my_key_name)
#if not is_article_exist(my_key_name,topic,link): #is_article_exist function need to be rewrite
if not models.UpdateItem.get_by_key_name(key_names = my_key_name, parent = pubsub_topic):
#Use hashed key_name to save query time
db.put(models.UpdateItem(parent = pubsub_topic, key_name=my_key_name))
counter += 1
logging.debug(entry)
#logging.debug(content)
for gruser in grusers:
if gruser.unsubscribe:
continue
else:
logging.info(gruser.username)
is_note = False
pub = False
#remove html element in content and title
mycontent = re.sub("<[^>]*>","",content)
title = re.sub("<[^>]*>","",title)
if mycontent == title: #it's a update only contains note
message = title.encode('utf-8') #encode the message for twitter
is_note = "Note"
else:
if content.find("<blockquote")==0:
start = content.find("<br />\n")
end = content.find("</blockquote>")
content = content[start+7:end]
content = re.sub("<[^>]*>","",content)
is_note = True #it's a gr update that contains note
else:
content = ''
link = shorten(link,gruser.bitlylogin,gruser.bitlykey)
logging.debug("debug link")
logging.debug(link)
format = gruser.format #get user's format
message = format_tweet(format,title,link,content,is_note)
#message = message[:140]
synctype = gruser.synctype
#check if pub to twitter based on user's config
if synctype == "all" or not synctype:
logging.info("Sync All")
pub = True
elif synctype == "note" and is_note == "Note":
logging.info("only Sync note")
pub = True
elif synctype == "comment" and is_note:
logging.info("Sync gr with note")
pub = True
else:
logging.info("Do not need to pub to twitter")
if pub:
if counter > 2:
logging.info("超出2篇")
gruser.fetch_old()
break
else:
#if gruser.key().name():
# if re.search("^Key_Weibo", gruser.key().name()):
# sina_oauth_handler.update_weibo(gruser, message)
if not gruser.oauth:
basestring = gruser.basestring
try:
logging.info(message)
pub_to_twitter_by_basestring(basestring,message)
except Exception,e:
logging.debug("pub to twitter error:%s" % str(e))
else:
client = twitter_oauth_handler.OAuthClient('twitter', self)
client.token = get_token_by_username(gruser.username)
try:
logging.info(message)
client.post("/statuses/update",status = message)
except Exception,e:
logging.debug("pub to twitter error:%s" % str(e))
logging.info("Published %d item(s) to Twitter" % counter)
UpdateStat.update_today_stat("GoogleReader",counter)
self.response.set_status(200)
self.response.out.write("Aight. Saved.")
def CutString(gs, length):
gs = gs.encode("gb2312")
us = unicode(gs, 'gbk')
n = int(length)*2
s_len = len(gs)
if s_len < n:
return gs
t = gs[:n]
while True:
try:
unicode(t, 'gbk')
break
except:
n -= 1
t = gs[:n]
return t.encode('utf-8')
def google_shorten(longUrl):
try:
request = urllib2.Request("https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyC4RM0ZE6kAaUPLd0WZSYv-pH01PrnwWVo", data='{"longUrl": "%s"}' % longUrl, headers = {'Content-Type':'application/json'})
response = urllib2.urlopen(request)
content = response.read()
return json.loads(content)['id']
except:
return ''
def shorten(link,login,apikey):
try:
if login and apikey:
client=bitly.Api(login=login,apikey=apikey)
short_link = client.shorten(link)
else:
short_link = google_shorten(link)
if not short_link:
short_link = urlfetch.fetch("http://is.gd/api.php?longurl=" + link).content
return short_link
except:
return link
def format_tweet(format,title,link,comment,is_note):
if not format:
return title.encode("utf-8")+" "+link.encode("utf-8")+" "+comment.encode("utf-8")
elif format.find("url")==-1:
return title.encode("utf-8")+" "+link.encode("utf-8")+" "+comment.encode("utf-8")
else:
if format.find("{comment}")==-1:
if not format.find("{title}")==-1:
format = format.replace("{title}",title)
format = format.replace("{url}",link)
format = format.encode("utf-8")
else:
if comment == "":
if not format.find("{title}")==-1:
a = format.find("{title}")
b = format.find("{url}")
if a>b:
format = format[:a+7]
else:
format = format[:b+5]
format = format.replace("{title}",title)
format = format.replace("{url}",link)
if is_note:
format = format.replace("{comment}",comment)
else:
format = format.replace("{comment}","")
format = format.encode("utf-8")
return format
class Test(webapp.RequestHandler):
"""Gets the items."""
def get(self):
self.response.out.write(google_shorten("http://www.kangye.org"))
class TestOauthHandler(webapp.RequestHandler):
"""Demo Twitter App."""
def get(self):
HEADER = """
<html><head><title>Twitter OAuth Demo</title>
</head><body>
<h1>Twitter OAuth Demo App</h1>
"""
FOOTER = "</body></html>"
client = twitter_oauth_handler.OAuthClient('twitter', self)
#gdata = OAuthClient('google', self, scope='http://www.google.com/calendar/feeds')
write = self.response.out.write; write(HEADER)
if not client.get_cookie():
write('<a href="/oauth/twitter/login">Login via Twitter</a>')
write(FOOTER)
return
client.post("/statuses/update",status = "test from oauth")
write('<a href="/oauth/twitter/logout">Logout from Twitter</a><br /><br />')
info = client.get('/account/verify_credentials')
write("<strong>Screen Name:</strong> %s<br />" % info['screen_name'])
write("<strong>Location:</strong> %s<br />" % info['location'])
rate_info = client.get('/account/rate_limit_status')
write("<strong>API Rate Limit Status:</strong> %r" % rate_info)
write(FOOTER)
class NewUser(webapp.RequestHandler):
"""Demo Twitter App."""
def get(self):
client = twitter_oauth_handler.OAuthClient('twitter', self)
if not client.get_cookie():
self.redirect('/')
return
info = client.get('/account/verify_credentials')
if not info:
self.response.out.write('Error occured when conmunicating with Twitter. Please <a href="/">try again</a>.')
else:
username = info['screen_name']
gruser = is_user(username)
language = get_language(self)
if not gruser:
gruser = GRUser(key_name='Key_Twitter_'+username,username = username, oauth = True)
gruser.put()
logging.debug("New Twitter-Login user.")
logging.info(username)
else:
gruser.oauth = True
gruser.put()
if language == "cn":
self.redirect('/cnuser')
else:
self.redirect('/enuser')
class FetchOld(webapp.RequestHandler):
"""fetch old items
todo: need to do some change to the key_name"""
def get(self):
#logging.info("start fetching the rss of new user")
update_list = []
topic = self.request.get("topic")
logging.info(topic)
if topic.startswith("http"):
gaehub = models.Hub.get_by_key_name("http://pubsubhubbub.appspot.com/subscribe")
pubsub_topic = models.PubSubTopic.get_or_insert(key_name=topic, hub = gaehub, verify_token = str(uuid.uuid4()))
entries = feedparser.parse(topic).entries
for entry in entries:
if hasattr(entry, 'content'):
# This is Atom.
entry_id = entry.id
else:
entry_id = entry.get('id', '')
if "buzz" in topic:
my_key_name = 'key_' + hashlib.sha1(buzz_date(entry) + '\n' + topic).hexdigest()
else:
my_key_name = 'key_' + entry_id
update_list.append(models.UpdateItem(parent = pubsub_topic, key_name=my_key_name))
db.put(update_list)
self.response.set_status(200)
self.response.out.write("Aight. Saved.")
post = get
class SaveOld(webapp.RequestHandler):
"""Handles feed input and subscription"""
def get(self):
try:
topic = self.request.get("topic")
link = self.request.get("link")
old_update = SomeUpdate(topic=topic,link=link)
old_update.put()
except:
self.response.out.write("OK")
post = get
class SubCN(webapp.RequestHandler):
"""fetch old items"""
def get(self):
topics = Topic.all().fetch(500)
for topic in topics:
pub_queue = taskqueue.Queue("my-queue-1")
task = taskqueue.Task(url='/subtopic',params=dict(topicurl=topic.url))
pub_queue.add(task)
self.response.set_status(200)
self.response.out.write("Aight. Saved.")
post = get
class SyncCNOauth(webapp.RequestHandler):
"""Debug handler for simulating events."""
def get(self):
client = twitter_oauth_handler.OAuthClient('twitter', self)
if not client.get_cookie():
self.response.out.write('请<a href="/cn">登录</a>')
return
try:
info = client.get('/account/verify_credentials')
except:
self.redirect('/oauth/twitter/login')
return
gruser = is_user(info['screen_name'])
template_value = {}
if gruser:
mygruser = {}
mygruser["username"]=gruser.username
mygruser["format"]=gruser.format
mygruser["bitlykey"]=gruser.bitlykey
mygruser["bitlylogin"]=gruser.bitlylogin
mygruser["synctype"]={}
mygruser["readerid"]=""
if gruser.topic:
mygruser["readerid"] = gruser.topic.replace("http://www.google.com/reader/public/atom/user/","")
mygruser["readerid"] = mygruser["readerid"].replace("/state/com.google/broadcast","")
if not gruser.synctype:
mygruser["synctype"] = {"all":"checked","comment":"","Note":""}
elif gruser.synctype == "all":
mygruser["synctype"] = {"all":"checked","comment":"","Note":""}
elif gruser.synctype == "comment":
mygruser["synctype"] = {"all":"","comment":"checked","Note":""}
else:
mygruser["synctype"] = {"all":"","comment":"","Note":"checked"}
template_value = {"gruser":mygruser}
self.response.out.write(template.render('syncoauth.html', template_value))
def post(self):
client = twitter_oauth_handler.OAuthClient('twitter', self)
if client.get_cookie():
client = twitter_oauth_handler.OAuthClient('twitter', self)
info = client.get('/account/verify_credentials')
username = info['screen_name']
logging.info(username)
#self.response.out.write("登录成功")
grid = self.request.get("grid")
if re.search(r"\d*",grid).group():
topic = "http://www.google.com/reader/public/atom/user/%s/state/com.google/broadcast" % grid
issync = self.request.get("mode")
gruser = is_user(username)
if issync == "subscribe":
format = self.request.get("format")
synctype = self.request.get("synctype")
bitlylogin = self.request.get("bitlylogin")
bitlykey = self.request.get("bitlykey")
logging.info(synctype)
if gruser:
gruser.topic = topic
gruser.format = format
gruser.synctype = synctype
gruser.bitlykey = bitlykey
gruser.bitlylogin = bitlylogin
gruser.unsubscribe = False
gruser.put()
else:
logging.info("新用户")
#basestring = base64.encodestring('%s:%s' % (username, password))[:-1]
gruser = GRUser(key_name='Key_Twitter_'+username,username = username,topic = topic, format = format)
gruser.put()
elif gruser:
logging.debug("用户退订")
logging.debug(gruser.username)
gruser.unsubscribe = True
gruser.put()
return
#gruser.delete()
payload = {"hub.mode":issync,"hub.verify":"sync","hub.callback":"http://reader2twitter.appspot.com/subscriber","hub.topic":topic}
payload= urllib.urlencode(payload)
url = "http://pubsubhubbub.appspot.com/subscribe"
try:
result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST)
logging.info(result.status_code)
if result.status_code == 204:
logging.info("%s success" % issync)
if issync == "unsubscribe":
self.response.out.write('You have stopped sync. Any suggestion to <a href="http://twitter.com/gr2t">gr2t</a>?')
else:
self.response.out.write("You sync is established. Wish you can share this tool in greader:) <a href=\"javascript:var%20b=document.body;var%20GR________bookmarklet_domain='http://www.google.com';if(b&&!document.xmlVersion){void(z=document.createElement('script'));void(z.src='http://www.google.com/reader/ui/link-bookmarklet.js');void(b.appendChild(z));}else{}\">click to share</a>")
else:
logging.info("向hub订阅Publisher失败")
self.response.out.write('Please <a href="/buzz">retry</a>')
except:
logging.info("和hub连接失败")
self.response.out.write('Please <a href="/buzz">retry</a>')
else:
logging.info("google reader id格式错误")
self.response.out.write('Please use Google Profile <b>number</b> ID,<a href="/buzz">retry</a>')
else:
logging.info("没有oauth认证")
self.response.out.write('Please <a href="/buzz">sign in</a>')
class SyncENOauth(webapp.RequestHandler):
"""Debug handler for simulating events."""
def get(self):
client = twitter_oauth_handler.OAuthClient('twitter', self)
if not client.get_cookie():
self.response.out.write('Please <a href="/en">sign in</a>')
return
try:
info = client.get('/account/verify_credentials')
except:
self.redirect('/oauth/twitter/login')
return
gruser = is_user(info['screen_name'])
template_value = {}
if gruser:
mygruser = {}
mygruser["username"]=gruser.username
mygruser["format"]=gruser.format
mygruser["bitlykey"]=gruser.bitlykey
mygruser["bitlylogin"]=gruser.bitlylogin
mygruser["synctype"]={}
if gruser.topic:
mygruser["readerid"] = gruser.topic.replace("http://www.google.com/reader/public/atom/user/","")
mygruser["readerid"] = mygruser["readerid"].replace("/state/com.google/broadcast","")
if not gruser.synctype:
mygruser["synctype"] = {"all":"checked","comment":"","Note":""}
elif gruser.synctype == "all":
mygruser["synctype"] = {"all":"checked","comment":"","Note":""}
elif gruser.synctype == "comment":
mygruser["synctype"] = {"all":"","comment":"checked","Note":""}
else:
mygruser["synctype"] = {"all":"","comment":"","Note":"checked"}
template_value = {"gruser":mygruser}
else:
self.redirect("/")
self.response.out.write(template.render('syncoauthen.html', template_value))
def post(self):
logging_now()
client = twitter_oauth_handler.OAuthClient('twitter', self)
if client.get_cookie():
client = twitter_oauth_handler.OAuthClient('twitter', self)
try:
info = client.get('/account/verify_credentials')
except DownloadError, e:
self.response.out.write("Twitter connection error. Please retry:)")
if not info:
self.response.out.write('Twitter connection error. Please <a href="/">retry</a>:)')
username = info['screen_name']
logging.info(username)
#self.response.out.write("登录成功")
grid = self.request.get("grid")
if re.search(r"\d*",grid).group():
topic = "http://www.google.com/reader/public/atom/user/%s/state/com.google/broadcast" % grid
logging.info(grid)
issync = self.request.get("mode")
gruser = is_user(username)
if issync == "subscribe":
format = self.request.get("format")
synctype = self.request.get("synctype")
bitlylogin = self.request.get("bitlylogin")
bitlykey = self.request.get("bitlykey")
logging.info(synctype)
if gruser:
gruser.topic = topic
gruser.format = format
gruser.synctype = synctype
gruser.bitlylogin = bitlylogin
gruser.bitlykey = bitlykey
gruser.unsubscribe = False
gruser.put()
else:
logging.info("new user")
gruser = GRUser(key_name='Key_Twitter_'+username,username = username,topic = topic, format = format)
gruser.put()
elif gruser:
logging.info(issync)
logging.debug("用户退订")
logging.debug(gruser.username)
gruser.unsubscribe = True
gruser.put()
payload = {"hub.mode":issync,"hub.verify":"sync","hub.callback":"http://reader2twitter.appspot.com/subscriber","hub.topic":topic}
payload= urllib.urlencode(payload)
url = "http://pubsubhubbub.appspot.com/subscribe"
try:
result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST)
if result.status_code == 204:
fetch_queue = taskqueue.Queue("fetch-old")
task = taskqueue.Task(url='/fetchold',params=dict(topic=topic))
fetch_queue.add(task)
logging.info("%s success" % issync)
if issync == "unsubscribe":
self.response.out.write('You have stopped sync. Any suggestion to <a href="http://twitter.com/gr2t">gr2t</a>?')
else:
self.response.out.write("You sync is established. Wish you can share this tool in greader:) <a href=\"javascript:var%20b=document.body;var%20GR________bookmarklet_domain='http://www.google.com';if(b&&!document.xmlVersion){void(z=document.createElement('script'));void(z.src='http://www.google.com/reader/ui/link-bookmarklet.js');void(b.appendChild(z));}else{}\">click to share</a>")
else:
logging.info("向hub订阅Publisher失败")
self.response.out.write('Please <a href="/enuser">retry</a>')
except:
logging.info("和hub连接失败")
self.response.out.write('Please <a href="/enuser">retry</a>')
else:
logging.info("google reader id格式错误")
self.response.out.write('Please use Google Reader <b>number</b> ID,<a href="/enuser">retry</a>')
else:
logging.info("没有oauth认证")
self.response.out.write('Please <a href="/en">sign in</a>')
class BuzzENOauth(webapp.RequestHandler):
"""Debug handler for simulating events."""
def get(self):
try:
client = twitter_oauth_handler.OAuthClient('twitter', self)
if not client.get_cookie():
self.response.out.write(template.render('buzzen.html',''))
return
try:
info = client.get('/account/verify_credentials')
except:
self.redirect('/oauth/twitter/login')
return
except:
self.response.out.write('Reader2Twitter has a problem with Twitter API. <a href="/buzz">Retry</a>')
return
gruser = is_user(info['screen_name'])
template_value = {}
if gruser:
mygruser = {}
mygruser["username"]=gruser.username
mygruser["bitlykey"]=gruser.bitlykey
mygruser["bitlylogin"]=gruser.bitlylogin
if gruser.buzztopic:
#mygruser["profileid"] = gruser.buzztopic.replace("http://buzz.googleapis.com/feeds/","")
mygruser["profileid"] = gruser.buzztopic.replace("https://www.googleapis.com/buzz/v1/activities/", '')
mygruser["profileid"] = mygruser["profileid"].replace("http://www.googleapis.com/buzz/v1/activities/", '')
#mygruser["profileid"] = mygruser["profileid"].replace("/public/posted","")
mygruser["profileid"] = mygruser["profileid"].replace("/@public", '')
template_value = {"gruser":mygruser}
else:
self.redirect("/oauth/twitter/login")
self.response.out.write(template.render('buzzoauthen.html', template_value))
def post(self):
logging_now()
client = twitter_oauth_handler.OAuthClient('twitter', self)
if client.get_cookie():
client = twitter_oauth_handler.OAuthClient('twitter', self)
info = client.get('/account/verify_credentials')
username = info['screen_name']
logging.info(username)
gpid = self.request.get("gpid")
if re.search(r"\d*",gpid).group():
#buzztopic = "http://buzz.googleapis.com/feeds/%s/public/posted" % gpid
buzztopic = "https://www.googleapis.com/buzz/v1/activities/%s/@public" % gpid
#topic = "http://www.google.com/reader/public/atom/user/%s/state/com.google/broadcast" % grid
logging.info(gpid)
issync = self.request.get("mode")
buzzlink = self.request.get("buzzlink")
gruser = is_user(username)
if issync == "subscribe":
if gruser:
gruser.buzztopic = buzztopic
gruser.buzzlink = buzzlink
gruser.put()
else:
logging.info("new user")
gruser = GRUser(key_name='Key_Twitter_'+username,username = username,buzztopic = buzztopic)
gruser.buzzlink = buzzlink
gruser.put()
elif gruser:
logging.info(issync)
logging.debug("用户退订")
logging.debug(gruser.username)
#gruser.put()
#return
payload = {"hub.mode":issync,"hub.verify":"sync","hub.callback":"http://reader2twitter.appspot.com/buzzpush","hub.topic":buzztopic}
payload= urllib.urlencode(payload)
url = "http://pubsubhubbub.appspot.com/subscribe"
try:
result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST)
logging.info(result.status_code)
if result.status_code == 204 or result2.status_code == 204:
logging.info("%s success" % issync)
if issync == "unsubscribe":
self.response.out.write('You have stopped sync. Any suggestion to <a href="http://twitter.com/gr2t">gr2t</a>?')
else:
self.response.out.write("You sync is established. Wish you can share this tool in greader:) <a href=\"javascript:var%20b=document.body;var%20GR________bookmarklet_domain='http://www.google.com';if(b&&!document.xmlVersion){void(z=document.createElement('script'));void(z.src='http://www.google.com/reader/ui/link-bookmarklet.js');void(b.appendChild(z));}else{}\">click to share</a>")
else:
logging.info("向hub订阅Publisher失败")
self.response.out.write('Please <a href="/buzz">retry</a>')
except:
logging.info("和hub连接失败")
self.response.out.write('Please <a href="/buzz">retry</a>')
else:
logging.info("google reader id格式错误")
self.response.out.write('Please use Google Profile <b>number</b> ID,<a href="/buzz">retry</a>')
else:
logging.info("没有oauth认证")
self.response.out.write('Please <a href="/buzz">sign in</a>')
class SyncEN(webapp.RequestHandler):
"""Debug handler for simulating events."""
def get(self):
logging.info('test')
client = twitter_oauth_handler.OAuthClient('twitter', self)
if client.get_cookie():
self.redirect("/enuser")
return
set_language(self,"en")
self.response.out.write(template.render('syncen.html', {}))
def pub_to_twitter_by_basestring(basestring,message):
payload= {'status' : message}
payload= urllib.urlencode(payload)
headers = {'Authorization': "Basic %s" % basestring}
url = "http://twitter.com/statuses/update.xml"
result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers)
def login_to_twitter(login,password):
login = login
password = password
base64string = base64.encodestring('%s:%s' % (login, password))[:-1]
headers = {'Authorization': "Basic %s" % base64string}
url = "http://twitter.com/account/verify_credentials.xml"
result = urlfetch.fetch(url, method=urlfetch.GET, headers=headers)
if not result.content.find("Could not authenticate you.")==-1:
return False
else:
logging.info("twitter user %s is using r2t" % login)
return True
def is_article_exist(my_key_name,topic,link):
if SomeUpdate.get_by_key_name(my_key_name):
logging.debug("article exists detected by key_name")
return True
else:
try:
query = SomeUpdate.all(keys_only=True).filter("link = ",link).filter("topic = ",topic)
if query.count()==0:
return False
else:
transfer_item_to_key(query,my_key_name)
return True
except Exception,e: