Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ALEJANDRO LARES PACHECO
BotSorteosTwitter
Commits
d7184c41
Commit
d7184c41
authored
Jan 13, 2022
by
ALEJANDRO LARES PACHECO
Browse files
Para presentar
parent
0f588ad2
Changes
1
Hide whitespace changes
Inline
Side-by-side
botSorteosTwitter.py
0 → 100644
View file @
d7184c41
import
requests
import
os
import
json
import
tweepy
from
requests_oauthlib
import
OAuth1Session
# Claves y tokens:
consumer_key
=
""
consumer_secret
=
""
access_token
=
""
access_token_secret
=
""
bearer_token
=
r
""
# URL de api twitter
search_url
=
"https://api.twitter.com/2/tweets/search/recent"
query_params
=
{
'query'
:
'(sorteo OR sorteazo OR giveaway) rt (ps5 OR switch OR Xbox OR logitech OR iPhone 13) -is:retweet -is:reply -is:quote is:verified'
,
'expansions'
:
'author_id'
,
'user.fields'
:
'name,username'
,
'max_results'
:
10
}
def
bearer_oauth
(
r
):
"""
Method required by bearer token authentication.
"""
r
.
headers
[
"Authorization"
]
=
f
"Bearer
{
bearer_token
}
"
r
.
headers
[
"User-Agent"
]
=
"v2RecentSearchPython"
return
r
def
search_recent_tweets
(
url
,
params
):
response
=
requests
.
get
(
url
,
auth
=
bearer_oauth
,
params
=
params
)
print
(
response
.
status_code
)
if
response
.
status_code
!=
200
:
raise
Exception
(
response
.
status_code
,
response
.
text
)
return
response
.
json
()
# given a string which may include twitter users, return said users in a list
def
get_usernames
(
text
):
splitted
=
text
.
split
(
'@'
)
splitted
.
pop
(
0
)
print
(
splitted
)
usernames_list
=
[]
for
words
in
splitted
:
usernames_list
.
append
(
words
.
split
(
' '
)[
0
].
splitlines
()[
0
].
split
(
'!'
)[
0
])
return
usernames_list
def
main
():
json_response_search
=
search_recent_tweets
(
search_url
,
query_params
)
print
(
json
.
dumps
(
json_response_search
,
indent
=
4
,
sort_keys
=
True
))
#Ahora querremos procesar los tweets encontrados, por ejemplo el primero de los resultados:
print
(
'-----------------TESTING------------------'
)
print
(
json_response_search
.
get
(
'data'
)[
0
].
get
(
'id'
))
# ID del tweet
print
(
json_response_search
.
get
(
'data'
)[
0
].
get
(
'text'
))
print
(
json_response_search
.
get
(
'data'
)[
0
].
get
(
'author_id'
))
#ID del autor
print
(
json_response_search
.
get
(
'includes'
).
get
(
'users'
)[
0
].
get
(
'username'
))
print
(
json_response_search
.
keys
())
#INICIALIZAMOS TWEEPY
client
=
tweepy
.
Client
(
bearer_token
=
bearer_token
,
consumer_key
=
consumer_key
,
consumer_secret
=
consumer_secret
,
access_token
=
access_token
,
access_token_secret
=
access_token_secret
)
# the ID of the tweet to be retweeted
for
i
in
range
(
len
(
json_response_search
.
get
(
'data'
))):
ID_tweet
=
json_response_search
.
get
(
'data'
)[
i
].
get
(
'id'
)
################## REACCIONAMOS A TODOS LOS TWEETS ENCONTRADOS #################
# PASO 1: RETWEET DEL TWEET
client
.
retweet
(
ID_tweet
)
# PASO 2: LIKE AL TWEET
client
.
like
(
ID_tweet
)
# PASO 3: Seguir a todos los @ presentes en el tweet y al creador del tweets
# 3.1: Creador del tweet:
client
.
follow_user
(
json_response_search
.
get
(
'data'
)[
i
].
get
(
'author_id'
))
# 3.2: Mencionados en el tweet:
usernames_to_follow
=
get_usernames
(
json_response_search
.
get
(
'data'
)[
i
].
get
(
'text'
))
for
user
in
usernames_to_follow
:
id_to_follow
=
client
.
get_user
(
username
=
user
).
_asdict
().
get
(
'data'
).
id
client
.
follow_user
(
id_to_follow
)
print
(
'Just followed'
,
user
)
if
__name__
==
"__main__"
:
main
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment