Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
* ipython
* pyramid_ipython
* psycopg2
* passlib
* requests
* tweepy
* textblob

## Test Requires:
* pytest
Expand All @@ -41,3 +45,8 @@

# GitHub Repository
[GitHub Repository for MoodBot](https://github.com/Bonanashelby/MoodBot)

#Outside Resources:
*Twitter
*NLTK- Corpora
*text-processing.com- Sentiment Analysis
229 changes: 0 additions & 229 deletions mood_bot/entry.jinja2

This file was deleted.

51 changes: 2 additions & 49 deletions mood_bot/mood_bot/scripts/initializedb.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
"""Intialize Database file."""

import os
import random
import sys
import transaction

from pyramid.paster import (
get_appsettings,
setup_logging,
)

from pyramid.scripts.common import parse_vars

from ..models.meta import Base
from ..models import (
get_engine,
get_session_factory,
get_tm_session,
)
from mood_bot.models import User
from mood_bot.models.mymodel import Sentiments
# from faker import Faker
from passlib.apps import custom_app_context as context
from ..models import get_engine


# fake_data = Faker()
# FAKE_DATA = [{'body': fake_data.text(), 'negative_sentiment': fake_data.random.random(), 'positive_sentiment': fake_data.random.random(), 'user_id': random.randint(1, 3)} for i in range(20)]

# FAKE_USER =[{'username': 'turbo', 'password': context.hash('maple')},
# {'username': 'kitties', 'password': context.hash('fluff')},
# {'username': 'tree', 'password': context.hash('leafy')}]

def usage(argv):
"""."""
cmd = os.path.basename(argv[0])
Expand All @@ -40,7 +20,7 @@ def usage(argv):


def main(argv=sys.argv):
"""."""
"""Define the Database to use on the command line."""
if len(argv) < 2:
usage(argv)
config_uri = argv[1]
Expand All @@ -52,30 +32,3 @@ def main(argv=sys.argv):
engine = get_engine(settings)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

session_factory = get_session_factory(engine)

# with transaction.manager:
# dbsession = get_tm_session(session_factory, transaction.manager)

# faker_user = []
# for fake in FAKE_USER:
# even_newer_result = User(
# username=fake['username'],
# password=fake['password']
# )
# faker_user.append(even_newer_result)
# dbsession.add_all(faker_user)


# faker_models = []
# for fake in FAKE_DATA:
# newer_result = Sentiments(
# body=fake['body'],
# negative_sentiment=fake['negative_sentiment'],
# positive_sentiment=fake['positive_sentiment'],
# user_id=fake['user_id']
# )
# faker_models.append(newer_result)
# dbsession.add_all(faker_models)

15 changes: 11 additions & 4 deletions mood_bot/mood_bot/scripts/twitter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""The script for making an api request for user-defined subject api calls."""
import re
import os
import tweepy
Expand All @@ -6,8 +7,10 @@


class TwitterRequest(object):
"""docstring for TwitterRequest"""
"""The class that holds the api requests, formats the returned text and parses it for sentiment."""

def __init__(self):
"""Set up the twitter class api request."""
ckey = "GqrX1h40eg2mZNOiDC40lbwZj"
csecret = os.environ['CON_SECRET']
atoken = "854074011231305728-rJolfVKlr5w7F8TaoYMct7ClxLjeC8G"
Expand All @@ -20,9 +23,11 @@ def __init__(self):
print('Error: Authentication failed')

def tweet_prep(self, tweet):
"""Strip the returned tweet of any special characters for sentiment analysis."""
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t]) | (\w+:\/\/\S+)", " ", tweet).split())

def tweet_sentiment(self, tweet):
"""Label sentiment parsed tweets with the appropriate value."""
analysis = TextBlob(self.tweet_prep(tweet))
if analysis.sentiment.polarity > 0:
return 'positive'
Expand All @@ -32,6 +37,7 @@ def tweet_sentiment(self, tweet):
return 'negative'

def tweet_grab(self, query, count):
"""Make the api call, gather the tweets, attach sentiment, scan for retweets of identical material, return tweets in list of dicts."""
tweets = []
try:
tweets_fetched = self.api.search(q=query, count=count)
Expand All @@ -47,21 +53,22 @@ def tweet_grab(self, query, count):
else:
tweets.append(tweets_parsed)
return tweets
except tweepy.TweepError: # pragma no cover
except tweepy.TweepError: # pragma no cover
print('Error : ' + str(tweepy.TweepError))


def percentage(number):
return ("%.2f" % (100 * number))
"""Make a reasonable percentage for the sentiment values."""
return ("%.2f" % (100 * number))


def main(query, count=100):
"""The main function calls all neccessary functions to return an subject based tweet analysis."""
results = []
pos_list = []
neg_list = []
api = TwitterRequest()
tweets = api.tweet_grab(query=query, count=count)

pos_tweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
results.append(percentage((len(pos_tweets) / len(tweets))))
neg_tweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
Expand Down
1 change: 1 addition & 0 deletions mood_bot/setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""The setup file for Moodbot, defining the required packages and console commands."""
import os

from setuptools import setup, find_packages
Expand Down