Initial code commits---basic scaffolding
Getty Ritter
8 years ago
| 1 |
#!/usr/bin/env python2
|
| 2 |
|
| 3 |
import sys
|
| 4 |
import shitbird
|
| 5 |
|
| 6 |
def usage():
|
| 7 |
print USAGE
|
| 8 |
sys.exit(1)
|
| 9 |
|
| 10 |
def main():
|
| 11 |
if len(sys.argv) > 1:
|
| 12 |
if sys.argv[1] == 'init':
|
| 13 |
shitbird.init.main()
|
| 14 |
elif sys.argv[1] == 'fetch':
|
| 15 |
shitbird.fetch.main()
|
| 16 |
elif sys.argv[1] == 'serve':
|
| 17 |
shitbird.serve.main()
|
| 18 |
else:
|
| 19 |
usage()
|
| 20 |
else:
|
| 21 |
usage()
|
| 1 |
import init
|
| 2 |
import common
|
| 3 |
import fetch
|
| 4 |
import serve
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import sqlite3
|
| 4 |
import yaml
|
| 5 |
|
| 6 |
DEFAULT_SBDIR = os.path.join(os.environ['HOME'], '.shitbird')
|
| 7 |
SBDIR = os.environ.get('SBDIR', DEFAULT_SBDIR)
|
| 8 |
DB_LOCATION = os.path.join(SBDIR, 'tweets.db')
|
| 9 |
CFG_LOCATION = os.path.join(SBDIR, 'config.yaml')
|
| 10 |
|
| 11 |
try:
|
| 12 |
with open(CFG_LOCATION) as f:
|
| 13 |
CONFIG = yaml.load(f)
|
| 14 |
except:
|
| 15 |
CONFIG = {}
|
| 16 |
|
| 17 |
def row_to_dict(row):
|
| 18 |
return { 'id': row[0],
|
| 19 |
'tweet': row[1],
|
| 20 |
'date': row[2],
|
| 21 |
'name': row[3],
|
| 22 |
'handle': row[4] }
|
| 23 |
|
| 24 |
def get_tweets():
|
| 25 |
with sqlite3.connect(DB_LOCATION) as conn:
|
| 26 |
c = conn.cursor()
|
| 27 |
for row in c.execute('SELECT * FROM tweets;'):
|
| 28 |
yield row_to_dict(row)
|
| 29 |
conn.commit()
|
| 1 |
#!/usr/bin/python2
|
| 2 |
|
| 3 |
import common
|
| 4 |
import sqlite3
|
| 5 |
import tweepy
|
| 6 |
|
| 7 |
def main():
|
| 8 |
auth = tweepy.OAuthHandler(common.CONFIG['consumer_key'],
|
| 9 |
common.CONFIG['consumer_secret'])
|
| 10 |
auth.set_access_token(common.CONFIG['access_token'],
|
| 11 |
common.CONFIG['access_token_secret'])
|
| 12 |
api = tweepy.API(auth)
|
| 13 |
|
| 14 |
with sqlite3.connect(common.DB_LOCATION) as conn:
|
| 15 |
c = conn.cursor()
|
| 16 |
add_tweets(api, c)
|
| 17 |
conn.commit()
|
| 18 |
|
| 19 |
def add_tweets(api, c):
|
| 20 |
for t in api.home_timeline():
|
| 21 |
c.execute('INSERT INTO tweets VALUES (?, ?, ?, ?, ?)',
|
| 22 |
( t.id,
|
| 23 |
t.text,
|
| 24 |
t.created_at.isoformat(),
|
| 25 |
t.author.name,
|
| 26 |
t.author.screen_name
|
| 27 |
))
|
| 28 |
|
| 29 |
if __name__ == '__main__':
|
| 30 |
main()
|
| 1 |
#!/usr/bin/python2
|
| 2 |
|
| 3 |
import common
|
| 4 |
import sqlite3
|
| 5 |
import sys
|
| 6 |
|
| 7 |
SCHEMA = '''
|
| 8 |
CREATE TABLE tweets
|
| 9 |
( id integer not null
|
| 10 |
, tweet text
|
| 11 |
, date integer
|
| 12 |
, username text
|
| 13 |
, userhandle text
|
| 14 |
);
|
| 15 |
'''
|
| 16 |
|
| 17 |
def main():
|
| 18 |
with sqlite3.connect(common.DB_LOCATION) as conn:
|
| 19 |
c = conn.cursor()
|
| 20 |
c.execute(SCHEMA)
|
| 21 |
conn.commit()
|
| 22 |
|
| 23 |
if __name__ == '__main__':
|
| 24 |
main()
|
| 1 |
#!/usr/bin/python2
|
| 2 |
|
| 3 |
import common
|
| 4 |
import flask
|
| 5 |
import pystache
|
| 6 |
import sqlite3
|
| 7 |
|
| 8 |
app = flask.Flask(__name__)
|
| 9 |
|
| 10 |
MAIN = '''
|
| 11 |
<!DOCTYPE html>
|
| 12 |
<html>
|
| 13 |
<head>
|
| 14 |
<title>shitbird</title>
|
| 15 |
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8;"/>
|
| 16 |
<style type="text/css">
|
| 17 |
body {
|
| 18 |
font-family: "Helvetica", "Arial", sans-serif;
|
| 19 |
font-size: 18pt;
|
| 20 |
background-color: #ffffff;
|
| 21 |
}
|
| 22 |
.contents {
|
| 23 |
width: 80%;
|
| 24 |
margin-left: auto;
|
| 25 |
margin-right: auto;
|
| 26 |
padding: 40px;
|
| 27 |
}
|
| 28 |
.tweet {
|
| 29 |
border-top-style: solid;
|
| 30 |
border-bottom-style: solid;
|
| 31 |
border-width: 1px;
|
| 32 |
padding: 20px;
|
| 33 |
margin-top: 20px;
|
| 34 |
margin-bottom: 20px;
|
| 35 |
width: 75%;
|
| 36 |
margin-left: auto;
|
| 37 |
margin-right: auto;
|
| 38 |
}
|
| 39 |
</style>
|
| 40 |
</head>
|
| 41 |
<body>
|
| 42 |
<div class="contents">{{{body}}}</div>
|
| 43 |
</body>
|
| 44 |
</html>
|
| 45 |
'''
|
| 46 |
|
| 47 |
TWEET = '''
|
| 48 |
<div class="tweet">
|
| 49 |
<div class="text">{{tweet}}</div>
|
| 50 |
<div class="author">
|
| 51 |
<a href="http://twitter.com/{{handle}}">{{name}}</a>
|
| 52 |
</div>
|
| 53 |
<div class="link">
|
| 54 |
<a href="http://twitter.com/{{handle}}/status/{{id}}">{{date}}</a>
|
| 55 |
</div>
|
| 56 |
</div>
|
| 57 |
'''
|
| 58 |
|
| 59 |
def render_body():
|
| 60 |
return ''.join((pystache.render(TWEET, t)
|
| 61 |
for t in common.get_tweets()))
|
| 62 |
|
| 63 |
@app.route("/")
|
| 64 |
def index():
|
| 65 |
for tweet in common.get_tweets():
|
| 66 |
return pystache.render(MAIN,
|
| 67 |
{'body': render_body()})
|
| 68 |
|
| 69 |
if __name__ == '__main__':
|
| 70 |
app.run()
|