Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Knupp committed Jan 30, 2014
1 parent 46ff5cb commit a8f3405
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 10 deletions.
29 changes: 19 additions & 10 deletions app.py
@@ -1,26 +1,32 @@
"""Find and record references to a person or brand on the Internet."""

import sys
import json
import pprint

from flask import Flask, make_response, render_template
from flask.ext.sqlalchemy import SQLAlchemy
from birdy.twitter import AppClient

from sqlalchemy import create_engine
from models import Source, Mention, Base
from sqlalchemy.orm import sessionmaker

CONSUMER_KEY = 'XCvzOpRBPjKoWFMSYnHqHg'
CONSUMER_SECRET = 'flO2rrzu6xss6ubnJUS8hp0nhgbSYp8cjjCjnaHCzG8'
client = AppClient(CONSUMER_KEY, CONSUMER_SECRET)
access_token = client.get_access_token()

engine = create_engine('sqlite:///sqlite.db')
Session = sessionmaker(bind=engine)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite+pysqlite:///sqlite.db'
db = SQLAlchemy(app)

@app.route('/')
def index():
return render_template('index.html')

def get_twitter_mentions():
response = client.api.search.tweets.get(q='jeffknupp.com', count=100)
statuses = response.data.statuses
session = Session()
session = db.session()
twitter = session.query(Source).get(1)
for status in statuses:
if not session.query(Mention).filter(Mention.domain_id==status.id_str).count():
Expand All @@ -30,17 +36,20 @@ def get_twitter_mentions():
source=twitter,
domain_id=status.id_str)
session.add(m)
else:
print 'Skipping already seen mention'

session.commit()

@app.route('/mentions')
def show_mentions():
session = Session()
get_twitter_mentions()
session = db.session()
mentions = session.query(Mention).all()
values = [mention.to_json() for mention in mentions]
response = make_response()
response.data = json.dumps(values)
return response

def main():
get_twitter_mentions()
app.run(debug=True)

if __name__ == '__main__':
sys.exit(main())
12 changes: 12 additions & 0 deletions models.py
Expand Up @@ -31,3 +31,15 @@ class Mention(Base):
def __str__(self):
"""Return the string representation of a mention."""
return self.text

def to_json(self):
return {
'id': self.id,
'domain_id': self.domain_id,
'source': self.source.name,
'text': self.text,
'associated_user': self.associated_user,
'seen': self.seen,
'recorded_at': str(self.recorded_at),
'occurred_at': str(self.occurred_at)}

Binary file modified sqlite.db
Binary file not shown.
6 changes: 6 additions & 0 deletions static/css/style.css
@@ -0,0 +1,6 @@
.mention-true {
text-color: lightGrey;
}
.mention-false {
font-weight: 700;
}
21 changes: 21 additions & 0 deletions static/js/controllers.js
@@ -0,0 +1,21 @@
var eavesApp = angular.module('eavesApp', []);

eavesApp.controller('MentionCtrl', function ($scope, $http) {

$http.get('/mentions').success(function (data) {
$scope.mentions = data;
});

$scope.addMention = function() {
$scope.mentions.push({text: $scope.mentionText, read:false});
$scope.mentionText = "";
};
$scope.unread = function() {
var count = 0;
angular.forEach($scope.mentions, function(mention) {
count += mention.read ? 1 : 0;
});
return count;
};
$scope.orderProp = 'name';
});
Binary file added templates/.index.html.swp
Binary file not shown.
43 changes: 43 additions & 0 deletions templates/index.html
@@ -0,0 +1,43 @@
<!doctype html>
<html ng-app="eavesApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<script src="../static/js/controllers.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="../static/css/style.css">
</head>
<body>

<div class="container" ng-controller="MentionCtrl">
<h2>Mentions</h2>
<div class="row">
<div class="col-md-8">


<p>Search: <input ng-model="query"></p>
<p>Order By:</p>
<select ng-model="orderProp">
<option value="found">Newest</option>
<option value="name">Alphabetical</option>
</select>


</div>
</div>

<div class="row">
<div class="col-md-12" >
{% raw %}
<span>{{unread()}} of {{mentions.length}} unread</span>
<ul class="unstyled">
<li ng-repeat="mention in mentions | filter:query | orderBy:orderProp">
<span class="mention-{{ mention.read }}">{{ mention.read ? "read" : "unread"}}</span>
<p>{{ mention.text }}</p>
</li>
</ul>
{% endraw %}
</div>
</div>
</div>
</body>
</html>

0 comments on commit a8f3405

Please sign in to comment.