Skip to content

Instantly share code, notes, and snippets.

@kirillsulim
Created June 18, 2015 19:06
Show Gist options
  • Save kirillsulim/8c2e0399034c51108b34 to your computer and use it in GitHub Desktop.
Save kirillsulim/8c2e0399034c51108b34 to your computer and use it in GitHub Desktop.
# This script will dump your history into out.txt
# Run script, authorize app, get token in url
# Enter token into script
# Enjoy out.txt
import requests
import webbrowser
import json
import datetime
my_id = 1 # Your id
other_id = 1 # friend id
APP_ID = '4963422' # Enter app ID
PERMISSIONS = 4096 # for messages
r = requests.get('https://oauth.vk.com/authorize', {
'client_id': APP_ID,
'scope': PERMISSIONS,
'redirect_uri': 'https://oauth.vk.com/blank.html',
'display': 'page',
'response_type': 'token',
})
webbrowser.open(r.url)
token = input('Enter token: ')
print(token)
resp = requests.get('https://api.vk.com/method/messages.getHistory', {
'offset': 0,
'count': 1,
'user_id': other_id,
'rev': 1,
'access_token': token,
'v': '5.34',
})
fix = lambda x: str(x).encode('cp1251', errors='replace').decode('cp1251')
count = json.loads(resp.text)['response']['count']
print(count)
file = open('out.txt', 'w')
for offset in range(0, count, 200):
print(offset)
resp = requests.get('https://api.vk.com/method/messages.getHistory', {
'offset': offset,
'count': 200,
'user_id': other_id,
'rev': 1,
'access_token': token,
'v': '5.34',
})
rs = json.loads(resp.text)['response']['items']
for x in rs:
d = datetime.datetime.fromtimestamp(x['date'])
text = fix(x['body'])
from_id = x['from_id']
if str(from_id) == my_id:
who = ' Я:'
else:
who = 'ОН:'
print(d, who, text, file=file)
file.close()
@kirillsulim
Copy link
Author

I wrote this to load all my history from vk.com. It takes me about 2 hours (including VK documentation reading)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment