Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # imports
  5.  
  6. from __future__ import absolute_import, print_function
  7. from tweepy.streaming import StreamListener
  8. from tweepy import OAuthHandler
  9. from tweepy import Stream
  10. from bs4 import BeautifulSoup
  11. import psycopg2
  12. import requests
  13. import json
  14.  
  15.  
  16. class StdOutListener(StreamListener):
  17.    
  18.     def on_data(self, raw_data):
  19.         data = json.loads(raw_data)
  20.         date =""
  21.         id = ""
  22.         location=""
  23.         content=""
  24.         source=""
  25.         if 'created_at' in data:
  26.             date = data['created_at']
  27.         if 'id' in data:
  28.             id = data['id']
  29.         if 'location' in data:
  30.             location = data['location']
  31.         if 'text' in data:
  32.             content = data['text'].replace("\n"," ")
  33.         if 'source' in data:
  34.             source = data['source']
  35.             if 'android' in source:
  36.                 source = "android"
  37.             if 'iphone' in source:
  38.                 source = "iphone"
  39.             if 'twitter' in source:
  40.                 source = "web"
  41.         print("date "+str(date)+"\nid "+str(id)+"\nlocation "+str(location)+"\ncontent "+str(content)+"\ndevice "+str(source))
  42.         return True
  43.  
  44.     def on_error(self, status):
  45.         print(status)
  46.  
  47.  
  48. # this function returns a soup page object
  49. def getPage(url):
  50.     r = requests.get(url)
  51.     data = r.text
  52.     spobj = BeautifulSoup(data, "lxml")
  53.     return spobj
  54.  
  55.  
  56. def connect():
  57.  
  58.     conn = None
  59.     try:
  60.         # connect to the PostgreSQL server
  61.         print('Connecting to the PostgreSQL database...')
  62.         conn = psycopg2.connect(host="localhost", database="dbs", user="postgres", password="postgres")
  63.  
  64.         # create a cursor
  65.         cur = conn.cursor()
  66.  
  67.         # execute a statement
  68.         print('PostgreSQL database version:')
  69.         cur.execute('SELECT version()')
  70.  
  71.         # display the PostgreSQL database server version
  72.         db_version = cur.fetchone()
  73.         print(db_version)
  74.  
  75.         # close the communication with the PostgreSQL
  76.         cur.close()
  77.     except (Exception, psycopg2.DatabaseError) as error:
  78.         print(error)
  79.     finally:
  80.         if conn is not None:
  81.             conn.close()
  82.             print('Database connection closed.')
  83.  
  84. def main():
  85.  
  86.     consumer_key = "consumerkey"
  87.     consumer_secret = "consumersecret"
  88.     access_token = "access_token"
  89.     access_token_secret = "access_token_secret"
  90.  
  91.     listener = StdOutListener()
  92.     auth = OAuthHandler(consumer_key, consumer_secret)
  93.     auth.set_access_token(access_token, access_token_secret)
  94.     stream = Stream(auth, listener)
  95.     stream.filter(track=['@realDonaldTrump'])
  96.  
  97. if __name__ == '__main__':
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement