Guest User

Untitled

a guest
Oct 4th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import mysql.connector
  2. from mysql.connector import Error
  3. import os
  4. import re
  5. import pandas as pd
  6. from nltk.tokenize import word_tokenize
  7. from nltk.corpus import stopwords
  8. from nltk.stem.porter import PorterStemmer
  9. import nltk
  10. from wordcloud import WordCloud, STOPWORDS
  11. import numpy as np
  12. import matplotlib.pyplot as plt
  13. from textblob import TextBlob
  14.  
  15.  
  16. class TweetObject():
  17. """
  18.  
  19. """
  20.  
  21. def __init__(self, host, database, user):
  22. self.consumer_key = os.environ['CONSUMER_KEY']
  23. self.consumer_secret = os.environ['CONSUMER_SECRET']
  24. self.access_token = os.environ['ACCESS_TOKEN']
  25. self.access_token_secret = os.environ['ACCESS_TOKEN_SECRET']
  26. self.password = os.environ['PASSWORD']
  27. self.host = host
  28. self.database = database
  29. self.user = user
  30.  
  31.  
  32.  
  33. def MySQLConnect(self,query):
  34. """
  35. Connects to database and extracts
  36. raw tweets and any other columns we
  37. need
  38. Parameters:
  39. ----------------
  40. arg1: string: SQL query
  41. Returns:
  42. ----------------
  43. Pandas dataframe
  44. """
  45.  
  46. try:
  47. con = mysql.connector.connect(host = self.host, database = self.database, \
  48. user = self.user, password = self.password, charset = 'utf8')
  49.  
  50. if con.is_connected():
  51. print("Successfully connected to database")
  52.  
  53. cursor = con.cursor()
  54. query = query
  55. cursor.execute(query)
  56.  
  57. data = cursor.fetchall()
  58. # store in dataframe
  59. df = pd.DataFrame(data,columns = ['date', 'tweet'])
  60. #print(df.head())
  61.  
  62.  
  63.  
  64. except Error as e:
  65. print(e)
  66.  
  67. cursor.close()
  68. con.close()
  69.  
  70. # dataframe to use in other methods
  71. return df
Add Comment
Please, Sign In to add comment