Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- from mysql.connector import Error
- import os
- import re
- import pandas as pd
- from nltk.tokenize import word_tokenize
- from nltk.corpus import stopwords
- from nltk.stem.porter import PorterStemmer
- import nltk
- from wordcloud import WordCloud, STOPWORDS
- import numpy as np
- import matplotlib.pyplot as plt
- from textblob import TextBlob
- class TweetObject():
- """
- """
- def __init__(self, host, database, user):
- self.consumer_key = os.environ['CONSUMER_KEY']
- self.consumer_secret = os.environ['CONSUMER_SECRET']
- self.access_token = os.environ['ACCESS_TOKEN']
- self.access_token_secret = os.environ['ACCESS_TOKEN_SECRET']
- self.password = os.environ['PASSWORD']
- self.host = host
- self.database = database
- self.user = user
- def MySQLConnect(self,query):
- """
- Connects to database and extracts
- raw tweets and any other columns we
- need
- Parameters:
- ----------------
- arg1: string: SQL query
- Returns:
- ----------------
- Pandas dataframe
- """
- try:
- con = mysql.connector.connect(host = self.host, database = self.database, \
- user = self.user, password = self.password, charset = 'utf8')
- if con.is_connected():
- print("Successfully connected to database")
- cursor = con.cursor()
- query = query
- cursor.execute(query)
- data = cursor.fetchall()
- # store in dataframe
- df = pd.DataFrame(data,columns = ['date', 'tweet'])
- #print(df.head())
- except Error as e:
- print(e)
- cursor.close()
- con.close()
- # dataframe to use in other methods
- return df
Add Comment
Please, Sign In to add comment