Skip to content

Instantly share code, notes, and snippets.

@11philip22
Forked from sousatg/TwitterBird,py
Created April 17, 2022 01:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 11philip22/bf9da7a327a86b45e081d87b820e22b7 to your computer and use it in GitHub Desktop.
Save 11philip22/bf9da7a327a86b45e081d87b820e22b7 to your computer and use it in GitHub Desktop.
Twitter private API wrapper class
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests, time
from lxml import etree
class TwitterHammer:
def __init__(self, username, password):
self.username = username
self.password = password
self.s = requests.Session()
self.s.headers.update = { 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0' }
self.login()
def login(self):
r = self.s.get( 'https://twitter.com/' )
htmlObj = etree.HTML( r.text )
autenticity_token = htmlObj.xpath('//*[@id="doc"]/div[1]/div/div/div/div/ul[1]/li/div[2]/form/input[3]/@value')[0]
# make the login
payload = {
'session[username_or_email]' : self.username,
'session[password]' : self.password,
'return_to_ssl' : 'true',
'scribe_log' : '',
'redirect_after_login' : '/',
'authenticity_token' : autenticity_token
}
r = self.s.post( 'https://twitter.com/sessions', data=payload )
def __is_followed__(self, html_txt):
htmlObj = etree.HTML(html_txt)
classes = htmlObj.xpath('//div[contains(@class, "UserActions")]/div[contains(@class, "user-actions")]/@class')[0]
if ' following' in classes:
return True
return False
def follow_by_name(self, screen_name):
url='https://twitter.com/%s' % screen_name
r = self.s.get( url )
# Ja segue este utilizador
if self.__is_followed__(r.text):
return 2
htmlObj = etree.HTML( r.text )
authenticity_code = htmlObj.xpath( '//*[@id="authenticity_token"]/@value' )[0]
user_id = htmlObj.xpath('//div[contains(@class, "ProfileNav")]/@data-user-id')[0]
payload = {
'authenticity_token' : authenticity_code,
'challenges_passed' : 'false',
'handles_challenges' : 1,
'inject_tweet' : 'false',
'user_id' : user_id
}
r = self.s.post( 'https://twitter.com/i/user/follow', data=payload )
# Se ocorreu um erro devolve 0
if r.status_code != 200:
return 0
return 1
def statusUpdate(self, message):
r = self.s.get( 'https://twitter.com' )
htmlObj = etree.HTML( r.text )
authenticity_code = htmlObj.xpath( '//*[@id="authenticity_token"]/@value' )[0]
payload = {
'authenticity_token' : authenticity_code,
'page_context' : 'profile',
'place_id' : '',
'status' : message,
'tagged_users' : ''
}
try:
r = self.s.post( 'https://twitter.com/i/tweet/create', data=payload )
except:
print "Erro e a enviar twittar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment