-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtwitter_bot.py
108 lines (78 loc) · 2.76 KB
/
twitter_bot.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import tweepy
import HTMLParser
import re
import random
import json
import requests
from bs4 import BeautifulSoup
#inserir seus dados do twitter aqui
consumer_key = "consumer key"
consumer_secret = "consumer secret"
access_token = "access token"
access_token_secret = "access token secret"
#faz a autenticacao e login aqui
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.me()
PATTERN = re.compile(r'/video(\d+)/.*')
#encontra a pagina
def _fetch_page(page_number):
url = 'https://www.xvideos.com/porn/portugues/' + str(page_number)
res = requests.get(url)
if res.status_code != 200:
raise Exception('Response Error: ' + str(res.status_code))
return BeautifulSoup(res.text, 'html.parser')
#encontra os videos
def _find_videos(soup):
for element in soup.select('.thumb-block > p > a'):
try:
reference = PATTERN.match(element['href']).group(1)
except AttributeError:
pass
yield element['title'], reference
#encontra os comentarios
def _get_comments(video_ref):
url_mask = 'https://www.xvideos.com/video-get-comments/{0}/0/'
url = url_mask.format(video_ref)
res = requests.post(url)
if res.status_code != 200:
raise Exception('Response Error: ' + str(res.status_code))
for item in json.loads(res.text)['comments']:
content = HTMLParser.HTMLParser().unescape(item['c']).replace('<br />', '\n')
author = HTMLParser.HTMLParser().unescape(item['n'])
if '<a href=' not in content:
yield author, content
#escolhe um comentario aleatorio
def choose_random_porn_comment():
for _ in range(10):
page = _fetch_page(random.randint(1, 40))
videos = _find_videos(page)
title, reference = random.choice(list(videos))
comments = _get_comments(reference)
try:
author, content = random.choice(list(comments))
except IndexError:
continue
return author, content, title
raise Exception('Too hard')
def main():
#comentario aleatorio = comment
comment = choose_random_porn_comment()
#printa o comentario no terminal
print(format_comment(*comment))
#formata o comentario
comentarioo = format_comment(*comment)
#posta no twitter
api.update_status(comentarioo)
#funcao para formatar o comentario
def format_comment(author, content, title):
mask = '{0} comentou o seguinte:\n{1}\n\nVi isso no video:\n{2}'
author = author.encode('utf-8')
content = content.encode('utf-8')
title = title.encode('utf-8')
return mask.format(author, content, title)
if __name__ == '__main__':
main()