-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvkdocs.py
More file actions
178 lines (149 loc) · 6.83 KB
/
vkdocs.py
File metadata and controls
178 lines (149 loc) · 6.83 KB
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
#!/usr/bin/env python3
"""
vkdocs.py - скрипт для поиска публичных документов
пользователя в VK.
* id - id пользователя: '123' или 'id123' или 'anon_228'
* from document - id документа, с которого начнется поиск.
По умолчанию приблизительно возможный id последнего документа в VK.
* access_token - токен, полученный в ходе авторизации.
При первом вводе сохраняется в файле token.txt
Ссылки на найденные документы сохраняются в папке /out
Radiokot | radiokot.com.ua
С любовью для /r
"""
import http.client, sys, time, json, time, os
# Открытие соединения с VK.
def getApiConnection():
return(http.client.HTTPSConnection("api.vk.com"))
# Запрос к API VK.
def vkRequest(apiConnection, url, body = ""):
url = url + "&v=5.71"
apiConnection.request("POST", url, body)
vkResponse = apiConnection.getresponse().read()
jsonString = vkResponse.decode("utf-8")
vkJson = json.loads(jsonString)
# Обработка Too many requests per second.
if vkJson.get("response", 0) == 0:
error = vkJson.get("error", 0)
if error != 0 and error.get("error_code", 0) == 6:
time.sleep(0.35)
return(vkRequest(apiConnection, url, body))
elif error == 0:
print("Unexpected error:", jsonString[:200])
time.sleep(2)
return(vkRequest(apiConnection, url, body))
return(vkJson)
# Вывод прогресса поиска.
def showProgress(fromId, toId, foundCount):
print("Total found: " + str(foundCount) + ", current range: " +
str(fromId) + " - " + str(toId))
# Читаем id.
id = input("Enter user ID: ").replace(" ", "")
# Получаем верхнюю границу поиска по самому тупому предположению.
fromDoc = round(53376090 + (int(time.time()) - 1329165420 ) * 2.2185)
# Предлагаем ее изменить.
print("Enter start document (" + str(fromDoc) + " if empty): ", end="")
fromDocInput = input().replace(" ", "")
if fromDocInput != "":
try:
fromDocInputInt = int(fromDocInput)
if fromDocInputInt > 0:
fromDoc = fromDocInputInt
else:
raise ValueError()
except ValueError:
print("Invalid start document. Using " + str(fromDoc) + " unstead")
# Читаем токен из файла или ввода.
try:
with open("token.txt", "r") as tokenFile:
token = tokenFile.readline().replace("\r", "").replace("\n", "")
if len(token) == 0:
raise Exception
print("access_token (saved): " + token[:10] + "...")
except:
token = input("access_token: ").replace(" ", "")
# Инициализируем соединение с API.
print("\nInit connection...\n")
apiConnection = getApiConnection()
# Пробуем зарезолвить id, попутно проверяя токен.
while True:
try:
vkResponse = vkRequest(apiConnection, "/method/execute.getProfile?id=" + id + "&access_token=" + token)
except:
apiConnection.close()
time.sleep(2)
apiConnection = getApiConnection()
continue
if vkResponse.get("response", 0) in (0, None):
# Если ошибка в execute, то, скорее всего, неправильный id.
if vkResponse.get("execute_errors", 0) != 0:
print(vkResponse.get("execute_errors")[0].get("error_msg"))
# Иначе что-то с токеном.
else:
errorCode = vkResponse.get("error", 0).get("error_code", 0)
# Токен не подходит если есть ошибка 5 (запретили доступ) или 10 (он просто не тот).
if errorCode in (5, 10):
print("Invalid access_token")
# Хз что тут может быть, скриньте в тред если что.
else:
print("Something wrong: " + str(vkResponse))
# В любом случае это плохо и дальше работать не стоит.
sys.exit()
break
id = str(vkResponse.get("response"))
# Нормально все, да? Значит можно работать.
# Сохраним токен в файл, раз он нормальный.
with open("token.txt", "w") as tokenFile:
tokenFile.write(token)
# Идем от новых к старым.
# В чем суть: локально генерируем некоторое количество доков,
# потом к ним на сервере ВК добавляется еще 400.
currentDoc = fromDoc
totalFound = 0
uid = id + "_"
if not os.path.exists("out"):
os.makedirs("out")
outFileName = "out/docs" + id + ".txt"
print("User ID: " + id + ", output file: " + outFileName)
with open(outFileName, "w") as outFile:
while currentDoc > 0:
preDocs = "preDocs="
startDoc = currentDoc
# С этим параметром можно играться,
# но чем больше засовывать документов в запрос тем дольше он будет выполняться.
# Max - 8970.
while (len(preDocs) < 4000):
preDocs += uid + str(startDoc) + ","
startDoc -= 1
preDocs = preDocs[:-1]
url = "/method/execute.findDocs?uid=" + id + "&from=" + str(startDoc) + "&access_token=" + token
try:
vkResponse = vkRequest(apiConnection, url, preDocs).get("response", 0)
except (KeyboardInterrupt, SystemExit):
print("Bye!")
exit()
except:
apiConnection.close()
time.sleep(2)
apiConnection = getApiConnection()
continue
if vkResponse == None:
continue
try:
lastDoc = vkResponse.get("to")
found = vkResponse.get("found")
currentFoundCount = len(found)
totalFound += currentFoundCount
except:
print("\n" + str(url))
print("\n" + str(vkResponse))
time.sleep(5)
continue
if currentFoundCount > 0:
for item in found:
outFile.write(item.get("url", 0) + "\n")
outFile.flush()
showProgress(currentDoc, lastDoc, totalFound)
currentDoc = lastDoc
print("\nDone! Found: " + str(totalFound))
apiConnection.close()