Commit 5f85ca1e4e13a61f10b799aab6fc91b350945388

Authored by Mrtwopay
Committed by GitHub
1 parent 59999fe5

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': 'DESKTOPMAC 10.10.2-YOSEMITE-x64 MAC 4.5.0'}
  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': 'DESKTOPMAC 10.10.2-YOSEMITE-x64 MAC 4.5.0',
  47 + 'X-Line-Application': 'CHROMEOS 8.2.1-MR-TWOPAY-x64',
  48 + 'X-Line-Access': self.authToken,
  49 + 'User-Agent': 'Line/8.2.1'
  50 + }
  51 +
  52 + self.Poll = Poll(self.authToken)
  53 + self.channel = channel.Channel(self.authToken)
  54 + self.channel.login()
  55 + self.mid = self.channel.mid
  56 + self.channel_access_token = self.channel.channel_access_token
  57 + self.token = self.channel.token
  58 + self.obs_token = self.channel.obs_token
  59 + self.refresh_token = self.channel.refresh_token
  60 +
  61 +
  62 + """User"""
  63 +
  64 + def getProfile(self):
  65 + return self.Talk.client.getProfile()
  66 +
  67 + def getSettings(self):
  68 + return self.Talk.client.getSettings()
  69 +
  70 + def getUserTicket(self):
  71 + return self.Talk.client.getUserTicket()
  72 +
  73 + def updateProfile(self, profileObject):
  74 + return self.Talk.client.updateProfile(0, profileObject)
  75 +
  76 + def updateSettings(self, settingObject):
  77 + return self.Talk.client.updateSettings(0, settingObject)
  78 +
  79 + def updateProfilePicture(self, mid):
  80 + return self.Talk.client.updateProfileAttribute(0, 8, mid)
  81 +
  82 + def cloneContactProfile(self, mid):
  83 + contact = self.getContact(mid)
  84 + profile = self.getProfile()
  85 + profile.displayName = contact.displayName
  86 + profile.statusMessage = contact.statusMessage
  87 + profile.pictureStatus = contact.pictureStatus
  88 + self.updateDisplayPicture(profile.pictureStatus)
  89 + return self.updateProfile(profile)
  90 +
  91 + def updateDisplayPicture(self, hash_id):
  92 + return self.Talk.client.updateProfileAttribute(0, 8, hash_id)
  93 +
  94 +
  95 + """Operation"""
  96 +
  97 + def fetchOperation(self, revision, count):
  98 + return self.Poll.client.fetchOperations(revision, count)
  99 +
  100 + def fetchOps(self, rev, count):
  101 + return self.Poll.client.fetchOps(rev, count, 0, 0)
  102 +
  103 + def getLastOpRevision(self):
  104 + return self.Talk.client.getLastOpRevision()
  105 +
  106 + def stream(self):
  107 + return self.Poll.stream()
  108 +
  109 + """Message"""
  110 +
  111 + def kedapkedip(self, tomid, text):
  112 + M = Message()
  113 + M.to = tomid
  114 + t1 = "\xf4\x80\xb0\x82\xf4\x80\xb0\x82\xf4\x80\xb0\x82\xf4\x80\xb0\x82"
  115 + t2 = "\xf4\x80\x82\xb3\xf4\x8f\xbf\xbf"
  116 + rst = t1 + text + t2
  117 + M.text = rst.replace("\n", " ")
  118 + return self.Talk.client.sendMessage(0, M)
  119 +
  120 + def removeAllMessages(self, lastMessageId):
  121 + return self.Talk.client.removeAllMessages(0,lastMessageId)
  122 +
  123 + def sendMessage(self, messageObject):
  124 + return self.Talk.client.sendMessage(0,messageObject)
  125 +
  126 + def sendText(self, Tomid, text):
  127 + msg = Message()
  128 + msg.to = Tomid
  129 + msg.text = text
  130 +
  131 + return self.Talk.client.sendMessage(0, msg)
  132 + def post_content(self, url, data=None, files=None):
  133 + return self._session.post(url, headers=self._headers, data=data, files=files)
  134 +
  135 + def sendImage(self, to_, path):
  136 + M = Message(to=to_, text=None, contentType = 1)
  137 + M.contentMetadata = None
  138 + M.contentPreview = None
  139 + M_id = self.Talk.client.sendMessage(0,M).id
  140 + files = {
  141 + 'file': open(path, 'rb'),
  142 + }
  143 + params = {
  144 + 'name': 'media',
  145 + 'oid': M_id,
  146 + 'size': len(open(path, 'rb').read()),
  147 + 'type': 'image',
  148 + 'ver': '1.0',
  149 + }
  150 + data = {
  151 + 'params': json.dumps(params)
  152 + }
  153 +
  154 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  155 + print r
  156 + if r.status_code != 201:
  157 + raise Exception('Upload image failure.')
  158 + return True
  159 +
  160 + def sendImageWithURL(self, to_, url):
  161 + """Send a image with given image url
  162 +
  163 + :param url: image url to send
  164 + """
  165 + path = 'pythonLine.data'
  166 +
  167 + r = requests.get(url, stream=True)
  168 + if r.status_code == 200:
  169 + with open(path, 'w') as f:
  170 + shutil.copyfileobj(r.raw, f)
  171 + else:
  172 + raise Exception('Download image failure.')
  173 +
  174 + try:
  175 + self.sendImage(to_, path)
  176 + except Exception as e:
  177 + raise e
  178 +
  179 + def sendAudioWithURL(self, to_, url):
  180 + path = 'pythonLiness.data'
  181 + r = requests.get(url, stream=True)
  182 + if r.status_code == 200:
  183 + with open(path, 'w') as f:
  184 + shutil.copyfileobj(r.raw, f)
  185 + else:
  186 + raise Exception('Download Audio failure.')
  187 + try:
  188 + self.sendAudio(to_, path)
  189 + except Exception as e:
  190 + raise e
  191 +
  192 + def sendAudio(self, to_, path):
  193 + M = Message(to=to_,contentType = 3)
  194 + M.contentMetadata = None
  195 + M.contentPreview = None
  196 + M_id = self.Talk.client.sendMessage(0,M).id
  197 + files = {
  198 + 'file': open(path, 'rb'),
  199 + }
  200 + params = {
  201 + 'name': 'media',
  202 + 'oid': M_id,
  203 + 'size': len(open(path, 'rb').read()),
  204 + 'type': 'audio',
  205 + 'ver': '1.0',
  206 + }
  207 + data = {
  208 + 'params': json.dumps(params)
  209 + }
  210 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  211 + if r.status_code != 201:
  212 + raise Exception('Upload image failure.')
  213 + return True
  214 +
  215 + def sendVideo(self, to_, path):
  216 + M = Message(to=to_,contentType = 2)
  217 + M.contentMetadata = {
  218 + 'VIDLEN' : '0',
  219 + 'DURATION' : '0'
  220 + }
  221 + M.contentPreview = None
  222 + M_id = self.Talk.client.sendMessage(0,M).id
  223 + files = {
  224 + 'file': open(path, 'rb'),
  225 + }
  226 + params = {
  227 + 'name': 'media',
  228 + 'oid': M_id,
  229 + 'size': len(open(path, 'rb').read()),
  230 + 'type': 'video',
  231 + 'ver': '1.0',
  232 + }
  233 + data = {
  234 + 'params': json.dumps(params)
  235 + }
  236 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  237 + if r.status_code != 201:
  238 + raise Exception('Upload image failure.')
  239 + return True
  240 +
  241 + def sendVideoWithURL(self, to_, url):
  242 + path = 'pythonLines.data'
  243 + r = requests.get(url, stream=True)
  244 + if r.status_code == 200:
  245 + with open(path, 'w') as f:
  246 + shutil.copyfileobj(r.raw, f)
  247 + else:
  248 + raise Exception('Download Audio failure.')
  249 + try:
  250 + self.sendVideo(to_, path)
  251 + except Exception as e:
  252 + raise e
  253 +
  254 + def sendGif(self, to_, path):
  255 + M = Message(to=to_,contentType = 1)
  256 + M.contentMetadata = {
  257 + 'VIDLEN' : '0',
  258 + 'DURATION' : '0'
  259 + }
  260 + M.contentPreview = None
  261 + M_id = self.Talk.client.sendMessage(0,M).id
  262 + files = {
  263 + 'file': open(path, 'rb'),
  264 + }
  265 + params = {
  266 + 'name': 'media',
  267 + 'oid': M_id,
  268 + 'size': len(open(path, 'rb').read()),
  269 + 'type': 'image',
  270 + 'ver': '1.0',
  271 + }
  272 + data = {
  273 + 'params': json.dumps(params)
  274 + }
  275 + r = self.post_content('https://os.line.naver.jp/talk/m/upload.nhn', data=data, files=files)
  276 + if r.status_code != 201:
  277 + raise Exception('Upload Gif failure.')
  278 + return True
  279 +
  280 + def sendGifWithURL(self, to_, url):
  281 + path = 'pythonLiness.data'
  282 + r = requests.get(url, stream=True)
  283 + if r.status_code == 200:
  284 + with open(path, 'w') as f:
  285 + shutil.copyfileobj(r.raw, f)
  286 + else:
  287 + raise Exception('Download Gif failure.')
  288 + try:
  289 + self.sendGif(to_, path)
  290 + except Exception as e:
  291 + raise e
  292 +
  293 + def sendEvent(self, messageObject):
  294 + return self.Talk.client.sendEvent(0, messageObject)
  295 +
  296 + def sendChatChecked(self, mid, lastMessageId):
  297 + return self.Talk.client.sendChatChecked(0, mid, lastMessageId)
  298 +
  299 + def getMessageBoxCompactWrapUp(self, mid):
  300 + return self.Talk.client.getMessageBoxCompactWrapUp(mid)
  301 +
  302 + def getMessageBoxCompactWrapUpList(self, start, messageBox):
  303 + return self.Talk.client.getMessageBoxCompactWrapUpList(start, messageBox)
  304 +
  305 + def getRecentMessages(self, messageBox, count):
  306 + return self.Talk.client.getRecentMessages(messageBox.id, count)
  307 +
  308 + def getMessageBox(self, channelId, messageboxId, lastMessagesCount):
  309 + return self.Talk.client.getMessageBox(channelId, messageboxId, lastMessagesCount)
  310 +
  311 + def getMessageBoxList(self, channelId, lastMessagesCount):
  312 + return self.Talk.client.getMessageBoxList(channelId, lastMessagesCount)
  313 +
  314 + def getMessageBoxListByStatus(self, channelId, lastMessagesCount, status):
  315 + return self.Talk.client.getMessageBoxListByStatus(channelId, lastMessagesCount, status)
  316 +
  317 + def getMessageBoxWrapUp(self, mid):
  318 + return self.Talk.client.getMessageBoxWrapUp(mid)
  319 +
  320 + def getMessageBoxWrapUpList(self, start, messageBoxCount):
  321 + return self.Talk.client.getMessageBoxWrapUpList(start, messageBoxCount)
  322 +
  323 + def getCover(self,mid):
  324 + h = self.getHome(mid)
  325 + objId = h["result"]["homeInfo"]["objectId"]
  326 + return "http://dl.profile.line-cdn.net/myhome/c/download.nhn?userid=" + mid+ "&oid=" + objId
  327 +
  328 + """Contact"""
  329 +
  330 +
  331 + def blockContact(self, mid):
  332 + return self.Talk.client.blockContact(0, mid)
  333 +
  334 +
  335 + def unblockContact(self, mid):
  336 + return self.Talk.client.unblockContact(0, mid)
  337 +
  338 +
  339 + def findAndAddContactsByMid(self, mid):
  340 + return self.Talk.client.findAndAddContactsByMid(0, mid)
  341 +
  342 +
  343 + def findAndAddContactsByMids(self, midlist):
  344 + for i in midlist:
  345 + self.Talk.client.findAndAddContactsByMid(0, i)
  346 +
  347 + def findAndAddContactsByUserid(self, userid):
  348 + return self.Talk.client.findAndAddContactsByUserid(0, userid)
  349 +
  350 + def findContactsByUserid(self, userid):
  351 + return self.Talk.client.findContactByUserid(userid)
  352 +
  353 + def findContactByTicket(self, ticketId):
  354 + return self.Talk.client.findContactByUserTicket(ticketId)
  355 +
  356 + def getAllContactIds(self):
  357 + return self.Talk.client.getAllContactIds()
  358 +
  359 + def getBlockedContactIds(self):
  360 + return self.Talk.client.getBlockedContactIds()
  361 +
  362 + def getContact(self, mid):
  363 + return self.Talk.client.getContact(mid)
  364 +
  365 + def getContacts(self, midlist):
  366 + return self.Talk.client.getContacts(midlist)
  367 +
  368 + def getFavoriteMids(self):
  369 + return self.Talk.client.getFavoriteMids()
  370 +
  371 + def getHiddenContactMids(self):
  372 + return self.Talk.client.getHiddenContactMids()
  373 +
  374 + def CloneContactProfile(self, mid):
  375 + contact = self.getContact(mid)
  376 + profile = self.getProfile()
  377 + profile.displayName = contact.displayName
  378 + profile.statusMessage = contact.statusMessage
  379 + profile.pictureStatus = contact.pictureStatus
  380 + self.updateDisplayPicture(profile.pictureStatus)
  381 + return self.updateProfile(profile)
  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 "")
... ...