Advertisement
Guest User

Untitled

a guest
Oct 29th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. import requests
  2. import time
  3. import datetime
  4. import os
  5. import sys
  6. import os.path
  7.  
  8. def file_exists():
  9. return os.path.exists('token.txt')
  10.  
  11. def get_data_from_file():
  12. with open('token.txt', 'r') as f:
  13. lines = f.read().splitlines()
  14. userD = {
  15. 'first_name':lines[0],
  16. 'last_name':lines[1],
  17. 'uid':lines[2],
  18. 'photo':lines[3],
  19. 'token':lines[4]
  20. }
  21. f.close()
  22. return userD
  23.  
  24. def get_token(user, password):
  25. req = requests.get('https://oauth.vk.com/token?grant_type=password&client_id=3140623&client_secret=VeWdmVclDCtn6ihuP1nt&username={}&password={}&v=5.40'.format(user, password)).json()
  26. if ('error' in req):
  27. print('got an error. msg: {}'.format(req['error_description']))
  28. quit()
  29. else:
  30. token = req['access_token']
  31. user = get_user_info(req['user_id'])
  32. userD = {
  33. 'first_name':user['first_name'],
  34. 'last_name':user['last_name'],
  35. 'uid':user['uid'],
  36. 'photo':user['photo'],
  37. 'token':token
  38. }
  39. f = open('token.txt', 'w')
  40. line = '{}\n{}\n{}\n{}\n{}'.format(userD['first_name'], userD['last_name'], userD['uid'], userD['photo'], userD['token'])
  41. f.write(line)
  42. f.close()
  43.  
  44. return userD
  45.  
  46. def get_dialogs(token):
  47. req = requests.get("https://api.vk.com/method/messages.getDialogs?count=200&access_token={}".format(token)).json()
  48. if ('error' in req):
  49. print('got an error. code: {}; msg: {}'.format(req['error']['error_code'], req['error']['error_msg']))
  50. quit()
  51.  
  52. userList = []
  53. for i in range(1, len(req['response'])):
  54. if ('chat_id') in req['response'][i]:
  55. continue
  56. else:
  57. userList.append(req['response'][i]['uid'])
  58.  
  59. return userList
  60.  
  61.  
  62. def get_peer_messages(uid, token):
  63. req = requests.get("https://api.vk.com/method/messages.getHistory?rev=1&count=200&access_token={}&user_id={}".format(token, uid)).json()
  64. if ('error' in req):
  65. print('got an error. code: {}; msg: {}'.format(req['error']['error_code'], req['error']['error_msg']))
  66. quit()
  67. msgList = []
  68. page = 0
  69. limit = 200
  70. offset = 0
  71. while (len(req['response']) > 1):
  72. ln = len(req['response'])
  73. for i in range(1, ln):
  74. msg = {
  75. 'from':req['response'][i]['uid'],
  76. 'date':req['response'][i]['date'],
  77. 'body':req['response'][i]['body'],
  78. 'out':req['response'][i]['out']
  79. }
  80. msgList.append(msg)
  81. #print(len(msgList))
  82. page += 1
  83. offset = page * limit
  84. time.sleep(0.35)
  85. req = requests.get("https://api.vk.com/method/messages.getHistory?rev=1&count=200&access_token={}&user_id={}&offset={}".format(token, uid, offset)).json()
  86.  
  87. return msgList
  88.  
  89. def get_user_info(uid):
  90. req = requests.get("https://api.vk.com/method/users.get?user_ids={}&fields=has_photo,photo_50".format(uid)).json()
  91. if ('error' in req):
  92. print('got an error. code: {}; msg: {}'.format(req['error']['error_code'], req['error']['error_msg']))
  93. quit()
  94.  
  95. user = {
  96. 'first_name':req['response'][0]['first_name'],
  97. 'last_name':req['response'][0]['last_name'],
  98. 'uid':req['response'][0]['uid'],
  99. 'photo':req['response'][0]['photo_50']
  100. }
  101.  
  102. return user
  103.  
  104. def unix_to_date(time):
  105. return datetime.datetime.fromtimestamp(int(time)).strftime('%d-%m-%Y %H:%M:%S')
  106.  
  107.  
  108. def make_html(self, uid, msgs):
  109. first_name = self['first_name']
  110. last_name = self['last_name']
  111. my_uid = self['uid']
  112. my_photo = self['photo']
  113. user = get_user_info(uid)
  114.  
  115. f = open(str(uid) + '.html', 'a')
  116. f.write('<!DOCTYPE html>\n')
  117. f.write('<html>\n')
  118. f.write('<meta charset="utf-8"/>\n')
  119. f.write('<link rel="shortcut icon" href="http://vk.com/images/fav_chat.ico"/>\n')
  120. f.write('<title>VK Messages: {} {}({})</title>\n'.format(first_name, last_name, my_uid))
  121. f.write('<style>\n')
  122. f.write('h4{font-family: inherit;font-weight: 500;line-height: 1.1;color: inherit;margin-top: 10px;margin-bottom: 10px;font-size: 18px;}\n')
  123. f.write('body{font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;font-size: 14px;line-height: 1.42857143;color: #333;background-color: #fff;margin:0;}\n')
  124. f.write('hr{height: 0;margin-top: 20px;margin-bottom: 20px;border: 0;border-top: 1px solid #eee;}\n')
  125. f.write('.messages{width:1170px;margin:0 auto;text-align:left;}\n')
  126. f.write('.msg_item {overflow:hidden;}\n')
  127. f.write('.msg_deleted{overflow:hidden;background-color: #bdc3c7;margin-top: 5px;}\n')
  128. f.write('.from,.msg_body,.att_head,.attacments,.attacment,.fwd{margin-left:60px;min-height: 1px;padding-right: 15px;padding-left: 15px;}\n')
  129. f.write('.msg_item{margin-top:5px;}\n')
  130. f.write('.upic{float:left}\n')
  131. f.write('.upic img{vertical-align:top;padding:5px;width: 50px;height: 50px;}\n')
  132. f.write('.round_upic .upic img{border-radius: 50%;}\n')
  133. f.write('a {color: #337ab7;text-decoration: none;}\n')
  134. f.write('a:active, a:hover {outline: 0;}\n')
  135. f.write('a:focus, a:hover {color: #23527c;text-decoration: underline;}\n')
  136. f.write('.att_head{color:#777;}\n')
  137. f.write(".att_ico{float:left;width:11px;height:11px;margin: 3px 3px 2px; background-image:url('http://vk.com/images/icons/mono_iconset.gif');}\n")
  138. f.write('.att_photo{background-position: 0 -30px;}\n')
  139. f.write('.att_audio{background-position: 0 -222px;}\n')
  140. f.write('.att_video{background-position: 0 -75px;}\n')
  141. f.write('.att_doc{background-position: 0 -280px;}\n')
  142. f.write('.att_wall,.att_fwd{background-position: 0 -194px;}\n')
  143. f.write('.att_gift{background-position: 0 -105px;}\n')
  144. f.write('.att_sticker{background-position: 0 -362px; width: 12px; height: 12px;}\n')
  145. f.write('.att_link{background-position: 0 -237px;}\n')
  146. f.write('.attb_link a span{color:#777777 !important;}\n')
  147. f.write('.att_geo{background-position: 0 -165px;}\n')
  148. f.write('.fwd{border:2px solid #C3D1E0;border-width: 0 0 0 2px;margin-left:85px;}\n')
  149. f.write('</style>\n')
  150. f.write('</head>\n')
  151. f.write('<body>\n')
  152. f.write('<div class="messages round_upic">\n')
  153. f.write('<h4>\n')
  154. f.write('Даты сообщений: с {} по {}'.format(unix_to_date(msgs[0]['date']), unix_to_date(msgs[len(msgs)-1]['date'])))
  155. f.write('<h4>\n')
  156. f.write('Всего сообщений: {}\n'.format(len(msgs)))
  157. f.write('<hr>\n')
  158. count = 1
  159.  
  160. for msg in msgs:
  161. date = unix_to_date(msg['date'])
  162. sender = ''
  163. senderID = ''
  164. photo = ''
  165. if (msg['out'] == 1):
  166. sender = first_name + ' ' + last_name
  167. senderID = 'https://vk.com/id' + str(my_uid)
  168. photo = my_photo
  169. else:
  170. sender = user['first_name'] + ' ' + user['last_name']
  171. senderID = 'https://vk.com/id' + str(user['uid'])
  172. photo = user['photo']
  173.  
  174. f.write('<div id="msg{}" class="msg_item">\n'.format(count))
  175. f.write('<div class="upic">\n')
  176. f.write('<img src="{}" alt="[photo_50]"> </div>\n'.format(photo))
  177. f.write('<div class="from">\n')
  178. f.write('<b>{}</b>\n'.format(sender))
  179. f.write('<a href="{}" target="_blank">@id{}</a>\n'.format(senderID, msg['from']))
  180. f.write('<a href="#msg{}">{}</a></div>\n'.format(count, date))
  181. f.write('<div class="msg_body">{}</div></div>\n'.format(msg['body']))
  182. count += 1
  183.  
  184. f.write('<hr> </div> </body> </html>\n')
  185. f.close()
  186.  
  187. data = {}
  188. dialogs = {}
  189.  
  190. if (file_exists()):
  191. choice = input('old token found. use it? (y/n): ')
  192. if (choice == 'y'):
  193. data = get_data_from_file()
  194. elif (choice == 'n'):
  195. login = input('login: ')
  196. pswd = input('pass: ')
  197. print('logging in')
  198. data = get_token(login, pswd)
  199.  
  200. dialogs = get_dialogs(data['token'])
  201.  
  202. else:
  203. login = input('login: ')
  204. pswd = input('pass: ')
  205. print('logging in')
  206. data = get_token(login, pswd)
  207. dialogs = get_dialogs(data['token'])
  208.  
  209. print('got {} dialogs'.format(len(dialogs)))
  210. for dialog in dialogs:
  211. msgs = get_peer_messages(dialog, data['token'])
  212. if (len(msgs) == 0):
  213. continue
  214. print('got {} messages for peer {}'.format(len(msgs), dialog))
  215. make_html(data, dialog, msgs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement