Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Created September 17, 2015 11:29
Show Gist options
  • Save iKlotho/0da4c390fc97d6a20806 to your computer and use it in GitHub Desktop.
Save iKlotho/0da4c390fc97d6a20806 to your computer and use it in GitHub Desktop.
import requests
import time
from BeautifulSoup import BeautifulSoup
s = requests.Session()
class TwitterBot:
def __init__(self,id,pasw,twit):
# needed variables
self.url = "https://twitter.com/sessions"
self.getAuth_token = "https://twitter.com/"
self.logout_url = 'https://twitter.com/logout'
self.twit_url = "https://twitter.com/i/tweet/create"
self.ssn = requests.Session()
self.id = id
self.pasw = pasw
self.token = ''
self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
,'content-type': 'application/x-www-form-urlencoded'}
self.twit = twit
# login process
self._getAuthenticity_token()
time.sleep(2)
self._login()
time.sleep(2)
def _login(self):
print "Account has been login"
self.loginR = self.ssn.post(self.url,data = {'session[username_or_email]':self.id,'session[password]':self.pasw,
'authenticity_token': self.token}, headers = self.headers )
print "Login status_code -> " , self.loginR.status_code # check if its okey
def _logout(self):
print "Account has been logout"
self.logoutR = self.ssn.post(self.url,data = {'authenticity_token':self.token},headers = self.headers)
print "Logout status_code -> " , self.logoutR.status_code
def _getAuthenticity_token(self):
tokenR = self.ssn.get(self.getAuth_token)
soup = BeautifulSoup(tokenR.content)
for i in soup.findAll('div','js-front-language'):
self.token = i.findAll('input')[2]['value']
def _postTwit(self):
print "Twit has been send!"
self.twitR = self.ssn.post(self.twit_url, data = {'authenticity_token' : self.token ,
'is_permalink_page' : 'false',
'lang' : 'en',
'place_id' : '',
'status' : self.twit,
'tagged_users' : '' }, headers = self.headers)
print "Twit status_code -> " , self.twitR.status_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment