Skip to content

Commit

Permalink
made into a proper flask app
Browse files Browse the repository at this point in the history
  • Loading branch information
frnsys committed Jul 17, 2014
1 parent 9a92a28 commit c32b5a8
Show file tree
Hide file tree
Showing 56 changed files with 3,723 additions and 146 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory": "app/static/js/vendor/bower"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ config.py
bower/
index.css
config.py
data/
db/
14 changes: 13 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask
from flask.ext.mongoengine import MongoEngine
from flask.ext.assets import Environment, Bundle

app = Flask(__name__, static_folder='static', static_url_path='')

Expand All @@ -9,4 +10,15 @@
# Setup the database.
db = MongoEngine(app)

from app import notify
# Assets
assets = Environment()
css = Bundle('css/index.sass', filters='sass', depends=['css/**/*.sass', 'css/**/**/*.sass'], output='css/index.css')
assets.register('css_all', css)
assets.init_app(app)

# So we can use Jade templates.
app.jinja_env.add_extension('pyjade.ext.jinja.PyJadeExtension')

# Register blueprints
from app import routes
app.register_blueprint(routes.clones.bp)
6 changes: 6 additions & 0 deletions app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from flask_wtf import Form
from wtforms import TextField
from wtforms.validators import Required

class CloneForm(Form):
username = TextField('Twitter Username', validators=[Required()])
90 changes: 62 additions & 28 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,76 @@
import datetime
from app import db
import random
import re
import config

class Verb(db.Document):
from textblob import TextBlob
from app import db, twitter

class Clone(db.Document):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
body = db.StringField(required=True, unique=True)
username = db.StringField(required=True, unique=True)
patterns = db.ListField(db.StringField(), required=True)
vocabulary = db.DictField(required=True)

meta = {
'allow_inheritance': True,
'indexes': ['-created_at'],
'indexes': ['-created_at', 'username'],
'ordering': ['-created_at']
}

class Noun(db.Document):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
body = db.StringField(required=True, unique=True)
def imprint(self, username):
"""
Generate a clone for a given Twitter user.
"""
self.username = username
user_tweets = twitter.tweets(username, count=2000)

meta = {
'allow_inheritance': True,
'indexes': ['-created_at'],
'ordering': ['-created_at']
}
# { POS tag: words }
self.vocabulary = {}

class Adjective(db.Document):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
body = db.StringField(required=True, unique=True)
# Mad-lib Tweet patterns.
self.patterns = []

meta = {
'allow_inheritance': True,
'indexes': ['-created_at'],
'ordering': ['-created_at']
}
for tweet in user_tweets:
text = tweet['body']
if '@' not in text: # Trying without any @mentions.

class Adverb(db.Document):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
body = db.StringField(required=True, unique=True)
# Remove urls and @mentions
text = re.sub(r'(?:\@|https?\://)\S+', '', text)
pattern = text

meta = {
'allow_inheritance': True,
'indexes': ['-created_at'],
'ordering': ['-created_at']
}
# Extract parts of speech.
for t in TextBlob(text).pos_tags:
token = t[0]
tag = t[1]

# Preserve hashtags.
# Skip untagged tokens.
# Skip tokens which are too short.
if token[0] == '#' or tag == '-NONE-' or len(token) <= 2:
continue

if tag in config.ELIGIBLE_TAGS:
# Build the pattern.
pattern = pattern.replace(token, '{{{{ {0} }}}}'.format(tag))

# Add new tokens to the vocabulary.
if tag not in self.vocabulary:
self.vocabulary[tag] = []
self.vocabulary[tag].append(token.lower())

self.patterns.append(pattern)

def speak(self):
pattern = random.choice(self.patterns)
tweet = pattern

# Extract the tags to be replaced.
p = re.compile(r'\{\{\s*([A-Za-z]+)\s*\}\}')
tags = p.findall(pattern)

# Replace the tags with selections from the vocabulary.
for tag in tags:
token = random.choice(self.vocabulary[tag])
tweet = re.sub(r'(\{\{\s*' + re.escape(tag) + r'\s*\}\})', token, tweet, 1)
return tweet
1 change: 1 addition & 0 deletions app/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import clones
40 changes: 40 additions & 0 deletions app/routes/clones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from flask import Blueprint, request, render_template, redirect, url_for, flash
from app.models import Clone
from app.forms import CloneForm
import re

bp = Blueprint('clones', __name__, url_prefix = '/clones')

@bp.route('/<string:username>')
def clone(username):
clone = Clone.objects.get_or_404(username=username)
return render_template('clones/member.jade', clone=clone)

@bp.route('/', methods=['GET', 'POST'])
def clones():
form = CloneForm()
if form.validate_on_submit():
username = form.username.data
if not Clone.objects.get(username):
clone = Clone()
clone.imprint(username)
clone.save()
return redirect(url_for('clones.clone', username=username))
return render_template('clones/create.jade', form=form)


@bp.app_template_filter()
def highlight_pattern(pattern):
"""
A template filter for highlighting
fill-in spots in patterns.
Example usage (in `pyjade`)::
div= pattern|highlight_pattern
"""
p = re.compile(r'\{\{\s*([A-Za-z]+)\s*\}\}')
tags = p.findall(pattern)
for tag in tags:
pattern = re.sub(r'(\{\{\s*' + re.escape(tag) + r'\s*\}\})', '<span style="color:#ddd;">'+tag+'</span>', pattern, 1)
return pattern
7 changes: 7 additions & 0 deletions app/static/css/core/_core.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ========================================== CORE
@import "normalize"
@import "h5bp"
@import "mixins"
@import "type"
@import "lists"
@import "images"
Loading

0 comments on commit c32b5a8

Please sign in to comment.