-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot3.py
183 lines (154 loc) · 7.1 KB
/
bot3.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
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
179
180
181
182
183
import pickle
import sqlite3
from random import choice
import time
import lxml.html as parser
from igninterage import Igninterage
from igninterage import exceptions as ex
"""
### Ign Bot3 Demo by Psychodynae - 27/07/2020 ###
Bot que responde a perguntas ao ser mencionado em qulquer post ex:
@Bot que horas sao
"""
estrutura_demo = {
"palavras_chave": [
{
"modo": "action",
"chave": ["horas", "horas?", "que horas são?", "que horas são", "diga as horas"],
"resposta": ["horas"]
},
{
"modo": "random",
"chave": ["oi", "ola", "olá", "dae", "beleza"],
"resposta": ["sim", "oi amigo", "dae meu consagrado", "dae meu relacionado"]
},
{
"modo": "random",
"chave": ["piada", "conte uma piada"],
"resposta": ["piu", "piada 1", "piada 2"]
},
{
"modo": "random",
"chave": ["foto"],
"resposta": ["[IMG]https://picsum.photos/200/300[/IMG]"]
},
{
"modo": "random",
"chave": ["aqui-eduardo", "aquieduardo", "aqui eduardo"],
"resposta": ["cada dia mais c@gado, curto demais. -aqui-eduardo332323416",
"Amorecos No verão, com o calor, as fezes tendem a suar e a feder muito amorecos certamente irei vomitar e peidar demais chorei. -aqui-eduardo332323416",
"Nada melhor que chegar em casa amores e achar 3 kilos de fezes alta a loira em cima da cama de calcinha querendo fazer amor. -aqui-eduardo332323416",
"Quero ficar bombadão pois quero que as fezes, em banheiros públicos, saiam das privadas e peçam meu telefone amorecos. -aqui-eduardo332323416",
"amorecos fiz uma fezes nesse momento é novinha. -aqui-eduardo332323416",
"aveia podre e vencida com fungos nas bordas com aveia com cheiro de ovo podre amorecos depois perco alguma tripa na privada. -aqui-eduardo332323416",
"Chorando demais amores. -aqui-eduardo332323416",
"fezes, curto amorecos. -aqui-eduardo332323416"]
}
]
}
def get_horas():
from datetime import datetime
hora = datetime.now()
return hora.strftime('Agora são: %H:%M')
def actions(acoes):
for acao in acoes:
if acao == 'horas':
return get_horas()
def save_cache_file(content, f_name):
pickle.dump(content, open(f_name, 'wb'))
def load_cache_file(f_name):
return pickle.load(open(f_name, 'rb'))
class Bot3:
_url = 'https://www.ignboards.com/'
def __init__(self, cache_session_file, cache_file, tempo_de_loop=60):
self.ign = Igninterage(self._url, cache_session_file)
self.ign.ign_login()
self.cache_file = cache_file
while True:
print('rodando...')
self.responde()
time.sleep(tempo_de_loop)
def get_mention(self):
html = self.ign.interact_session.get(self._url + 'account/alerts').content
tree = parser.fromstring(html)
alerts = tree.find_class('contentRow-main contentRow-main--close')
mention_uris = []
for alert in alerts:
msg_text = alert.xpath('text()')[1]
if 'men' in msg_text:
mention_uris.append(alert.xpath('a/@href')[1])
return mention_uris
def ultimo_respondido_cache(self):
try:
return load_cache_file(self.cache_file)
except FileNotFoundError:
return 0
def get_quem_te_mencionou(self):
responder_list = []
mention_list = self.get_mention()
for item in reversed(mention_list):
int_post_id = int(item.split('/')[-2])
ultimo_respondido = self.ultimo_respondido_cache()
if int_post_id > ultimo_respondido:
save_cache_file(int_post_id, self.cache_file)
responder_list.append(item)
return responder_list
def procura_posts(self):
mentions = self.get_quem_te_mencionou()
post_mentions = []
for mention in mentions:
mention = 'posts' + mention.split('posts')[1]
html = self.ign.interact_session.get(self._url + mention).content
tree = parser.fromstring(html)
post_id = mention.split('/')[-2]
xpath_user = f'//*[@id="js-post-{post_id}"]'
xpath_text = f'{xpath_user}/div/div[2]/div/div[1]/div/article/div[1]'
my_username = tree.find_class('p-navgroup-linkText')[0].text_content()
thread = tree.find_class('block-container lbContainer')[0].attrib['data-lb-id'].split('-')[1]
user = tree.xpath(xpath_user)[0].attrib['data-author']
text = tree.xpath(xpath_text)[0].text_content().replace(f'@{my_username}', '').strip().lower()
post_mentions.append([thread, post_id, user, text])
return post_mentions
def responde(self):
perguntas = self.procura_posts()
for pergunta_list in perguntas:
thread, post, user, pergunta = pergunta_list
for cada in estrutura_demo['palavras_chave']:
modo = cada['modo']
cada_chave = cada['chave']
respostas = cada['resposta']
if modo == 'random':
check = any(item in pergunta for item in cada_chave)
if check:
try:
full_resp = f'[QUOTE="{user}, post: {post}"]{pergunta}[/QUOTE]{choice(respostas)}'
self.ign.comentar(full_resp, thread)
time.sleep(20)
except (ConnectionError, ex.LoginError, ex.NotXenforoPage) as err:
print(err)
print('tentar novamente ?')
if modo == 'action':
check = any(item in pergunta for item in cada_chave)
if check:
resp = actions(respostas)
if resp:
full_resp = f'[QUOTE="{user}, post: {post}"]{pergunta}[/QUOTE]{resp}'
try:
self.ign.comentar(full_resp, thread)
time.sleep(20)
except (ConnectionError, ex.LoginError, ex.NotXenforoPage) as err2:
print(err2)
print('tentar novamente ?')
else:
continue
if __name__ == '__main__':
cache_da_sessao = 'ign_cache.session'
cache_mecao_respondida = 'ign_cache.mention'
try:
print('A ultima mecao respondida foi:', load_cache_file(cache_mecao_respondida))
except FileNotFoundError:
print(cache_mecao_respondida, 'ainda nao foi criado')
# atualize esse numero com o ultima mecao respondida na pagina https://www.ignboards.com/account/alerts caso
# necessario. use a funcao save_cache_file abaixo:
# save_cache_file('123456789', cache_mecao_respondida')
Bot3(cache_da_sessao, cache_mecao_respondida, tempo_de_loop=30)