Advertisement
verretor

Twitter Bot Base

Jan 19th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. #!/usr/bin/python
  2. # coding=utf-8
  3. #
  4. # Copyright (c) 2017 Benoit Verret <benoit.verret@protonmail.ch>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. #
  18.  
  19. from bs4 import BeautifulSoup
  20.  
  21. import datetime
  22. import getpass
  23. import requests
  24. import sys
  25. import time
  26.  
  27. def check_login(html_doc):
  28.     screen_name = u''
  29.     soup = BeautifulSoup(html_doc, 'html5lib', from_encoding="utf-8")
  30.  
  31.     div_screen_name = soup.find('div', attrs={'class':'account-group js-mini-current-user'})
  32.     if div_screen_name == None:
  33.         return u''
  34.     if u'data-screen-name' not in div_screen_name.attrs:
  35.         return u''
  36.     screen_name = div_screen_name[u'data-screen-name']
  37.  
  38.     return screen_name
  39.  
  40. def check_mentions(reply, screen_name):
  41.     div_tweet = reply.find('div')
  42.     if div_tweet == None:
  43.         return False
  44.     if u'data-mentions' not in div_tweet.attrs:
  45.         return False
  46.     mention = screen_name in div_tweet[u'data-mentions']
  47.  
  48.     return mention
  49.  
  50. if __name__ == '__main__':
  51.     headers = {}
  52.     headers['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'
  53.     sess = requests.session()
  54.     sess.headers = headers
  55.  
  56.     # session[username_or_email]
  57.     username = raw_input('Phone, email or username: ')
  58.     # session[password]
  59.     password = getpass.getpass()
  60.  
  61.     while True:
  62.         # Wait 5 minutes and log back in when something goes wrong.
  63.         problem = False
  64.  
  65.         screen_name = u''
  66.  
  67.         print
  68.         print('###############################################')
  69.         print
  70.        
  71.         # GET https://twitter.com/login
  72.         url = 'https://twitter.com/login'
  73.         response = sess.get(url, headers=headers)
  74.  
  75.         # Restart bot if status code is not 200.
  76.         if response.status_code == 200:
  77.             resp = response.content
  78.         else:
  79.             print >> sys.stderr, 'Error ' + str(response.status_code)
  80.             problem = True
  81.        
  82.         if problem == False:
  83.             soup = BeautifulSoup(resp, 'html5lib', from_encoding="utf-8")
  84.            
  85.             input_token = soup.find('input', attrs={'name':'authenticity_token'})
  86.             auth_token = input_token[u'value']
  87.  
  88.             time.sleep(5)
  89.            
  90.             # POST https://twitter.com/sessions
  91.             payload = {}
  92.             payload['session[username_or_email]'] = username
  93.             payload['session[password]'] = password
  94.             payload['authenticity_token'] = auth_token
  95.             payload['scribe_log'] = ''
  96.             payload['redirect_after_login'] = ''
  97.             payload['remember_me'] = '1'
  98.  
  99.             url = 'https://twitter.com/sessions'
  100.             response = sess.post(url, data=payload, headers=headers)
  101.  
  102.             if response.status_code == 200:
  103.                 resp = response.content
  104.             else:
  105.                 print >> sys.stderr, 'Error ' + str(response.status_code)
  106.                 problem = True
  107.  
  108.         # Restart bot if not logged in.
  109.         if problem == False:
  110.             screen_name = check_login(resp)
  111.             if screen_name == u'':
  112.                 print >> sys.stderr, url
  113.                 print >> sys.stderr, 'Not logged in.'
  114.                 problem = True
  115.  
  116.             time.sleep(5)
  117.            
  118.         while problem == False:
  119.             # GET https://twitter.com/i/notifications
  120.             url = 'https://twitter.com/i/notifications'
  121.             response = sess.get(url, headers=headers)
  122.  
  123.             if response.status_code == 200:
  124.                 resp = response.content
  125.             else:
  126.                 print >> sys.stderr, url
  127.                 print >> sys.stderr, 'Error ' + str(response.status_code)
  128.                 break
  129.  
  130.             screen_name = check_login(resp)
  131.             print(screen_name)
  132.             if screen_name == u'':
  133.                 print >> sys.stderr, url
  134.                 print >> sys.stderr, 'Not logged in.'
  135.                 break
  136.  
  137.             time.sleep(5)
  138.                
  139.             soup = BeautifulSoup(resp, 'html5lib', from_encoding="utf-8")
  140.  
  141.             # Find replies.
  142.             replies = soup.find_all('li', attrs={'data-component-context':'reply_activity'})
  143.  
  144.             for reply in replies:
  145.                 if 'highlighted' in reply[u'class']:
  146.                     tweet_id = reply[u'data-item-id']
  147.                     tweet_header = reply.find('div', attrs={'class':'stream-item-header'})
  148.                     tweet_content = reply.find('div', attrs={'class':'js-tweet-text-container'})
  149.                     print(tweet_id)
  150.                     print(tweet_header.text)
  151.                     print(tweet_content.text)
  152.                     print
  153.                     print('###############################################')
  154.                     print
  155.             print(datetime.datetime.now())
  156.  
  157.             time.sleep(60)
  158.  
  159.         time.sleep(300)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement