tweetcast/gevent/server/tweetcast.py
author Raphael Velt <raph.velt@gmail.com>
Wed, 30 Nov 2011 12:11:59 +0100
changeset 403 dd1686ae5506
parent 311 13702105c5ee
permissions -rwxr-xr-x
minor changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
311
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     3
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     4
from gevent import monkey; monkey.patch_all()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     5
# Importer d'abord, sinon exception
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     6
import anyjson, gevent, psycopg2
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     7
from sqlalchemy import (Boolean, Column, BigInteger, Integer, String, 
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     8
    ForeignKey, DateTime, create_engine, desc, func)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     9
from sqlalchemy.orm import backref, relationship, sessionmaker
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    10
from sqlalchemy.ext.declarative import declarative_base
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    11
from gevent.pywsgi import WSGIServer
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    12
from urlparse import parse_qs
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    13
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    14
annotation_keywords = {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    15
    "positive" : '++',
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    16
    "negative" : '--',
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    17
    "reference" : '==',
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    18
    "question" : '??'
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    19
}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    20
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    21
Base = declarative_base()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    22
engine = create_engine('postgresql://postgres:doiteshimashite@localhost/tweet_live')
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    23
Session = sessionmaker(bind=engine)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    24
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    25
class EntityType(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    26
    __tablename__ = "tweet_entity_type"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    27
    id = Column(Integer, primary_key=True, autoincrement=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    28
    label = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    29
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    30
class Entity(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    31
    __tablename__ = "tweet_entity"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    32
    id = Column(Integer, primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    33
    tweet_id = Column(BigInteger, ForeignKey('tweet_tweet.id'))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    34
    type = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    35
    entity_type_id = Column(Integer, ForeignKey('tweet_entity_type.id'), nullable=False)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    36
    entity_type = relationship("EntityType", backref="entities")
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    37
    indice_start = Column(Integer)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    38
    indice_end = Column(Integer)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    39
    source = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    40
    __mapper_args__ = {'polymorphic_on': type, 'polymorphic_identity': 'entity_entity', 'with_polymorphic':'*'}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    41
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    42
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    43
    	return {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    44
    		"indice_start" : self.indice_start,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    45
    		"indice_end" : self.indice_end,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    46
    		"type" : self.type
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    47
    	}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    48
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    49
class Tweet(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    50
    __tablename__ = 'tweet_tweet'
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    51
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    52
    id = Column(BigInteger, primary_key=True, autoincrement=False)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    53
    created_at = Column(DateTime)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    54
    text = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    55
    user_id = Column(Integer, ForeignKey('tweet_user.id'))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    56
    user = relationship("User", backref="tweets")
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    57
    entity_list = relationship(Entity, backref='tweet')
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    58
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    59
    def annotations(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    60
        aa = []
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    61
        for a in annotation_keywords:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    62
            n = self.text.count(annotation_keywords[a])
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    63
            if n:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    64
                aa.append({
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    65
                    "name" : a,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    66
                    "text" : annotation_keywords[a],
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    67
                    "count" : n
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    68
                })
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    69
        return aa
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    70
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    71
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    72
        return {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    73
            "id" : str(self.id),
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    74
            "created_at" : str(self.created_at),
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    75
            "text" : self.text,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    76
            "user" : self.user.jsondict(),
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    77
            "entities" : [en.jsondict() for en in self.entity_list],
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    78
            "annotations" : self.annotations()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    79
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    80
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    81
class User(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    82
    __tablename__ = "tweet_user"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    83
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    84
    id = Column(BigInteger, primary_key=True, autoincrement=False)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    85
    screen_name = Column(String, index=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    86
    profile_image_url = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    87
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    88
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    89
        return {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    90
            "id" : str(self.id),
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    91
            "screen_name" : self.screen_name,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    92
            "profile_image_url" : self.profile_image_url
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    93
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    94
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    95
class Hashtag(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    96
    __tablename__ = "tweet_hashtag"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    97
    id = Column(Integer, primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    98
    text = Column(String, unique=True, index=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    99
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   100
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   101
    	return {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   102
    		"text" : self.text
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   103
    	}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   104
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   105
class Url(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   106
    __tablename__ = "tweet_url"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   107
    id = Column(Integer, primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   108
    url = Column(String, unique=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   109
    expanded_url = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   110
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   111
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   112
    	return {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   113
    		"url" : self.url,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   114
    		"expanded_url" : self.expanded_url
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   115
    	}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   116
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   117
class Media(Base):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   118
    __tablename__ = "tweet_media"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   119
    id = Column(BigInteger, primary_key=True, autoincrement=False)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   120
    url = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   121
    expanded_url = Column(String)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   122
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   123
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   124
    	return {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   125
    		"url" : self.url,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   126
    		"expanded_url" : self.expanded_url
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   127
    	}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   128
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   129
class EntityHashtag(Entity):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   130
    __tablename__ = "tweet_entity_hashtag"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   131
    __mapper_args__ = {'polymorphic_identity': 'entity_hashtag'}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   132
    id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   133
    hashtag_id = Column(Integer, ForeignKey("tweet_hashtag.id"))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   134
    hashtag = relationship(Hashtag, primaryjoin=hashtag_id == Hashtag.id)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   135
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   136
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   137
    	d = super(EntityHashtag, self).jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   138
    	d['entity'] = self.hashtag.jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   139
    	return d
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   140
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   141
class EntityUrl(Entity):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   142
    __tablename__ = "tweet_entity_url"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   143
    __mapper_args__ = {'polymorphic_identity': 'entity_url'}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   144
    id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   145
    url_id = Column(Integer, ForeignKey("tweet_url.id"))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   146
    url = relationship(Url, primaryjoin=url_id == Url.id)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   147
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   148
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   149
    	d = super(EntityUrl, self).jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   150
    	d['entity'] = self.url.jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   151
    	return d
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   152
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   153
class EntityUser(Entity):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   154
    __tablename__ = "tweet_entity_user"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   155
    __mapper_args__ = {'polymorphic_identity': 'entity_user'}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   156
    id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   157
    user_id = Column(BigInteger, ForeignKey('tweet_user.id'))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   158
    user = relationship(User, primaryjoin=(user_id == User.id))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   159
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   160
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   161
    	d = super(EntityUser, self).jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   162
    	d['entity'] = self.user.jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   163
    	return d
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   164
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   165
class EntityMedia(Entity):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   166
    __tablename__ = "tweet_entity_media"
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   167
    __mapper_args__ = {'polymorphic_identity': 'entity_media'}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   168
    id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   169
    media_id = Column(BigInteger, ForeignKey('tweet_media.id'))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   170
    media = relationship(Media, primaryjoin=(media_id == Media.id))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   171
    
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   172
    def jsondict(self):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   173
    	d = super(EntityMedia, self).jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   174
    	d['entity'] = self.media.jsondict()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   175
    	return d
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   176
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   177
# ranges = []
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   178
# lastid = 0L
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   179
# 
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   180
# def define_ranges:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   181
# 	
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   182
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   183
def webserver(env, start_response):
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   184
	if env['PATH_INFO'] == '/':
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   185
		httpquery = parse_qs(env['QUERY_STRING'])
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   186
		print "serving tweets to", env['REMOTE_ADDR'], httpquery
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   187
		query = session.query(Tweet)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   188
		if "since_id" in httpquery:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   189
		    query = query.filter(Tweet.id >= long(httpquery["since_id"][0]))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   190
		if "after_id" in httpquery:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   191
		    query = query.filter(Tweet.id > long(httpquery["after_id"][0]))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   192
		if "max_id" in httpquery:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   193
		    query = query.filter(Tweet.id <= long(httpquery["max_id"][0]))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   194
		if "before_id" in httpquery:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   195
		    query = query.filter(Tweet.id < long(httpquery["before_id"][0]))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   196
		query = query.order_by(desc(Tweet.id))
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   197
		if "limit" in httpquery:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   198
			result = query[:int(httpquery["limit"][0])]
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   199
		else:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   200
			result = query[:200]
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   201
		start_response('200 OK', [('Content-Type', 'application/javascript' if "callback" in httpquery else 'application/json' )])
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   202
		return ["%s%s%s"%(
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   203
			"%s("%httpquery["callback"][0] if "callback" in httpquery else "",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   204
			anyjson.serialize({"tweets" : [t.jsondict() for t in result]}),
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   205
			")" if "callback" in httpquery else ""
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   206
		)]
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   207
	else:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   208
		start_response('404 Not Found', [('Content-Type', 'text/html')])
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   209
		return ['<h1>Not Found</h1>']
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   210
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   211
session = Session()
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   212
WSGIServer(('', 8888), webserver).serve_forever()