What's new? | Help | Directory | Sign in
Google
gpowered
source code and examples from gpowered.net
  
    
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
############
from django.core.management import setup_environ
import sys
sys.path.append('/home/broderboy/workspace/')
sys.path.append('/home/gpowered/gpowered-read-only/')
from gpowered import settings

setup_environ(settings)

############

from gpowered.core.models import ServiceLogin, Service, RsaKey
import gpowered.rsa

import sys, xmpp, os, twitter, urllib2, time, simplejson
from time import gmtime, strftime

class Twitter2gChat:

def __init__(self):
self.twitter_service = Service.objects.get(name='Twitter')
twitter_service_login = self.twitter_service.servicelogin_set.all()[:1][0]

self.ts_login = twitter_service_login.username
self.ts_pass = twitter_service_login.password

self.twitter_status = None
self.updated = None
self.catches = 0

#keep looping and wait for xmpp response
def GoOn(self,conn):
while self.StepOn(conn):
pass

#keep listening for responses
def StepOn(self,conn):
print 'StepOn'
if self.updated:
return 0
try:
conn.Process(1)
except KeyboardInterrupt:
return 0
return 1

#handle responses
def iqHandler(self, conn,iq_node):
print 'in iqHandler'
print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
self.catches = self.catches + 1

#we have looped enough, die
if self.catches == 4:
print 'i think we did it'
#sys.exit(0)
self.updated = True
return

#print response, don't need to send anything back
if self.updated == True:
try:
print iq_node
except:
pass

#havn't updated yet, sent status update
else:
#we can build of response
node = iq_node.getChildren()[0]

#remove what we don't ned
node.delAttr('status-list-max')
node.delAttr('status-max')
node.delAttr('status-list-contents-max')
iq_node.delAttr('from')
iq_node.delAttr('type')
iq_node.delAttr('to')

#update the current status
curr_status = node.getChildren()[0]

#no need to update
if curr_status.getData() == self.twitter_status:
print 'status is already tweet'
#sys.exit(0)
self.updated = True

curr_status.setData(self.twitter_status)

#set response
iq_node.setType('set')

print 'sending'
try:
print iq_node
except:
pass
self.updated = True
conn.send(iq_node)
print 'end of iqHandler\n\n'

#start talking to the server and update status
def updateGtalkStatus(self, google_username, google_pass):
if '@' not in google_username:
google_username = '%s@gmail.com' % google_username
print google_username
#connect
jid=xmpp.protocol.JID(google_username)
cl=xmpp.Client(jid.getDomain(),debug=[])
if not cl.connect(('talk.google.com',5222)):
print 'Can not connect to server.'
#sys.exit(1)
self.updated = True
return
if not cl.auth(jid.getNode(),google_pass):
print 'Can not auth with server %s ' % google_username
self.updated = True
return

#build query to get current status
iq = xmpp.Iq()
iq.setType('get')
iq.setTo(google_username)

node = xmpp.Node()
node.setName('query')
node.setAttr('xmlns', 'google:shared-status')

iq.addChild(node=node)
print iq

#register with server and send subscribe to status updates
cl.RegisterHandler('iq',self.iqHandler)
cl.send(iq)

self.GoOn(cl)
cl.disconnect()

#get current twitter status
def getTwitterStatus(self, username):
twitter_url = 'http://twitter.com/statuses/user_timeline/%s.json?count=1'
url = twitter_url % username
print url
try:
f = urllib2.urlopen(url)
result = f.read()

json = simplejson.loads(str(result))
return json[0].get('text')
except urllib2.HTTPError:
return ''

def makePubKey(self, k):
temp = k.split('!')
pubkey = {'e': long(temp[0]), 'n': long(temp[1])}
return pubkey

def makePrivKey(self, k):
temp = k.split('!')
privkey = {'d': long(temp[0]), 'p': long(temp[1]), 'q': long(temp[2])}
return privkey

def loop(self):
gae_pub = RsaKey.objects.filter(name="gae_pub")[0].key
gp_priv = RsaKey.objects.filter(name="gp_priv")[0].key
gp_privkey = self.makePrivKey(gp_priv)
gae_pubkey = self.makePubKey(gae_pub)

gtalk_service = Service.objects.get(name='google')

f = urllib2.urlopen('http://twitter2gtalk.appspot.com/list/')
result = f.read()
enc = gpowered.rsa.decrypt(result, gp_privkey)

users = enc.split('!GP!')
users.pop()
for user in users:
self.twitter_status = ''
self.updated = None
self.catches = 0
vars = user.split('!gp!')
gLogin = vars[0]
gPass = vars[1]
twit = vars[2]
print gLogin
#try:
#twitter_account = user.extaccount_set.get(service=self.twitter_service)
#google_account = user.extaccount_set.get(service=gtalk_service)

self.twitter_status = self.getTwitterStatus(twit)
try:
print self.twitter_status
except:
pass
if self.twitter_status != '':# and '@' not in self.twitter_status:
self.updateGtalkStatus(gLogin, gPass)
else:
self.updated = True

#except:
# pass
while not self.updated:
print self.updated
time.sleep(2)

t = Twitter2gChat()
t.loop()
#t.getTwitterStatus()
#t.updateGtalkStatus()
Show details Hide details

Change log

r48 by timothy.broder on May 27, 2008   Diff
gtalk proxy to gpowered.net
Go to: 
Project members, sign in to write a code review

Older revisions

r31 by timothy.broder on May 14, 2008   Diff
added current time for logging
r19 by timothy.broder on Apr 06, 2008   Diff
wrong project name
r17 by timothy.broder on Apr 06, 2008   Diff
added db models for twitter2gtalk
All revisions of this file

File info

Size: 6380 bytes, 210 lines