-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtipzeny.py
411 lines (333 loc) · 14.6 KB
/
tipzeny.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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from decimal import *
import json
import re
import logging
import sqlite3
import logging.config
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from tweepy.streaming import StreamListener, Stream
from tweepy.auth import OAuthHandler
from tweepy.api import API
import datetime
# bitcoind
rpc_user=''
rpc_password=''
zeny = AuthServiceProxy("http://%s:%s@ip:port"%(rpc_user, rpc_password))
# logger
logging.config.fileConfig('logging.conf')
logger = logging.getLogger()
def get_oauth():
consumer_key = ''
consumer_secret = ''
access_key = ''
access_secret = ''
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
return auth
def DecimaltoStr(d):
return '{0:f}'.format(d)
def str_isfloat(str):
try:
float(str)
return True
except ValueError:
return False
def savetip(from_id, to_id, amount):
con = sqlite3.connect('tipzeny.db')
c = con.cursor()
create_table = '''CREATE TABLE IF NOT EXISTS tiplist(from_id int, to_id int, amount text, date_time text)'''
c.execute(create_table)
date_str = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
amount = str(amount)
ins = 'insert into tiplist (from_id, to_id, amount, date_time) values (?,?,?,?)'
tip = (from_id, to_id, amount, date_str)
c.execute(ins, tip)
con.commit()
con.close()
def gettip(user_id):
con = sqlite3.connect('tipzeny.db')
c = con.cursor()
from_ids = c.execute("select * from tiplist where from_id = ?",(user_id,))
for row in from_ids:
date = datetime.datetime.strptime(row[3], "%Y/%m/%d %H:%M:%S")
if datetime.datetime.now() > date + datetime.timedelta(days = 3):
zeny.move("tippot", "tipzeny-" + str(row[0]), float(row[2]))
c.execute("delete from tiplist where date_time = ? and from_id = ?", (row[3],row[0]))
con.commit()
tos = c.execute("select * from tiplist where to_id = ?",(user_id,))
for row in tos:
date = datetime.datetime.strptime(row[3], "%Y/%m/%d %H:%M:%S")
if datetime.datetime.now() < date + datetime.timedelta(days = 3):
zeny.move("tippot", "tipzeny-" + str(row[1]), float(row[2]))
c.execute("delete from tiplist where date_time = ? and from_id = ?", (row[3],row[0]))
con.commit()
con.close()
def helptweet(status,txt):
tweet = "@" + status.user.screen_name + u" " + txt
api.update_status(status=tweet, in_reply_to_status_id=status.id)
# ツイート処理
def on_tweet(status):
if status.text.find("RT") == -1 and status.text.find("QT") == -1 and status.user.screen_name != "zenytips":
if status.text.find("@zenytips") == -1:
return
name = status.user.screen_name
message = status.text[(status.text.find("@zenytips")+10):]
account = "tipzeny-" + str(status.user.id)
if re.search("balance", message) or re.search(u"残高", message):
gettip(status.user.id)
balance = zeny.getbalance(account,6)
all_balance = zeny.getbalance(account,0)
if all_balance < 10:
f = open('accountlist.json','r')
json_data = json.load(f)
if status.user.id in json_data:
datas = filter(lambda x: x == status.user.id, json_data)
w = open('accountlist.json','w')
json.dump(datas, w)
else:
f = open('accountlist.json','r')
json_data = json.load(f)
if status.user.id not in json_data:
json_data.append(status.user.id)
w = open('accountlist.json','w')
json.dump(json_data, w)
logger.info("check balance..." + name)
logger.info(DecimaltoStr(balance) + "ZNY all(" + DecimaltoStr(all_balance) + "ZNY)")
tweet = "@" + name + " " + DecimaltoStr(balance) + u"ZNY持ってます!"
if balance < all_balance:
tweet += u"\n(confirm中" + DecimaltoStr(all_balance-balance) + u"ZNY)"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
elif re.search("deposit", message) or re.search(u"入金", message):
address = zeny.getaccountaddress(account)
logger.info("get deposit address..." + name)
logger.info(account + " => " + address)
tweet = "@" + name + " " + address + u" に送金お願いしますっ!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
elif re.match("withdrawall", message) or re.match(u"全額出金", message):
m = re.split(" ", message)
if len(m) < 2:
helptweet(status, u"withdrawall(全額出金)の使い方\n@zenytips withdrawall 受取ZNYアドレス\nhttps://github.com/trasta298/tipzeny/wiki")
return
address = m[1]
balance = zeny.getbalance(account,6)
tax = 0.01
payying = float(balance)-tax
logger.info("withdraw..."+name)
if round(0-payying,7) >= 0:
logger.info("-> Not enough ZNY (0.01 > " + str(payying) + ")")
tweet = "@" + name + u" 残高が足りないみたいですっ!\n所持zny: " + str(balance) + "ZNY"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
validate = zeny.validateaddress(address)
if not validate['isvalid']:
logger.info("-> Invalid address")
tweet = "@" + name + u" アドレスが間違ってるみたいですっ"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
logger.info("-> Sending...")
txid = zeny.sendfrom(account,address,payying)
# taxまわりとりあえず後回し
logger.info("-> Checking transaction...")
tx = zeny.gettransaction(txid)
if tx:
fee = float(tx['fee'])
logger.info("-> Tax: " + str(fee))
else:
fee = 0
logger.info("-> No Tax")
zeny.move(account, "taxpot", tax+fee)
logger.info("-> Fee sent to taxpot: " + str(tax+fee) + "ZNY")
tweet = "@" + name + u" zenyを引き出しましたっ!(手数料0.01ZNY)\nhttp://namuyan.dip.jp/MultiLightBlockExplorer/gettxid.php?coin=zeny&txid=" + str(txid)
api.update_status(status=tweet, in_reply_to_status_id=status.id)
f = open('accountlist.json','r')
json_data = json.load(f)
if status.user.id in json_data:
datas = filter(lambda x: x == status.user.id, json_data)
w = open('accountlist.json','w')
json.dump(datas, w)
elif re.match("withdraw", message) or re.match(u"出金", message):
m = re.split(" ", message)
if len(m) < 3 or not str_isfloat(m[2]):
helptweet(status, u"withdraw(出金)の使い方\n@zenytips withdraw 受取ZNYアドレス 出金額(ZNY)\nhttps://github.com/trasta298/tipzeny/wiki")
return
address = m[1]
amot= float(m[2])
balance = zeny.getbalance(account,6)
tax = 0.01
amount = amot-tax
if amount <= 0:
tweet = "@" + name + u" 0以下の数は指定できません!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
logger.info("withdraw..."+name)
if round(Decimal(amount)-balance,7) >= 0:
logger.info("-> Not enough ZNY ("+ DecimaltoStr(balance) + " < " + str(amount) + ")")
tweet = "@" + name + u" 残高が足りないみたいですっ!\n所持zny: " + DecimaltoStr(balance) + "ZNY"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
validate = zeny.validateaddress(address)
if not validate['isvalid']:
logger.info("-> Invalid address")
tweet = "@" + name + u" アドレスが間違ってるみたいですっ"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
logger.info("-> Sending...")
txid = zeny.sendfrom(account,address,amount)
# taxまわりとりあえず後回し
logger.info("-> Checking transaction...")
tx = zeny.gettransaction(txid)
if tx:
fee = float(tx['fee'])
logger.info("-> Tax: " + str(fee))
else:
fee = 0
logger.info("-> No Tax")
zeny.move(account, "taxpot", tax+fee)
logger.info("-> Fee sent to taxpot: " + str(tax+fee) + "ZNY")
tweet = "@" + name + u" zenyを引き出しましたっ!(手数料0.01ZNY)\nhttp://namuyan.dip.jp/MultiLightBlockExplorer/gettxid.php?coin=zeny&txid=" + str(txid)
api.update_status(status=tweet, in_reply_to_status_id=status.id)
if float(balance)-amount < 10:
f = open('accountlist.json','r')
json_data = json.load(f)
if status.user.id in json_data:
datas = filter(lambda x: x == status.user.id, json_data)
w = open('accountlist.json','w')
json.dump(datas, w)
elif re.match("rain", message) or re.match(u"撒き銭", message):
m = re.split(" ", message)
if len(m) < 2 or not str_isfloat(m[1]):
helptweet(status, u"rain(撒き銭)の使い方\n@zenytips rain 撒銭額(ZNY)\nhttps://github.com/trasta298/tipzeny/wiki")
return
amount = float(m[1])
balance = zeny.getbalance(account,6)
if amount <= 0:
tweet = "@" + name + u" 0以下の数は指定できません!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
logger.info("Raining..."+str(amount)+"ZNY from "+name)
if round(Decimal(amount)-balance,7) > 0:
logger.info("-> Not enough ZNY ("+ DecimaltoStr(balance) + " < " + str(amount) + ")")
tweet = "@" + name + u" 残高が足りないみたいですっ!\n所持zny: " + DecimaltoStr(balance) + "ZNY"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
f = open('accountlist.json','r')
json_data = json.load(f)
nmb = len(json_data)
if nmb == 0:
tweet = "@" + name + u" 対象者がいないみたいです。。。"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
mon = round(amount/nmb,7)
for userid in json_data:
to_account = "tipzeny-" + str(userid)
zeny.move(account,to_account,mon)
logger.info("-> Sent.")
tweet = "@" + name + u" " + str(nmb) + u"人にそれぞれ " + str(mon) + u"ZNYずつ送りましたっ!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
elif re.match("send", message) or re.match(u"送金", message):
m = re.split(" ", message)
if len(m) < 3 or not str_isfloat(m[2]):
helptweet(status, u"send(送金)の使い方\n@zenytips send @twitterアカウント 投銭額(ZNY)\nhttps://github.com/trasta298/tipzeny/wiki")
return
if m[1][0] != "@":
helptweet(status, u"send(送金)の使い方\n@zenytips send @twitterアカウント 投銭額(ZNY)\nhttps://github.com/trasta298/tipzeny/wiki")
return
to = m[1][1:]
amount = float(m[2])
balance = zeny.getbalance(account,6)
if amount <= 0:
tweet = "@" + name + u" 0以下の数は指定できません!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
logger.info("Sending..."+str(amount)+"ZNY from "+name+" to "+to)
if round(Decimal(amount)-balance,7) > 0:
logger.info("-> Not enough ZNY ("+ DecimaltoStr(balance) + " < " + str(amount) + ")")
tweet = "@" + name + u" 残高が足りないみたいですっ!\n所持zny: " + DecimaltoStr(balance) + "ZNY"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
try:
to_user = api.get_user(to)
except:
logger.info("-> User not found.")
tweet = "@" + name + u" 送り主(" + to + u")が見つかりませんでした…"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
to_account = "tipzeny-" + str(to_user.id)
zeny.move(account,to_account,amount)
logger.info("-> Sent.")
tweet = "@" + to + u" りん姫より @" + name + u" さんから" + str(amount) + u"ZNYのお届け物だよっ!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
elif re.match("tip", message) or re.match(u"投銭", message):
m = re.split(" ", message)
if len(m) < 3 or not str_isfloat(m[2]):
helptweet(status, u"tip(投銭)の使い方\n@zenytips tip @twitterアカウント 投銭額(ZNY)\nhttps://github.com/trasta298/tipzeny/wiki")
return
if m[1][0] != "@":
helptweet(status, u"tip(投銭)の使い方\n@zenytips tip @twitterアカウント 投銭額(ZNY)\nhttps://github.com/trasta298/tipzeny/wiki")
return
to = m[1][1:]
amount = float(m[2])
balance = zeny.getbalance(account,6)
if amount <= 0:
tweet = "@" + name + u" 0以下の数は指定できません!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
logger.info("Tip..."+str(amount)+"ZNY from "+name+" to "+to)
if round(Decimal(amount)-balance,7) > 0:
logger.info("-> Not enough ZNY ("+ DecimaltoStr(balance) + " < " + str(amount) + ")")
tweet = "@" + name + u" 残高が足りないみたいですっ!\n所持zny: " + DecimaltoStr(balance) + "ZNY"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
try:
to_user = api.get_user(to)
except:
logger.info("-> User not found.")
tweet = "@" + name + u" 送り主(" + to + u")が見つかりませんでした…"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
return
to_account = "tipzeny-" + str(to_user.id)
savetip(status.user.id, to_user.id, amount)
zeny.move(account,"tippot",amount)
logger.info("-> Sent.")
tweet = "@" + to + u" りん姫より @" + name + u" さんから" + str(amount) + u"ZNYのお届け物だよっ! 3日以内にbalanceして受け取ってね!"
api.update_status(status=tweet, in_reply_to_status_id=status.id)
elif len(message) < 2:
helptweet(status, u"りん姫の使い方はここっ\nhttps://github.com/trasta298/tipzeny/wiki")
#3029861817
class Listener(StreamListener):
def on_status(self, status):
on_tweet(status)
return True
def on_data(self, raw_data):
data = json.loads(raw_data)
if 'direct_message' in data:
print(raw_data)
return True
def on_error(self, status_code):
logger.error('エラー発生: ' + str(status_code))
return True
def on_connect(self):
logger.info('Streamに接続しました')
return
def on_disconnect(self, notice):
logger.info('Streamから切断されました:' + str(notice.code))
return
def on_limit(self, track):
logger.warning('受信リミットが発生しました:' + str(track))
return
def on_timeout(self):
logger.info('タイムアウト')
return True
def on_warning(self, notice):
logger.warning('警告メッセージ:' + str(notice.message))
return
def on_exception(self, exception):
logger.error('例外エラー:' + str(exception))
return
# main
if __name__ == '__main__':
auth = get_oauth()
api = API(auth)
stream = Stream(auth, Listener(), secure=True)
stream.userstream()