script/lib/iri_tweet/models.py
changeset 254 2209e66bb50b
parent 253 e9335ee3cf71
child 255 500cd0405c7a
--- a/script/lib/iri_tweet/models.py	Tue Aug 09 13:07:23 2011 +0200
+++ b/script/lib/iri_tweet/models.py	Fri Aug 12 18:17:27 2011 +0200
@@ -27,31 +27,38 @@
     else:
         return anyjson.serialize(obj)
 
+class EntityType(Base):
+    __tablename__ = "tweet_entity_type"
+    id = Column(Integer, primary_key=True, autoincrement=True)
+    label = Column(String)
+
 class Entity(Base):
     __tablename__ = "tweet_entity"
-    id = Column(Integer, primary_key = True)
+    id = Column(Integer, primary_key=True)
     tweet_id = Column(BigInteger, ForeignKey('tweet_tweet.id'))
-    #tweet = relationship(Tweet, primaryjoin = tweet_id == Tweet.id)
     type = Column(String)
+    entity_type_id = Column(Integer, ForeignKey('tweet_entity_type.id'), nullable=False)
+    entity_type = relationship("EntityType", backref="entities")
     indice_start = Column(Integer)
     indice_end = Column(Integer)
-    __mapper_args__ = {'polymorphic_on': type}
+    source = Column(String)
+    __mapper_args__ = {'polymorphic_on': type, 'polymorphic_identity': 'entity_entity', 'with_polymorphic':'*'}
 
     def __init__(self, **kwargs):
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
 
 class TweetSource(Base):
     __tablename__ = 'tweet_tweet_source'
-    id = Column(Integer, primary_key = True, autoincrement=True)
+    id = Column(Integer, primary_key=True, autoincrement=True)
     original_json = Column(String)
-    received_at = Column(DateTime, default=datetime.datetime.now())
+    received_at = Column(DateTime, default=datetime.datetime.now(), index=True)
     
     def __init__(self, **kwargs):
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
 
 
 class TweetLog(Base):
@@ -62,7 +69,7 @@
     }
     
     __tablename__ = 'tweet_tweet_log'
-    id = Column(Integer, primary_key = True, autoincrement=True)
+    id = Column(Integer, primary_key=True, autoincrement=True)
     tweet_source_id = Column(Integer, ForeignKey('tweet_tweet_source.id'))
     tweet_source = relationship("TweetSource", backref="logs")
     status = Column(Integer)
@@ -76,17 +83,17 @@
     id = Column(BigInteger, primary_key=True, autoincrement=False)
     id_str = Column(String)
     contributors = Column(String)
-    coordinates =  Column(String) 
+    coordinates = Column(String) 
     created_at = Column(DateTime)
     favorited = Column(Boolean)
     geo = Column(String)
     in_reply_to_screen_name = Column(String)
     in_reply_to_status_id = Column(BigInteger) 
     in_reply_to_status_id_str = Column(String)
-    in_reply_to_user_id = Column(Integer)
+    in_reply_to_user_id = Column(BigInteger)
     in_reply_to_user_id_str = Column(String)
     place = Column(String)
-    retweet_count = Column(Integer)
+    retweet_count = Column(String)
     retweeted = Column(Boolean)
     source = Column(String)
     text = Column(String)
@@ -96,17 +103,17 @@
     tweet_source_id = Column(Integer, ForeignKey('tweet_tweet_source.id'))
     tweet_source = relationship("TweetSource", backref="tweet")
     entity_list = relationship(Entity, backref='tweet')
-    received_at = Column(DateTime, default=datetime.datetime.now())
+    received_at = Column(DateTime, default=datetime.datetime.now(), index=True)
         
     def __init__(self, **kwargs):
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
 
 class UserMessage(Base):
     __tablename__ = "tweet_user_message"
 
-    id = Column(Integer, primary_key = True)
+    id = Column(Integer, primary_key=True)
     user_id = Column(Integer, ForeignKey('tweet_user.id'))
     user = relationship("User", backref="messages")
     created_at = Column(DateTime, default=datetime.datetime.now())
@@ -115,7 +122,7 @@
 class Message(Base):
     __tablename__ = "tweet_message"
     
-    id = Column(Integer, primary_key = True)
+    id = Column(Integer, primary_key=True)
     created_at = Column(DateTime, default=datetime.datetime.now())
     text = Column(String)
     users = relationship(UserMessage, backref='message')
@@ -124,57 +131,55 @@
 class User(Base):
     __tablename__ = "tweet_user"
     
-    id = Column(Integer, primary_key = True, autoincrement=False)
-    id_str= Column(String)
-    contributors_enabled= Column(Boolean)
-    created_at= Column(DateTime)
-    description= Column(String)
+    id = Column(BigInteger, primary_key=True, autoincrement=False)
+    id_str = Column(String)
+    contributors_enabled = Column(Boolean)
+    created_at = Column(DateTime)
+    description = Column(String)
     favourites_count = Column(Integer)
     follow_request_sent = Column(Boolean)
     followers_count = Column(Integer)
     following = Column(String)
     friends_count = Column(Integer)
-    geo_enabled= Column(Boolean)
-    is_translator= Column(Boolean)
+    geo_enabled = Column(Boolean)
+    is_translator = Column(Boolean)
     lang = Column(String)
     listed_count = Column(Integer)
-    location= Column(String)
+    location = Column(String)
     name = Column(String)
     notifications = Column(String)
