script/iri_tweet/models.py
changeset 9 bb44692e09ee
child 11 54d7f1486ac4
equal deleted inserted replaced
8:b7f4b0554ef8 9:bb44692e09ee
       
     1 from sqlalchemy import Boolean, Table, Column, BigInteger, \
       
     2     Integer, String, MetaData, ForeignKey, DateTime
       
     3 from sqlalchemy.ext.declarative import declarative_base
       
     4 from sqlalchemy.orm import relationship, backref, sessionmaker
       
     5 import datetime
       
     6 import email.utils
       
     7 import simplejson
       
     8 
       
     9 
       
    10 Base = declarative_base()
       
    11 
       
    12 CONSUMER_KEY = "54ThDZhpEjokcMgHJOMnQA"
       
    13 CONSUMER_SECRET = "wUoL9UL2T87tfc97R0Dff2EaqRzpJ5XGdmaN2XK3udA"
       
    14 ACCESS_TOKEN_KEY= "47312923-LiNTtz0I18YXMVIrFeTuhmH7bOvYsK6p3Ln2Dc"
       
    15 ACCESS_TOKEN_SECRET = "r3LoXVcjImNAElUpWqTu2SG2xCdWFHkva7xeQoncA"
       
    16 
       
    17 def adapt_date(date_str):
       
    18     ts = email.utils.parsedate_tz(date_str)
       
    19     return datetime.datetime(*ts[0:7])
       
    20 
       
    21 def adapt_json(obj):
       
    22     if obj is None:
       
    23         return None
       
    24     else:
       
    25         return simplejson.dumps(obj)
       
    26 
       
    27 class Entity(Base):
       
    28     __tablename__ = "tweet_entity"
       
    29     id = Column(Integer, primary_key = True)
       
    30     tweet_id = Column(BigInteger, ForeignKey('tweet_tweet.id'))
       
    31     #tweet = relationship(Tweet, primaryjoin = tweet_id == Tweet.id)
       
    32     type = Column(String)
       
    33     indice_start = Column(Integer)
       
    34     indice_end = Column(Integer)
       
    35     __mapper_args__ = {'polymorphic_on': type}
       
    36 
       
    37     def __init__(self, **kwargs):
       
    38         for key, value in kwargs.items():
       
    39             if hasattr(self,key):
       
    40                 setattr(self,key,value)
       
    41 
       
    42 
       
    43 class Tweet(Base):
       
    44     __tablename__ = 'tweet_tweet'
       
    45 
       
    46     id = Column(BigInteger, primary_key=True, autoincrement=False)
       
    47     id_str = Column(String)
       
    48     contributors = Column(String)
       
    49     coordinates =  Column(String) 
       
    50     created_at = Column(DateTime)
       
    51     favorited = Column(Boolean)
       
    52     geo = Column(String)
       
    53     in_reply_to_screen_name = Column(String)
       
    54     in_reply_to_status_id = Column(BigInteger) 
       
    55     in_reply_to_status_id_str = Column(String)
       
    56     in_reply_to_user_id = Column(Integer)
       
    57     in_reply_to_user_id_str = Column(String)
       
    58     place = Column(String)
       
    59     retweet_count = Column(Integer)
       
    60     retweeted = Column(Boolean)
       
    61     source = Column(String)
       
    62     text = Column(String)
       
    63     truncated = Column(Boolean)
       
    64     user_id = Column(Integer, ForeignKey('tweet_user.id'))
       
    65     original_json = Column(String)
       
    66     entity_list = relationship(Entity, backref='tweet')
       
    67     
       
    68     #user = relationship(User, primaryjoin=user_id == User.id)
       
    69     
       
    70     def __init__(self, **kwargs):
       
    71         for key, value in kwargs.items():
       
    72             if hasattr(self,key):
       
    73                 setattr(self,key,value)
       
    74     
       
    75 
       
    76 class User(Base):
       
    77     __tablename__ = "tweet_user"
       
    78     
       
    79     id = Column(Integer, primary_key = True, autoincrement=False)
       
    80     id_str= Column(String)
       
    81     contributors_enabled= Column(Boolean)
       
    82     created_at= Column(DateTime)
       
    83     description= Column(String)
       
    84     favourites_count = Column(Integer)
       
    85     follow_request_sent = Column(Boolean)
       
    86     followers_count = Column(Integer)
       
    87     following = Column(String)
       
    88     friends_count = Column(Integer)
       
    89     geo_enabled= Column(Boolean)
       
    90     is_translator= Column(Boolean)
       
    91     lang = Column(String)
       
    92     listed_count = Column(Integer)
       
    93     location= Column(String)
       
    94     name = Column(String)
       
    95     notifications = Column(String)
       
    96     profile_background_color= Column(String)
       
    97     profile_background_image_url= Column(String)
       
    98     profile_background_tile= Column(Boolean)
       
    99     profile_image_url= Column(String)
       
   100     profile_link_color= Column(String)
       
   101     profile_sidebar_border_color= Column(String)
       
   102     profile_sidebar_fill_color= Column(String)
       
   103     profile_text_color= Column(String)
       
   104     profile_use_background_image= Column(Boolean)
       
   105     protected= Column(Boolean)
       
   106     screen_name= Column(String)
       
   107     show_all_inline_media= Column(Boolean)
       
   108     statuses_count = Column(Integer)
       
   109     time_zone= Column(String)
       
   110     url= Column(String)
       
   111     utc_offset = Column(Integer)
       
   112     verified= Column(Boolean)
       
   113     tweets = relationship(Tweet, backref='user')
       
   114 
       
   115     def __init__(self, **kwargs):
       
   116         for key, value in kwargs.items():
       
   117             if hasattr(self,key):
       
   118                 setattr(self,key,value)
       
   119 
       
   120     
       
   121 
       
   122 class Hashtag(Base):
       
   123     __tablename__ = "tweet_hashtag"
       
   124     id = Column(Integer, primary_key=True)
       
   125     text = Column(String, unique = True)
       
   126     def __init__(self, **kwargs):
       
   127         for key, value in kwargs.items():
       
   128             if hasattr(self,key):
       
   129                 setattr(self,key,value)
       
   130 
       
   131 
       
   132 
       
   133 class Url(Base):
       
   134     __tablename__ = "tweet_url"
       
   135     id = Column(Integer, primary_key=True)
       
   136     url = Column(String, unique=True)
       
   137     expanded_url = Column(String)
       
   138     def __init__(self, **kwargs):
       
   139         for key, value in kwargs.items():
       
   140             if hasattr(self,key):
       
   141                 setattr(self,key,value)
       
   142 
       
   143     
       
   144 
       
   145 class EntityHashtag(Entity):
       
   146     __tablename__ = "tweet_entity_hashtag"
       
   147     __mapper_args__ = {'polymorphic_identity': 'entity_hashtag'}
       
   148     id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
       
   149     hashtag_id = Column(Integer, ForeignKey("tweet_hashtag.id"))
       
   150     hashtag = relationship(Hashtag, primaryjoin=hashtag_id == Hashtag.id)
       
   151     def __init__(self, **kwargs):
       
   152         super(EntityHashtag, self).__init__(**kwargs)
       
   153         for key, value in kwargs.items():
       
   154             if hasattr(self,key):
       
   155                 setattr(self,key,value)
       
   156 
       
   157     
       
   158 class EntityUrl(Entity):
       
   159     __tablename__ = "tweet_entity_url"
       
   160     __mapper_args__ = {'polymorphic_identity': 'entity_url'}
       
   161     id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
       
   162     url_id = Column(Integer, ForeignKey("tweet_url.id"))
       
   163     url = relationship(Url, primaryjoin=url_id == Url.id)
       
   164     def __init__(self, **kwargs):
       
   165         super(EntityUrl, self).__init__(**kwargs)
       
   166         for key, value in kwargs.items():
       
   167             if hasattr(self,key):
       
   168                 setattr(self,key,value)
       
   169 
       
   170 class EntityUser(Entity):
       
   171     __tablename__ = "tweet_entity_user"
       
   172     __mapper_args__ = {'polymorphic_identity': 'entity_user'}
       
   173     id = Column(Integer, ForeignKey('tweet_entity.id'), primary_key=True)
       
   174     user_id = Column(Integer, ForeignKey('tweet_user.id'))
       
   175     user = relationship(User, primaryjoin=user_id == User.id)
       
   176 
       
   177     def __init__(self, **kwargs):
       
   178         super(EntityUser, self).__init__(**kwargs)
       
   179         for key, value in kwargs.items():
       
   180             if hasattr(self,key):
       
   181                 setattr(self,key,value)
       
   182 
       
   183 rest_tweet_tweet = {
       
   184     u'iso_language_code': 'unicode',
       
   185     u'text': 'unicode',
       
   186     u'from_user_id_str': 'unicode',
       
   187     u'profile_image_url': 'unicode',
       
   188     u'to_user_id_str': 'NoneType',
       
   189     u'created_at': 'unicode',
       
   190     u'source': 'unicode',
       
   191     u'to_user': 'unicode',
       
   192     u'id_str': 'unicode',
       
   193     u'from_user': 'unicode',
       
   194     u'place': {u'type': 'unicode', u'id': 'unicode', u'full_name': 'unicode'},
       
   195     u'from_user_id': 'int',
       
   196     u'to_user_id': 'NoneType',
       
   197     u'geo': 'NoneType',
       
   198     u'id': 'int',
       
   199     u'metadata': {u'result_type': 'unicode'}
       
   200 }
       
   201 
       
   202 tweet_tweet = {
       
   203     'contributors': None,
       
   204     'coordinates': None, 
       
   205     'created_at': 'date', 
       
   206     'entities': "tweet_entity", 
       
   207     'favorited': "bool",
       
   208     'geo': None,
       
   209     'id': "long",
       
   210     'id_str': "string",
       
   211     'in_reply_to_screen_name': "string", 
       
   212     'in_reply_to_status_id': "long", 
       
   213     'in_reply_to_status_id_str': "string",
       
   214     'in_reply_to_user_id': "int",
       
   215     'in_reply_to_user_id_str': "string",
       
   216     'place': "string",
       
   217     'retweet_count': "int",
       
   218     'retweeted': "bool",
       
   219     'source': "string",
       
   220     'text': "string",
       
   221     'truncated': "bool",
       
   222     'user': "tweet_user"
       
   223 }
       
   224 tweet_user = {
       
   225     'contributors_enabled': 'bool',
       
   226     'created_at': 'str',
       
   227     'description': 'str',
       
   228     'favourites_count': 'int',
       
   229     'follow_request_sent': None,
       
   230     'followers_count': 'int',
       
   231     'following': None,
       
   232     'friends_count': 'int',
       
   233     'geo_enabled': 'bool',
       
   234     'id': 'int',
       
   235     'id_str': 'str',
       
   236     'is_translator': 'bool',
       
   237     'lang': 'str',
       
   238     'listed_count': 'int',
       
   239     'location': 'str',
       
   240     'name': 'str',
       
   241     'notifications': 'NoneType',
       
   242     'profile_background_color': 'str',
       
   243     'profile_background_image_url': 'str',
       
   244     'profile_background_tile': 'bool',
       
   245     'profile_image_url': 'str',
       
   246     'profile_link_color': 'str',
       
   247     'profile_sidebar_border_color': 'str',
       
   248     'profile_sidebar_fill_color': 'str',
       
   249     'profile_text_color': 'str',
       
   250     'profile_use_background_image': 'bool',
       
   251     'protected': 'bool',
       
   252     'screen_name': 'str',
       
   253     'show_all_inline_media': 'bool',
       
   254     'statuses_count': 'int',
       
   255     'time_zone': 'str',
       
   256     'url': 'str',
       
   257     'utc_offset': 'int',
       
   258     'verified': 'bool',
       
   259 }
       
   260 
       
   261 
       
   262 tweet_entity_hashtag = {
       
   263     'hashtag' : 'tweet_hashtag',
       
   264     'indice_start' : 'int',
       
   265     'indice_end' : 'int',
       
   266     'tweet':'tweet_tweet'
       
   267 }
       
   268 
       
   269 tweet_entity_url = {
       
   270     'url' : 'tweet_url',
       
   271     'indice_start' : 'int',
       
   272     'indice_end' : 'int',
       
   273     'tweet':'tweet_tweet'
       
   274 }
       
   275 
       
   276 tweet_entity_user = {
       
   277     'user' : 'tweet_user',
       
   278     'indice_start' : 'int',
       
   279     'indice_end' : 'int',
       
   280     'tweet':'tweet_tweet'
       
   281 }
       
   282 
       
   283 #id int
       
   284 #id_str str
       
   285 #indices list
       
   286 #name str
       
   287 #screen_name str
       
   288 
       
   289 tweet_hashtag = {
       
   290     "text": "string"
       
   291 }
       
   292 
       
   293 tweet_url = {
       
   294     "url": "string",
       
   295     "expanded_url" : "string",    
       
   296 }
       
   297