-
Notifications
You must be signed in to change notification settings - Fork 4
/
part_one.py
103 lines (80 loc) · 4.06 KB
/
part_one.py
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
import json
import os
import requests
from auth_data import token
# group_name = input("Введите название группы: ")
#
# url = f"https://api.vk.com/method/wall.get?domain={group_name}&count=40&access_token={token}&v=5.52"
# req = requests.get(url)
# print(req.text)
def get_wall_posts(group_name):
url = f"https://api.vk.com/method/wall.get?domain={group_name}&count=40&access_token={token}&v=5.52"
req = requests.get(url)
src = req.json()
# проверяем существует ли директория с именем группы
if os.path.exists(f"{group_name}"):
print(f"Директория с именем {group_name} уже существует!")
else:
os.mkdir(group_name)
# сохраняем данные в json файл, чтобы видеть структуру
with open(f"{group_name}/{group_name}.json", "w", encoding="utf-8") as file:
json.dump(src, file, indent=4, ensure_ascii=False)
# собираем ID новых постов в список
fresh_posts_id = []
posts = src["response"]["items"]
for fresh_post_id in posts:
fresh_post_id = fresh_post_id["id"]
fresh_posts_id.append(fresh_post_id)
"""Проверка, если файла не существует, значит это первый
парсинг группы(отправляем все новые посты). Иначе начинаем
проверку и отправляем только новые посты."""
if not os.path.exists(f"{group_name}/exist_posts_{group_name}.txt"):
print("Файла с ID постов не существует, создаём файл!")
with open(f"{group_name}/exist_posts_{group_name}.txt", "w") as file:
for item in fresh_posts_id:
file.write(str(item) + "\n")
# извлекаем данные из постов
for post in posts:
post_id = post["id"]
print(f"Отправляем пост с ID {post_id}")
try:
if "attachments" in post:
post = post["attachments"]
# забираем фото
if post[0]["type"] == "photo":
photo_quality = [
"photo_2560",
"photo_1280",
"photo_807",
"photo_604",
"photo_130",
"photo_75"
]
if len(post) == 1:
for pq in photo_quality:
if pq in post[0]["photo"]:
post_photo = post[0]["photo"][pq]
print(f"Фото с расширением {pq}")
print(post_photo)
break
else:
for post_item_photo in post:
if post_item_photo["type"] == "photo":
for pq in photo_quality:
if pq in post_item_photo["photo"]:
post_photo = post_item_photo["photo"][pq]
print(f"Фото с расширением {pq}")
print(post_photo)
break
else:
print("Линк или аудио пост")
break
except Exception:
print(f"Что-то пошло не так с постом ID {post_id}!")
else:
print("Файл с ID постов найден, начинаем выборку свежих постов!")
def main():
group_name = input("Введите название группы: ")
get_wall_posts(group_name)
if __name__ == '__main__':
main()