-    profile_background_color= Column(String)
-    profile_background_image_url= Column(String)
-    profile_background_tile= Column(Boolean)
-    profile_image_url= Column(String)
-    profile_link_color= Column(String)
-    profile_sidebar_border_color= Column(String)
-    profile_sidebar_fill_color= Column(String)
-    profile_text_color= Column(String)
-    profile_use_background_image= Column(Boolean)
-    protected= Column(Boolean)
-    screen_name= Column(String, index=True, unique=True)
-    show_all_inline_media= Column(Boolean)
+    profile_background_color = Column(String)
+    profile_background_image_url = Column(String)
+    profile_background_tile = Column(Boolean)
+    profile_image_url = Column(String)
+    profile_link_color = Column(String)
+    profile_sidebar_border_color = Column(String)
+    profile_sidebar_fill_color = Column(String)
+    profile_text_color = Column(String)
+    profile_use_background_image = Column(Boolean)
+    protected = Column(Boolean)
+    screen_name = Column(String, index=True, unique=True)
+    show_all_inline_media = Column(Boolean)
     statuses_count = Column(Integer)
-    time_zone= Column(String)
-    url= Column(String)
+    time_zone = Column(String)
+    url = Column(String)
     utc_offset = Column(Integer)
-    verified= Column(Boolean)
+    verified = Column(Boolean)
 
     def __init__(self, **kwargs):
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)    
+            if hasattr(self, key):
+                setattr(self, key, value)    
     
 
 class Hashtag(Base):
     __tablename__ = "tweet_hashtag"
     id = Column(Integer, primary_key=True)
-    text = Column(String, unique = True, index = True)
+    text = Column(String, unique=True, index=True)
     def __init__(self, **kwargs):
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
-
-
+            if hasattr(self, key):
+                setattr(self, key, value)
 
 class Url(Base):
     __tablename__ = "tweet_url"
@@ -183,8 +188,35 @@
     expanded_url = Column(String)
     def __init__(self, **kwargs):
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
+
+class MediaType(Base):
+    __tablename__ = "tweet_media_type"
+    id = Column(Integer, primary_key=True, autoincrement=True)
+    label = Column(String, unique=True, index=True)
+    def __init__(self, **kwargs):
+        for key, value in kwargs.items():
+            if hasattr(self, key):
+                setattr(self, key, value)
+    
+
+class Media(Base):
+    __tablename__ = "tweet_media"
+    id = Column(BigInteger, primary_key=True, autoincrement=False)
+    id_str = Column(String, unique=True)
+    media_url = Column(String, unique=True)
+    media_url_https = Column(String, unique=True)
+    url = Column(String)
+    display_url = Column(String)
+    expanded_url = Column(String)
+    sizes = Column(String)
+    type_id = Column(Integer, ForeignKey("tweet_media_type.id"))
+    type = relationship(MediaType, primaryjoin=type_id == MediaType.id)
+    def __init__(self, **kwargs):
+        for key, value in kwargs.items():
+            if hasattr(self, key):
+                setattr(self, key, value)
 
     
 
@@ -197,8 +229,8 @@
     def __init__(self, **kwargs):
         super(EntityHashtag, self).__init__(**kwargs)
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
 
     
 class EntityUrl(Entity):
@@ -210,22 +242,35 @@
     def __init__(self, **kwargs):
         super(EntityUrl, self).__init__(**kwargs)
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
 
 class EntityUser(Entity):
     __tablename__ = "tweet_entity_user"
     __mapper_args__ = {'polymorphic_identity': 'entity_user'}
     id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
-    user_id = Column(Integer, ForeignKey('tweet_user.id'))
-    user = relationship(User, primaryjoin=user_id == User.id)
+    user_id = Column(BigInteger, ForeignKey('tweet_user.id'))
+    user = relationship(User, primaryjoin=(user_id == User.id))
 
     def __init__(self, **kwargs):
         super(EntityUser, self).__init__(**kwargs)
         for key, value in kwargs.items():
-            if hasattr(self,key):
-                setattr(self,key,value)
+            if hasattr(self, key):
+                setattr(self, key, value)
                 
+class EntityMedia(Entity):
+    __tablename__ = "tweet_entity_media"
+    __mapper_args__ = {'polymorphic_identity': 'entity_media'}
+    id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
+    media_id = Column(BigInteger, ForeignKey('tweet_media.id'))
+    media = relationship(Media, primaryjoin=(media_id == Media.id))
+
+    def __init__(self, **kwargs):
+        super(EntityMedia, self).__init__(**kwargs)
+        for key, value in kwargs.items():
+            if hasattr(self, key):
+                setattr(self, key, value)
+
                 
 def setup_database(*args, **kwargs):
         
@@ -263,15 +308,15 @@
 
 tweet_tweet = {
     'contributors': None,
-    'coordinates': None, 
-    'created_at': 'date', 
-    'entities': "tweet_entity", 
+    'coordinates': None,
+    'created_at': 'date',
+    'entities': "tweet_entity",
     'favorited': "bool",
     'geo': None,
     'id': "long",
     'id_str': "string",
-    'in_reply_to_screen_name': "string", 
-    'in_reply_to_status_id': "long", 
+    'in_reply_to_screen_name': "string",
+    'in_reply_to_status_id': "long",
     'in_reply_to_status_id_str': "string",
     'in_reply_to_user_id': "int",
     'in_reply_to_user_id_str': "string",
@@ -354,6 +399,6 @@
 
 tweet_url = {
     "url": "string",
-    "expanded_url" : "string",    
+    "expanded_url" : "string",
 }