This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
content_randompost.py
executable file
·82 lines (57 loc) · 2.8 KB
/
content_randompost.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
from handler.base_plugin import CommandPlugin
import random
class RandomPostPlugin(CommandPlugin):
__slots__ = ("commgroups", )
def __init__(self, commgroups, prefixes=None, strict=False):
"""Answers with random post from group specified in commgroups"""
if not strict:
self.commgroups = {}
for k, v in commgroups.items():
self.commgroups[k.lower()] = v
else:
self.commgroups = commgroups
if not self.commgroups:
self.commgroups = {
"kitties": -145935681,
"random": -111759315,
"savehouse": -96322217,
"octavia": -36007583
}
super().__init__(*list(self.commgroups), prefixes=prefixes, strict=strict)
self.description = ["Случайные посты из групп", "Доступные команды:"]
for k in self.commgroups.keys():
self.description.append(self.prefixes[-1] + str(k))
async def process_message(self, msg):
command, text = self.parse_message(msg, full=self.strict)
group_id = self.commgroups[command]
message, attachments = "", ""
data = await self.api.wall.get(owner_id=group_id, count=100)
if not data:
return await msg.answer("Я не могу получить посты!")
posts = data["items"]
count = data["count"]
if count < 1 or len(posts) < 1:
return await msg.answer("Не найдено ни одного поста!")
for _ in range(10):
if count > 100:
data = await self.api.wall.get(owner_id=group_id,
offset=int(random.random() * (count - 90)), count=100)
posts = data["items"]
random.shuffle(posts)
for i in posts:
if i.get("marked_as_ads") or i.get("post_type") == "copy":
continue
if i.get("text"):
if any(bad in i["text"] for bad in ("vk.com/", "vk.cc/", " подпишись ", "www.", "http://", "https://")):
continue
message = i["text"]
for a in i.get("attachments", []):
if a["type"] in ("photo", "video", "audio", "doc"):
atta = a[a["type"]]
attachments += a["type"] + str(atta["owner_id"]) + "_" + str(atta["id"])
if "access_key" in atta:
attachments += "_" + atta["access_key"]
attachments += ","
if message or attachments:
return await msg.answer(message=message, attachment=attachments)
return await msg.answer("Не найдено ни одного поста!")