Commit c9bf043e800d3d27d5b19f3fa510c9efbec0b1d2

Authored by Nadya Sutjiadi
Committed by GitHub
1 parent 5ccdcc1d

Add files via upload

Showing 1 changed file with 510 additions and 0 deletions
LINETCR/LineApi.py 0 → 100644
  1 +# -*- coding: utf-8 -*-
  2 +from Api import Poll, Talk, channel
  3 +from lib.curve.ttypes import *
  4 +import requests
  5 +import shutil
  6 +import json
  7 +from random import randint
  8 +
  9 +def def_callback(str):
  10 + print(str)
  11 +
  12 +class LINE:
  13 +
  14 + mid = None
  15 + authToken = None
  16 + cert = None
  17 + channel_access_token = None
  18 + token = None
  19 + obs_token = None
  20 + refresh_token = None
  21 +
  22 +
  23 + def __init__(self):
  24 + self.Talk = Talk()
  25 + self._session = requests.session()
  26 + self._headers = {'X-Line-Application': 'CHROMEOS\t1.4.17\tChrome_OS\t1'}
  27 +
  28 + def login(self, mail=None, passwd=None, cert=None, token=None, qr=False, callback=None):
  29 + if callback is None:
  30 + callback = def_callback
  31 + resp = self.__validate(mail,passwd,cert,token,qr)
  32 + if resp == 1:
  33 + self.Talk.login(mail, passwd, callback=callback)
  34 + elif resp == 2:
  35 + self.Talk.login(mail,passwd,cert, callback=callback)
  36 + elif resp == 3:
  37 + self.Talk.TokenLogin(token)
  38 + elif resp == 4:
  39 + self.Talk.qrLogin(callback)
  40 + else:
  41 + raise Exception("invalid arguments")
  42 +
  43 + self.authToken = self.Talk.authToken
  44 + self.cert = self.Talk.cert
  45 + self._headers = {
  46 + 'X-Line-Application': 'CHROMEOS\t1.4.17\tChrome_OS\t1',
  47 + 'X-Line-Access': self.authToken,
  48 + 'User-Agent': 'Line/1.4.17'
  49 + }
  50 +
  51 + self.Poll = Poll(self.authToken)
  52 + self.channel = channel.Channel(self.authToken)
  53 + self.channel.login()
  54 + self.mid = self.channel.mid
  55 + self.channel_access_token = self.channel.channel_access_token
  56 + self.token = self.channel.token
  57 + self.obs_token = self.channel.obs_token
  58 + self.refresh_token = self.channel.refresh_token
  59 +
  60 +
  61 + """User"""
  62 +
  63 + def getProfile(self):
  64 + return self.Talk.client.getProfile()
  65 +
  66 + def getSettings(self):
  67 + return self.Talk.client.getSettings()
  68 +
  69 + def getUserTicket(self):
  70 + return self.Talk.client.getUserTicket()
  71 +
  72 + def updateProfile(self, profileObject):
  73 + return self.Talk.client.updateProfile(0, profileObject)
  74 +
  75 + def updateSettings(self, settingObject):
  76 + return self.Talk.client.updateSettings(0, settingObject)
  77 +
  78 + def cloneContactProfile(self, mid):
  79 + contact = self.getContact(mid)
  80 + profile = self.getProfile()
  81 + profile.displayName = contact.displayName
  82 + profile.statusMessage = contact.statusMessage
  83 + profile.pictureStatus = contact.pictureStatus
  84 + self.updateDisplayPicture(profile.pictureStatus)
  85 + return self.updateProfile(profile)
  86 +
  87 + def updateDisplayPicture(self, hash_id):
  88 + return self.Talk.client.updateProfileAttribute(0, 8, hash_id)
  89 +
  90 +
  91 + """Operation"""
  92 +
  93 + def fetchOperation(self, revision, count):
  94 + return self.Poll.client.fetchOperations(revision, count)
  95 +
  96 + def fetchOps(self, rev, count):
  97 + return self.Poll.client.fetchOps(rev, count, 0, 0)
  98 +
  99 + def getLastOpRevision(self):
  100 + return self.Talk.client.getLastOpRevision()
  101 +
  102 + def stream(self):
  103 + return self.Poll.stream()
  104 +
  105 + """Message"""
  106 +
  107 + def kedapkedip(self, tomid, text):
  108 + M = Message()
  109 + M.to = tomid
  110 + t1 = "\xf4\x80\xb0\x82\xf4\x80\xb0\x82\xf4\x80\xb0\x82\xf4\x80\xb0\x82"
  111 + t2 = "\xf4\x80\x82\xb3\xf4\x8f\xbf\xbf"
  112 + rst = t1 + text + t2
  113 + M.text = rst.replace("\n", " ")
  114 + return self.Talk.client.sendMessage(0, M)
  115 +
  116 + def removeAllMessages(self, lastMessageId):
  117 + return self.Talk.client.removeAllMessages(0,lastMessageId)
  118 +
  119 + def sendMessage(self, messageObject):
  120 + return self.Talk.client.sendMessage(0,messageObject)
  121 +
  122 + def sendText(self, Tomid, text):
  123 + msg = Message()
  124 + msg.to = Tomid
  125 + msg.text = text
  126 +
  127 + return self.Talk.client.sendMessage(0, msg)
  128 + def post_content(self, url, data=None, files=None):
  129 + return self._session.post(url, headers=self._headers, data=data, files=files)
  130 +
  131 + def sendImage(self, to_, path):
  132 + M = Message(to=to_, text=None, contentType = 1)
  133 + M.contentMetadata = None
  134 + M.contentPreview = None
  135 + M_id = self.Talk.client.sendMessage(0,M).id
  136 + files = {
  137 + 'file': open(path, 'rb'),
  138 + }
  139 + params = {
  140 + 'name': 'media',
  141 + 'oid': M_id,
  142 + 'size': len(open(path, 'rb').read()),
  143 + 'type': 'image',
  144 + 'ver': '1.0',
  145 + }
  146 + data = {
  147 + 'params': json.dumps(params)
  148 + }
  149 +
  150 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  151 + print r
  152 + if r.status_code != 201:
  153 + raise Exception('Upload image failure.')
  154 + return True
  155 +
  156 + def sendImageWithURL(self, to_, url):
  157 + """Send a image with given image url
  158 +
  159 + :param url: image url to send
  160 + """
  161 + path = 'pythonLine.data'
  162 +
  163 + r = requests.get(url, stream=True)
  164 + if r.status_code == 200:
  165 + with open(path, 'w') as f:
  166 + shutil.copyfileobj(r.raw, f)
  167 + else:
  168 + raise Exception('Download image failure.')
  169 +
  170 + try:
  171 + self.sendImage(to_, path)
  172 + except Exception as e:
  173 + raise e
  174 +
  175 + def sendAudioWithURL(self, to_, url):
  176 + path = 'pythonLiness.data'
  177 + r = requests.get(url, stream=True)
  178 + if r.status_code == 200:
  179 + with open(path, 'w') as f:
  180 + shutil.copyfileobj(r.raw, f)
  181 + else:
  182 + raise Exception('Download Audio failure.')
  183 + try:
  184 + self.sendAudio(to_, path)
  185 + except Exception as e:
  186 + raise e
  187 +
  188 + def sendAudio(self, to_, path):
  189 + M = Message(to=to_,contentType = 3)
  190 + M.contentMetadata = None
  191 + M.contentPreview = None
  192 + M_id = self.Talk.client.sendMessage(0,M).id
  193 + files = {
  194 + 'file': open(path, 'rb'),
  195 + }
  196 + params = {
  197 + 'name': 'media',
  198 + 'oid': M_id,
  199 + 'size': len(open(path, 'rb').read()),
  200 + 'type': 'audio',
  201 + 'ver': '1.0',
  202 + }
  203 + data = {
  204 + 'params': json.dumps(params)
  205 + }
  206 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  207 + if r.status_code != 201:
  208 + raise Exception('Upload image failure.')
  209 + return True
  210 +
  211 + def sendVideo(self, to_, path):
  212 + M = Message(to=to_,contentType = 2)
  213 + M.contentMetadata = {
  214 + 'VIDLEN' : '0',
  215 + 'DURATION' : '0'
  216 + }
  217 + M.contentPreview = None
  218 + M_id = self.Talk.client.sendMessage(0,M).id
  219 + files = {
  220 + 'file': open(path, 'rb'),
  221 + }
  222 + params = {
  223 + 'name': 'media',
  224 + 'oid': M_id,
  225 + 'size': len(open(path, 'rb').read()),
  226 + 'type': 'video',
  227 + 'ver': '1.0',
  228 + }
  229 + data = {
  230 + 'params': json.dumps(params)
  231 + }
  232 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  233 + if r.status_code != 201:
  234 + raise Exception('Upload image failure.')
  235 + return True
  236 +
  237 + def sendVideoWithURL(self, to_, url):
  238 + path = 'pythonLines.data'
  239 + r = requests.get(url, stream=True)
  240 + if r.status_code == 200:
  241 + with open(path, 'w') as f:
  242 + shutil.copyfileobj(r.raw, f)
  243 + else:
  244 + raise Exception('Download Audio failure.')
  245 + try:
  246 + self.sendVideo(to_, path)
  247 + except Exception as e:
  248 + raise e
  249 +
  250 + def sendGif(self, to_, path):
  251 + M = Message(to=to_,contentType = 1)
  252 + M.contentMetadata = {
  253 + 'VIDLEN' : '0',
  254 + 'DURATION' : '0'
  255 + }
  256 + M.contentPreview = None
  257 + M_id = self.Talk.client.sendMessage(0,M).id
  258 + files = {
  259 + 'file': open(path, 'rb'),
  260 + }
  261 + params = {
  262 + 'name': 'media',
  263 + 'oid': M_id,
  264 + 'size': len(open(path, 'rb').read()),
  265 + 'type': 'image',
  266 + 'ver': '1.0',
  267 + }
  268 + data = {
  269 + 'params': json.dumps(params)
  270 + }
  271 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  272 + if r.status_code != 201:
  273 + raise Exception('Upload Gif failure.')
  274 + return True
  275 +
  276 + def sendGifWithURL(self, to_, url):
  277 + path = 'pythonLiness.data'
  278 + r = requests.get(url, stream=True)
  279 + if r.status_code == 200:
  280 + with open(path, 'w') as f:
  281 + shutil.copyfileobj(r.raw, f)
  282 + else:
  283 + raise Exception('Download Gif failure.')
  284 + try:
  285 + self.sendGif(to_, path)
  286 + except Exception as e:
  287 + raise e
  288 +
  289 + def sendEvent(self, messageObject):
  290 + return self.Talk.client.sendEvent(0, messageObject)
  291 +
  292 + def sendChatChecked(self, mid, lastMessageId):
  293 + return self.Talk.client.sendChatChecked(0, mid, lastMessageId)
  294 +
  295 + def getMessageBoxCompactWrapUp(self, mid):
  296 + return self.Talk.client.getMessageBoxCompactWrapUp(mid)
  297 +
  298 + def getMessageBoxCompactWrapUpList(self, start, messageBox):
  299 + return self.Talk.client.getMessageBoxCompactWrapUpList(start, messageBox)
  300 +
  301 + def getRecentMessages(self, messageBox, count):
  302 + return self.Talk.client.getRecentMessages(messageBox.id, count)
  303 +
  304 + def getMessageBox(self, channelId, messageboxId, lastMessagesCount):
  305 + return self.Talk.client.getMessageBox(channelId, messageboxId, lastMessagesCount)
  306 +
  307 + def getMessageBoxList(self, channelId, lastMessagesCount):
  308 + return self.Talk.client.getMessageBoxList(channelId, lastMessagesCount)
  309 +
  310 + def getMessageBoxListByStatus(self, channelId, lastMessagesCount, status):
  311 + return self.Talk.client.getMessageBoxListByStatus(channelId, lastMessagesCount, status)
  312 +
  313 + def getMessageBoxWrapUp(self, mid):
  314 + return self.Talk.client.getMessageBoxWrapUp(mid)
  315 +
  316 + def getMessageBoxWrapUpList(self, start, messageBoxCount):
  317 + return self.Talk.client.getMessageBoxWrapUpList(start, messageBoxCount)
  318 +
  319 + def getCover(self,mid):
  320 + h = self.getHome(mid)
  321 + objId = h["result"]["homeInfo"]["objectId"]
  322 + return "http://dl.profile.line-cdn.net/myhome/c/download.nhn?userid=" + mid+ "&oid=" + objId
  323 +
  324 + """Contact"""
  325 +
  326 +
  327 + def blockContact(self, mid):
  328 + return self.Talk.client.blockContact(0, mid)
  329 +
  330 +
  331 + def unblockContact(self, mid):
  332 + return self.Talk.client.unblockContact(0, mid)
  333 +
  334 +
  335 + def findAndAddContactsByMid(self, mid):
  336 + return self.Talk.client.findAndAddContactsByMid(0, mid)
  337 +
  338 +
  339 + def findAndAddContactsByMids(self, midlist):
  340 + for i in midlist:
  341 + self.Talk.client.findAndAddContactsByMid(0, i)
  342 +
  343 + def findAndAddContactsByUserid(self, userid):
  344 + return self.Talk.client.findAndAddContactsByUserid(0, userid)
  345 +
  346 + def findContactsByUserid(self, userid):
  347 + return self.Talk.client.findContactByUserid(userid)
  348 +
  349 + def findContactByTicket(self, ticketId):
  350 + return self.Talk.client.findContactByUserTicket(ticketId)
  351 +
  352 + def getAllContactIds(self):
  353 + return self.Talk.client.getAllContactIds()
  354 +
  355 + def getBlockedContactIds(self):
  356 + return self.Talk.client.getBlockedContactIds()
  357 +
  358 + def getContact(self, mid):
  359 + return self.Talk.client.getContact(mid)
  360 +
  361 + def getContacts(self, midlist):
  362 + return self.Talk.client.getContacts(midlist)
  363 +
  364 + def getFavoriteMids(self):
  365 + return self.Talk.client.getFavoriteMids()
  366 +
  367 + def getHiddenContactMids(self):
  368 + return self.Talk.client.getHiddenContactMids()
  369 +
  370 + def CloneContactProfile(self, mid):
  371 + contact = self.getContact(mid)
  372 + profile = self.getProfile()
  373 + profile.displayName = contact.displayName
  374 + profile.statusMessage = contact.statusMessage
  375 + profile.pictureStatus = contact.pictureStatus
  376 + self.updateDisplayPicture(profile.pictureStatus)
  377 + return self.updateProfile(profile)
  378 +
  379 + def updateDisplayPicture(self, hash_id):
  380 + return self.Talk.client.updateProfileAttribute(0, 8, hash_id)
  381 +
  382 +
  383 + """Group"""
  384 +
  385 + def findGroupByTicket(self, ticketId):
  386 + return self.Talk.client.findGroupByTicket(ticketId)
  387 +
  388 + def acceptGroupInvitation(self, groupId):
  389 + return self.Talk.client.acceptGroupInvitation(0, groupId)
  390 +
  391 + def acceptGroupInvitationByTicket(self, groupId, ticketId):
  392 + return self.Talk.client.acceptGroupInvitationByTicket(0, groupId, ticketId)
  393 +
  394 + def cancelGroupInvitation(self, groupId, contactIds):
  395 + return self.Talk.client.cancelGroupInvitation(0, groupId, contactIds)
  396 +
  397 + def createGroup(self, name, midlist):
  398 + return self.Talk.client.createGroup(0, name, midlist)
  399 +
  400 + def getGroup(self, groupId):
  401 + return self.Talk.client.getGroup(groupId)
  402 +
  403 + def getGroups(self, groupIds):
  404 + return self.Talk.client.getGroups(groupIds)
  405 +
  406 + def getGroupIdsInvited(self):
  407 + return self.Talk.client.getGroupIdsInvited()
  408 +
  409 + def getGroupIdsJoined(self):
  410 + return self.Talk.client.getGroupIdsJoined()
  411 +
  412 + def inviteIntoGroup(self, groupId, midlist):
  413 + return self.Talk.client.inviteIntoGroup(0, groupId, midlist)
  414 +
  415 + def kickoutFromGroup(self, groupId, midlist):
  416 + return self.Talk.client.kickoutFromGroup(0, groupId, midlist)
  417 +
  418 + def leaveGroup(self, groupId):
  419 + return self.Talk.client.leaveGroup(0, groupId)
  420 +
  421 + def rejectGroupInvitation(self, groupId):
  422 + return self.Talk.client.rejectGroupInvitation(0, groupId)
  423 +
  424 + def reissueGroupTicket(self, groupId):
  425 + return self.Talk.client.reissueGroupTicket(groupId)
  426 +
  427 + def updateGroup(self, groupObject):
  428 + return self.Talk.client.updateGroup(0, groupObject)
  429 +
  430 + def findGroupByTicket(self,ticketId):
  431 + return self.Talk.client.findGroupByTicket(0,ticketId)
  432 +
  433 + """Room"""
  434 +
  435 + def createRoom(self, midlist):
  436 + return self.Talk.client.createRoom(0, midlist)
  437 +
  438 + def getRoom(self, roomId):
  439 + return self.Talk.client.getRoom(roomId)
  440 +
  441 + def inviteIntoRoom(self, roomId, midlist):
  442 + return self.Talk.client.inviteIntoRoom(0, roomId, midlist)
  443 +
  444 + def leaveRoom(self, roomId):
  445 + return self.Talk.client.leaveRoom(0, roomId)
  446 +
  447 + """TIMELINE"""
  448 +
  449 + def new_post(self, text):
  450 + return self.channel.new_post(text)
  451 +
  452 + def like(self, mid, postid, likeType=1001):
  453 + return self.channel.like(mid, postid, likeType)
  454 +
  455 + def comment(self, mid, postid, text):
  456 + return self.channel.comment(mid, postid, text)
  457 +
  458 + def activity(self, limit=20):
  459 + return self.channel.activity(limit)
  460 +
  461 + def getAlbum(self, gid):
  462 +
  463 + return self.channel.getAlbum(gid)
  464 + def changeAlbumName(self, gid, name, albumId):
  465 + return self.channel.changeAlbumName(gid, name, albumId)
  466 +
  467 + def deleteAlbum(self, gid, albumId):
  468 + return self.channel.deleteAlbum(gid,albumId)
  469 +
  470 + def getNote(self,gid, commentLimit, likeLimit):
  471 + return self.channel.getNote(gid, commentLimit, likeLimit)
  472 +
  473 + def getDetail(self,mid):
  474 + return self.channel.getDetail(mid)
  475 +
  476 + def getHome(self,mid):
  477 + return self.channel.getHome(mid)
  478 +
  479 + def createAlbum(self, gid, name):
  480 + return self.channel.createAlbum(gid,name)
  481 +
  482 + def createAlbum2(self, gid, name, path):
  483 + return self.channel.createAlbum(gid, name, path, oid)
  484 +
  485 +
  486 + def __validate(self, mail, passwd, cert, token, qr):
  487 + if mail is not None and passwd is not None and cert is None:
  488 + return 1
  489 + elif mail is not None and passwd is not None and cert is not None:
  490 + return 2
  491 + elif token is not None:
  492 + return 3
  493 + elif qr is True:
  494 + return 4
  495 + else:
  496 + return 5
  497 +
  498 + def loginResult(self, callback=True):
  499 + if callback is True:
  500 + callback = def_callback
  501 +
  502 + prof = self.getProfile()
  503 +
  504 + print("===============[Chucky_Bot]================")
  505 + print(" Thanks for TCR and my friend")
  506 + print("===============[© By_Nadya]================")
  507 + print("mid -> " + prof.mid)
  508 + print("name -> " + prof.displayName)
  509 + print("authToken -> " + self.authToken)
  510 + print("cert -> " + self.cert if self.cert is not None else "")
... ...