| author | Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com> |
| Tue, 20 Dec 2011 15:03:38 +0100 | |
| changeset 460 | bc52ce9ab0c2 |
| parent 451 | c223cae8db5b |
| child 462 | 663f82cc659b |
| permissions | -rwxr-xr-x |
|
404
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
3 |
|
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
4 |
from gevent import monkey; monkey.patch_all() |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
5 |
# Importer d'abord, sinon exception |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
6 |
import anyjson, gevent, psycopg2 |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
7 |
from sqlalchemy import (Boolean, Column, BigInteger, Integer, String, |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
8 |
ForeignKey, DateTime, create_engine, asc, func) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
9 |
from sqlalchemy.orm import backref, relationship, sessionmaker, joinedload |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
10 |
from sqlalchemy.ext.declarative import declarative_base |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
11 |
from gevent.pywsgi import WSGIServer |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
12 |
from urlparse import parse_qs |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
13 |
import datetime |
|
460
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
14 |
from server_setup import SQL_CONNECT, LISTENER |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
15 |
import os |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
16 |
import _socket |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
17 |
import pwd |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
18 |
from gevent.server import StreamServer |
|
404
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
19 |
|
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
20 |
Base = declarative_base() |
|
405
6626b728b142
Added configuration file to Tweetcast Gevent Server
Raphael Velt <raph.velt@gmail.com>
parents:
404
diff
changeset
|
21 |
engine = create_engine(SQL_CONNECT) |
|
404
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
22 |
Session = sessionmaker(bind=engine) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
23 |
|
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
24 |
class TweetSource(Base): |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
25 |
__tablename__ = 'tweet_tweet_source' |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
26 |
id = Column(Integer, primary_key=True, autoincrement=True) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
27 |
original_json = Column(String) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
28 |
received_at = Column(DateTime, default=datetime.datetime.utcnow, index=True) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
29 |
|
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
30 |
class Tweet(Base): |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
31 |
__tablename__ = 'tweet_tweet' |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
32 |
id = Column(BigInteger, primary_key=True, autoincrement=False) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
33 |
tweet_source_id = Column(Integer, ForeignKey('tweet_tweet_source.id')) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
34 |
tweet_source = relationship("TweetSource", backref="tweet") |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
35 |
def jsondict(self): |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
36 |
tweetdict = anyjson.deserialize(self.tweet_source.original_json) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
37 |
keys_to_delete = [ |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
38 |
'in_reply_to_screen_name', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
39 |
'in_reply_to_user_id', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
40 |
'retweeted', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
41 |
'place', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
42 |
'geo', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
43 |
'source', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
44 |
'contributors', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
45 |
'coordinates', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
46 |
'retweet_count', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
47 |
'favorited', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
48 |
'truncated', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
49 |
'possibly_sensitive' |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
50 |
] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
51 |
user_keys_to_delete = [ |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
52 |
'default_profile_image', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
53 |
'show_all_inline_media', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
54 |
'contributors_enabled', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
55 |
'profile_sidebar_fill_color', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
56 |
'created_at', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
57 |
'lang', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
58 |
'time_zone', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
59 |
'profile_sidebar_border_color', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
60 |
'follow_request_sent', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
61 |
'profile_background_image_url', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
62 |
'profile_background_image_url_https', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
63 |
'followers_count', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
64 |
'description', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
65 |
'url', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
66 |
'geo_enabled', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
67 |
'profile_use_background_image', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
68 |
'default_profile', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
69 |
'following', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
70 |
'profile_text_color', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
71 |
'is_translator', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
72 |
'favourites_count', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
73 |
'listed_count', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
74 |
'friends_count', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
75 |
'profile_link_color', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
76 |
'protected', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
77 |
'location', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
78 |
'notifications', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
79 |
'profile_image_url_https', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
80 |
'statuses_count', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
81 |
'verified', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
82 |
'profile_background_color', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
83 |
'profile_background_tile', |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
84 |
'utc_offset' |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
85 |
] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
86 |
|
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
87 |
def textids(dictionary): |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
88 |
idfields = [key for key in dictionary if key[-2:] == 'id'] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
89 |
for key in idfields: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
90 |
keystr = key + '_str' |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
91 |
if keystr in dictionary: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
92 |
dictionary[key] = dictionary[keystr] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
93 |
del dictionary[keystr] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
94 |
|
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
95 |
for key in keys_to_delete: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
96 |
if key in tweetdict: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
97 |
del tweetdict[key] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
98 |
for key in user_keys_to_delete: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
99 |
if key in tweetdict['user']: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
100 |
del tweetdict['user'][key] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
101 |
textids(tweetdict) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
102 |
textids(tweetdict['user']) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
103 |
if 'retweeted_status' in tweetdict: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
104 |
for key in keys_to_delete: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
105 |
if key in tweetdict['retweeted_status']: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
106 |
del tweetdict['retweeted_status'][key] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
107 |
for key in user_keys_to_delete: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
108 |
if key in tweetdict['retweeted_status']['user']: |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
109 |
del tweetdict['retweeted_status']['user'][key] |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
110 |
textids(tweetdict['retweeted_status']) |
|
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
111 |
return tweetdict |
| 438 | 112 |
|
|
460
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
113 |
|
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
114 |
def bind_unix_listener(path, backlog=50, user=None): |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
115 |
pid = os.getpid() |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
116 |
tempname = '%s.%s.tmp' % (path, pid) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
117 |
backname = '%s.%s.bak' % (path, pid) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
118 |
unlink(tempname) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
119 |
unlink(backname) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
120 |
link(path, backname) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
121 |
try: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
122 |
sock = _socket.socket(_socket.AF_UNIX, _socket.SOCK_STREAM) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
123 |
sock.setblocking(0) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
124 |
sock.bind(tempname) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
125 |
try: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
126 |
if user is not None: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
127 |
user = pwd.getpwnam(user) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
128 |
os.chown(tempname, user.pw_uid, user.pw_gid) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
129 |
os.chmod(tempname, 0600) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
130 |
sock.listen(backlog) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
131 |
try: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
132 |
os.rename(tempname, path) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
133 |
except: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
134 |
os.rename(backname, path) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
135 |
backname = None |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
136 |
raise |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
137 |
tempname = None |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
138 |
return sock |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
139 |
finally: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
140 |
if tempname is not None: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
141 |
unlink(tempname) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
142 |
finally: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
143 |
if backname is not None: |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
144 |
unlink(backname) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
145 |
|
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
146 |
class Tweetcast(object): |
| 438 | 147 |
|
|
460
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
148 |
def __init__(self): |
| 450 | 149 |
print "Starting server" |
150 |
self.data = [] |
|
151 |
self.lastid = 0L |
|
152 |
self.session = Session() |
|
|
460
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
153 |
self.refresh() |
| 450 | 154 |
|
155 |
def refresh(self): |
|
156 |
print "refreshing" |
|
157 |
query = self.session.query(Tweet).order_by(asc(Tweet.id)).options(joinedload(Tweet.tweet_source)).filter(Tweet.id > self.lastid) |
|
158 |
for tweet in query: |
|
159 |
self.lastid = tweet.id |
|
160 |
self.data.append(anyjson.serialize(tweet.jsondict())) |
|
161 |
print len(self.data), "tweets in memory" |
|
162 |
self.lastrefresh = datetime.datetime.now() |
|
|
404
89968844eb7d
Tweetcast: heavy refactoring !
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
163 |
|
| 450 | 164 |
def webserver(self, env, start_response): |
165 |
if (datetime.datetime.now() - self.lastrefresh).seconds > 10: |
|
166 |
self.refresh() |
|
167 |
if env['PATH_INFO'] == '/': |
|
168 |
httpquery = parse_qs(env['QUERY_STRING']) |
|
169 |
print "serving tweets to", env['REMOTE_ADDR'], httpquery |
|
170 |
frompos = 0 |
|
171 |
if "from" in httpquery: |
|
172 |
frompos = int(httpquery["from"][0]) |
|
173 |
result = '%s{"tweets" : [ %s ] }%s'%( |
|
174 |
"%s("%httpquery["callback"][0] if "callback" in httpquery else "", |
|
175 |
",".join(self.data[frompos:]), |
|
176 |
")" if "callback" in httpquery else "" |
|
177 |
) |
|
178 |
print "Sending response" |
|
179 |
start_response('200 OK', [('Content-Type', 'application/javascript' if "callback" in httpquery else 'application/json' )]) |
|
180 |
return [result] |
|
181 |
else: |
|
182 |
start_response('404 Not Found', [('Content-Type', 'text/html')]) |
|
183 |
return ['<h1>Not Found</h1>'] |
|
|
460
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
184 |
|
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
185 |
def startserver(listener): |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
186 |
|
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
187 |
if isinstance(listener, str): |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
188 |
listener = bind_unix_listener(listener) |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
189 |
|
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
190 |
WSGIServer(listener, self.webserver).serve_forever() |
|
425
b346fd32fc34
prepare for publication, add sync info
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
405
diff
changeset
|
191 |
|
|
b346fd32fc34
prepare for publication, add sync info
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
405
diff
changeset
|
192 |
if __name__ == "__main__": |
|
460
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
193 |
tc = Tweetcast() |
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
194 |
|
|
bc52ce9ab0c2
small code reorganisation.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
451
diff
changeset
|
195 |
ts.startserver(LISTENER) |