-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathLineApi.py
450 lines (347 loc) · 13.5 KB
/
LineApi.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
# -*- coding: utf-8 -*-
from Api import Poll, Talk, channel
from lib.curve.ttypes import *
import requests
import shutil
import json
import subprocess
from gtts import gTTS
from random import randint
def def_callback(str):
print(str)
class LINE:
mid = None
authToken = None
cert = None
channel_access_token = None
token = None
obs_token = None
refresh_token = None
def __init__(self):
self.Talk = Talk()
self._session = requests.session()
def login(self, mail=None, passwd=None, cert=None, token=None, qr=False, callback=None):
if callback is None:
callback = def_callback
resp = self.__validate(mail,passwd,cert,token,qr)
if resp == 1:
self.Talk.login(mail, passwd, callback=callback)
elif resp == 2:
self.Talk.login(mail,passwd,cert, callback=callback)
elif resp == 3:
self.Talk.TokenLogin(token)
elif resp == 4:
self.Talk.qrLogin(callback)
else:
raise Exception("invalid arguments")
self.authToken = self.Talk.authToken
self.cert = self.Talk.cert
self.Poll = Poll(self.authToken)
self.channel = channel.Channel(self.authToken)
self.channel.login()
self.mid = self.channel.mid
self.channel_access_token = self.channel.channel_access_token
self.token = self.channel.token
self.obs_token = self.channel.obs_token
self.refresh_token = self.channel.refresh_token
self._headers = {
APP_TYPE = ApplicationType.IOS
APP_NAME = 'IOSIPAD\t7.14.0\tiPhone OS\t10.12.0'
}
"""User"""
def getProfile(self):
return self.Talk.client.getProfile()
def getSettings(self):
return self.Talk.client.getSettings()
def getUserTicket(self):
return self.Talk.client.getUserTicket()
def updateProfile(self, profileObject):
return self.Talk.client.updateProfile(0, profileObject)
def updateSettings(self, settingObject):
return self.Talk.client.updateSettings(0, settingObject)
def CloneContactProfile(self, mid):
contact = self.getContact(mid)
profile = self.getProfile()
profile.displayName = contact.displayName
profile.statusMessage = contact.statusMessage
profile.pictureStatus = contact.pictureStatus
self.updateDisplayPicture(profile.pictureStatus)
return self.updateProfile(profile)
def updateDisplayPicture(self, hash_id):
return self.Talk.client.updateProfileAttribute(0, 8, hash_id)
"""Operation"""
def fetchOperation(self, revision, count):
return self.Poll.client.fetchOperations(revision, count)
def fetchOps(self, rev, count):
return self.Poll.client.fetchOps(rev, count, 0, 0)
def getLastOpRevision(self):
return self.Talk.client.getLastOpRevision()
def stream(self):
return self.Poll.stream()
"""Message"""
def kedapkedip(self, tomid, text):
M = Message()
M.to = tomid
t1 = "\xf4\x80\xb0\x82\xf4\x80\xb0\x82\xf4\x80\xb0\x82\xf4\x80\xb0\x82"
t2 = "\xf4\x80\x82\xb3\xf4\x8f\xbf\xbf"
rst = t1 + text + t2
M.text = rst.replace("\n", " ")
return self.Talk.client.sendMessage(0, M)
def sendMessage(self, messageObject):
return self.Talk.client.sendMessage(0,messageObject)
def sendText(self, Tomid, text):
msg = Message()
msg.to = Tomid
msg.text = text
return self.Talk.client.sendMessage(0, msg)
def post_content(self, url, data=None, files=None):
return self._session.post(url, headers=self._headers, data=data, files=files)
def sendImage(self, to_, path):
M = Message(to=to_, text=None, contentType = 1)
M.contentMetadata = None
M.contentPreview = None
M_id = self.Talk.client.sendMessage(0,M).id
files = {
'file': open(path, 'rb'),
}
params = {
'name': 'media',
'oid': M_id,
'size': len(open(path, 'rb').read()),
'type': 'image',
'ver': '1.0',
}
data = {
'params': json.dumps(params)
}
r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
print r
if r.status_code != 201:
raise Exception('Upload image failure.')
return True
def sendImageWithURL(self, to_, url):
"""Send a image with given image url
:param url: image url to send
"""
path = 'pythonLine.data'
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(path, 'w') as f:
shutil.copyfileobj(r.raw, f)
else:
raise Exception('Download image failure.')
try:
self.sendImage(to_, path)
except Exception as e:
raise e
def sendAudioWithURL(self, to_, url):
path = 'pythonLiness.data'
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(path, 'w') as f:
shutil.copyfileobj(r.raw, f)
else:
raise Exception('Download Audio failure.')
try:
self.sendAudio(to_, path)
except Exception as e:
raise e
def sendAudio(self, to_, path):
M = Message(to=to_,contentType = 3)
M.contentMetadata = None
M.contentPreview = None
M_id = self.Talk.client.sendMessage(0,M).id
files = {
'file': open(path, 'rb'),
}
params = {
'name': 'media',
'oid': M_id,
'size': len(open(path, 'rb').read()),
'type': 'audio',
'ver': '1.0',
}
data = {
'params': json.dumps(params)
}
r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
if r.status_code != 201:
raise Exception('Upload image failure.')
return True
def sendVideo(self, to_, path):
M = Message(to=to_,contentType = 2)
M.contentMetadata = {
'VIDLEN' : '0',
'DURATION' : '0'
}
M.contentPreview = None
M_id = self.Talk.client.sendMessage(0,M).id
files = {
'file': open(path, 'rb'),
}
params = {
'name': 'media',
'oid': M_id,
'size': len(open(path, 'rb').read()),
'type': 'video',
'ver': '1.0',
}
data = {
'params': json.dumps(params)
}
r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
if r.status_code != 201:
raise Exception('Upload image failure.')
return True
def sendVideoWithURL(self, to_, url):
path = 'pythonLines.data'
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(path, 'w') as f:
shutil.copyfileobj(r.raw, f)
else:
raise Exception('Download Audio failure.')
try:
self.sendVideo(to_, path)
except Exception as e:
raise e
def sendEvent(self, messageObject):
return self._client.sendEvent(0, messageObject)
def sendChatChecked(self, mid, lastMessageId):
return self.Talk.client.sendChatChecked(0, mid, lastMessageId)
def getMessageBoxCompactWrapUp(self, mid):
return self.Talk.client.getMessageBoxCompactWrapUp(mid)
def getMessageBoxCompactWrapUpList(self, start, messageBox):
return self.Talk.client.getMessageBoxCompactWrapUpList(start, messageBox)
def getRecentMessages(self, messageBox, count):
return self.Talk.client.getRecentMessages(messageBox.id, count)
def getMessageBox(self, channelId, messageboxId, lastMessagesCount):
return self.Talk.client.getMessageBox(channelId, messageboxId, lastMessagesCount)
def getMessageBoxList(self, channelId, lastMessagesCount):
return self.Talk.client.getMessageBoxList(channelId, lastMessagesCount)
def getMessageBoxListByStatus(self, channelId, lastMessagesCount, status):
return self.Talk.client.getMessageBoxListByStatus(channelId, lastMessagesCount, status)
def getMessageBoxWrapUp(self, mid):
return self.Talk.client.getMessageBoxWrapUp(mid)
def getMessageBoxWrapUpList(self, start, messageBoxCount):
return self.Talk.client.getMessageBoxWrapUpList(start, messageBoxCount)
def getCover(self,mid):
h = self.getHome(mid)
objId = h["result"]["homeInfo"]["objectId"]
return "http://dl.profile.line-cdn.net/myhome/c/download.nhn?userid=" + mid+ "&oid=" + objId
"""Contact"""
def blockContact(self, mid):
return self.Talk.client.blockContact(0, mid)
def unblockContact(self, mid):
return self.Talk.client.unblockContact(0, mid)
def findAndAddContactsByMid(self, mid):
return self.Talk.client.findAndAddContactsByMid(0, mid)
def findAndAddContactsByMids(self, midlist):
for i in midlist:
self.Talk.client.findAndAddContactsByMid(0, i)
def findAndAddContactsByUserid(self, userid):
return self.Talk.client.findAndAddContactsByUserid(0, userid)
def findContactsByUserid(self, userid):
return self.Talk.client.findContactByUserid(userid)
def findContactByTicket(self, ticketId):
return self.Talk.client.findContactByUserTicket(ticketId)
def getAllContactIds(self):
return self.Talk.client.getAllContactIds()
def getBlockedContactIds(self):
return self.Talk.client.getBlockedContactIds()
def getContact(self, mid):
return self.Talk.client.getContact(mid)
def getContacts(self, midlist):
return self.Talk.client.getContacts(midlist)
def getFavoriteMids(self):
return self.Talk.client.getFavoriteMids()
def getHiddenContactMids(self):
return self.Talk.client.getHiddenContactMids()
"""Group"""
def findGroupByTicket(self, ticketId):
return self.Talk.client.findGroupByTicket(ticketId)
def acceptGroupInvitation(self, groupId):
return self.Talk.client.acceptGroupInvitation(0, groupId)
def acceptGroupInvitationByTicket(self, groupId, ticketId):
return self.Talk.client.acceptGroupInvitationByTicket(0, groupId, ticketId)
def cancelGroupInvitation(self, groupId, contactIds):
return self.Talk.client.cancelGroupInvitation(0, groupId, contactIds)
def createGroup(self, name, midlist):
return self.Talk.client.createGroup(0, name, midlist)
def getGroup(self, groupId):
return self.Talk.client.getGroup(groupId)
def getGroups(self, groupIds):
return self.Talk.client.getGroups(groupIds)
def getGroupIdsInvited(self):
return self.Talk.client.getGroupIdsInvited()
def getGroupIdsJoined(self):
return self.Talk.client.getGroupIdsJoined()
def inviteIntoGroup(self, groupId, midlist):
return self.Talk.client.inviteIntoGroup(0, groupId, midlist)
def kickoutFromGroup(self, groupId, midlist):
return self.Talk.client.kickoutFromGroup(0, groupId, midlist)
def leaveGroup(self, groupId):
return self.Talk.client.leaveGroup(0, groupId)
def rejectGroupInvitation(self, groupId):
return self.Talk.client.rejectGroupInvitation(0, groupId)
def reissueGroupTicket(self, groupId):
return self.Talk.client.reissueGroupTicket(groupId)
def updateGroup(self, groupObject):
return self.Talk.client.updateGroup(0, groupObject)
def findGroupByTicket(self,ticketId):
return self.Talk.client.findGroupByTicket(0,ticketId)
"""Room"""
def createRoom(self, midlist):
return self.Talk.client.createRoom(0, midlist)
def getRoom(self, roomId):
return self.Talk.client.getRoom(roomId)
def inviteIntoRoom(self, roomId, midlist):
return self.Talk.client.inviteIntoRoom(0, roomId, midlist)
def leaveRoom(self, roomId):
return self.Talk.client.leaveRoom(0, roomId)
"""TIMELINE"""
def new_post(self, text):
return self.channel.new_post(text)
def like(self, mid, postid, likeType=1001):
return self.channel.like(mid, postid, likeType)
def comment(self, mid, postid, text):
return self.channel.comment(mid, postid, text)
def activity(self, limit=20):
return self.channel.activity(limit)
def getAlbum(self, gid):
return self.channel.getAlbum(gid)
def changeAlbumName(self, gid, name, albumId):
return self.channel.changeAlbumName(gid, name, albumId)
def deleteAlbum(self, gid, albumId):
return self.channel.deleteAlbum(gid,albumId)
def getNote(self,gid, commentLimit, likeLimit):
return self.channel.getNote(gid, commentLimit, likeLimit)
def getDetail(self,mid):
return self.channel.getDetail(mid)
def getHome(self,mid):
return self.channel.getHome(mid)
def createAlbum(self, gid, name):
return self.channel.createAlbum(gid,name)
def createAlbum2(self, gid, name, path):
return self.channel.createAlbum(gid, name, path, oid)
def __validate(self, mail, passwd, cert, token, qr):
if mail is not None and passwd is not None and cert is None:
return 1
elif mail is not None and passwd is not None and cert is not None:
return 2
elif token is not None:
return 3
elif qr is True:
return 4
else:
return 5
def loginResult(self, callback=None):
if callback is None:
callback = def_callback
prof = self.getProfile()
print("<=[BotTobyDanFidho]>=>")
print("mid :" + prof.mid)
print("name : " + prof.displayName)
print("authToken : " + self.authToken)
print("cert : " + self.cert if self.cert is not None else "")
print(" Thx for using My bot ")