Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # imports
- from __future__ import absolute_import, print_function
- from tweepy.streaming import StreamListener
- from tweepy import OAuthHandler
- from tweepy import Stream
- from bs4 import BeautifulSoup
- import psycopg2
- import requests
- import json
- class StdOutListener(StreamListener):
- def on_data(self, raw_data):
- data = json.loads(raw_data)
- date =""
- id = ""
- location=""
- content=""
- source=""
- if 'created_at' in data:
- date = data['created_at']
- if 'id' in data:
- id = data['id']
- if 'location' in data:
- location = data['location']
- if 'text' in data:
- content = data['text'].replace("\n"," ")
- if 'source' in data:
- source = data['source']
- if 'android' in source:
- source = "android"
- if 'iphone' in source:
- source = "iphone"
- if 'twitter' in source:
- source = "web"
- print("date "+str(date)+"\nid "+str(id)+"\nlocation "+str(location)+"\ncontent "+str(content)+"\ndevice "+str(source))
- return True
- def on_error(self, status):
- print(status)
- # this function returns a soup page object
- def getPage(url):
- r = requests.get(url)
- data = r.text
- spobj = BeautifulSoup(data, "lxml")
- return spobj
- def connect():
- conn = None
- try:
- # connect to the PostgreSQL server
- print('Connecting to the PostgreSQL database...')
- conn = psycopg2.connect(host="localhost", database="dbs", user="postgres", password="postgres")
- # create a cursor
- cur = conn.cursor()
- # execute a statement
- print('PostgreSQL database version:')
- cur.execute('SELECT version()')
- # display the PostgreSQL database server version
- db_version = cur.fetchone()
- print(db_version)
- # close the communication with the PostgreSQL
- cur.close()
- except (Exception, psycopg2.DatabaseError) as error:
- print(error)
- finally:
- if conn is not None:
- conn.close()
- print('Database connection closed.')
- def main():
- consumer_key = "consumerkey"
- consumer_secret = "consumersecret"
- access_token = "access_token"
- access_token_secret = "access_token_secret"
- listener = StdOutListener()
- auth = OAuthHandler(consumer_key, consumer_secret)
- auth.set_access_token(access_token, access_token_secret)
- stream = Stream(auth, listener)
- stream.filter(track=['@realDonaldTrump'])
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement