2011-12-21 09:43:17 -08:00
|
|
|
import datetime
|
2011-12-14 09:52:06 -08:00
|
|
|
import zlib
|
2011-12-22 13:36:03 -08:00
|
|
|
import hashlib
|
2011-12-14 09:52:06 -08:00
|
|
|
import mongoengine as mongo
|
2011-12-21 09:43:17 -08:00
|
|
|
from django.conf import settings
|
2011-12-22 13:36:03 -08:00
|
|
|
from django.contrib.auth.models import User
|
2011-12-20 11:49:49 -08:00
|
|
|
from vendor import facebook
|
|
|
|
from vendor import tweepy
|
2011-12-14 09:52:06 -08:00
|
|
|
|
|
|
|
|
|
|
|
class MSharedStory(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
|
|
|
shared_date = mongo.DateTimeField()
|
|
|
|
comments = mongo.StringField()
|
2011-12-19 09:24:19 -08:00
|
|
|
has_comments = mongo.BooleanField(default=False)
|
2011-12-14 09:52:06 -08:00
|
|
|
story_feed_id = mongo.IntField()
|
|
|
|
story_date = mongo.DateTimeField()
|
|
|
|
story_title = mongo.StringField(max_length=1024)
|
|
|
|
story_content = mongo.StringField()
|
|
|
|
story_content_z = mongo.BinaryField()
|
|
|
|
story_original_content = mongo.StringField()
|
|
|
|
story_original_content_z = mongo.BinaryField()
|
|
|
|
story_content_type = mongo.StringField(max_length=255)
|
|
|
|
story_author_name = mongo.StringField()
|
|
|
|
story_permalink = mongo.StringField()
|
|
|
|
story_guid = mongo.StringField(unique_with=('user_id',))
|
|
|
|
story_tags = mongo.ListField(mongo.StringField(max_length=250))
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'shared_stories',
|
|
|
|
'indexes': [('user_id', '-shared_date'), ('user_id', 'story_feed_id'), 'story_feed_id'],
|
|
|
|
'index_drop_dups': True,
|
|
|
|
'ordering': ['-shared_date'],
|
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.story_content:
|
|
|
|
self.story_content_z = zlib.compress(self.story_content)
|
|
|
|
self.story_content = None
|
|
|
|
if self.story_original_content:
|
|
|
|
self.story_original_content_z = zlib.compress(self.story_original_content)
|
|
|
|
self.story_original_content = None
|
|
|
|
super(MSharedStory, self).save(*args, **kwargs)
|
2011-12-20 11:49:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
class MSocialServices(mongo.Document):
|
|
|
|
user_id = mongo.IntField()
|
2011-12-22 13:36:03 -08:00
|
|
|
autofollow = mongo.BooleanField(default=True)
|
2011-12-20 11:49:49 -08:00
|
|
|
twitter_uid = mongo.StringField()
|
|
|
|
twitter_access_key = mongo.StringField()
|
|
|
|
twitter_access_secret = mongo.StringField()
|
|
|
|
twitter_friend_ids = mongo.ListField(mongo.StringField())
|
|
|
|
twitter_picture_url = mongo.StringField()
|
|
|
|
twitter_username = mongo.StringField()
|
|
|
|
twitter_refresh_date = mongo.DateTimeField()
|
|
|
|
facebook_uid = mongo.StringField()
|
|
|
|
facebook_access_token = mongo.StringField()
|
|
|
|
facebook_friend_ids = mongo.ListField(mongo.StringField())
|
|
|
|
facebook_picture_url = mongo.StringField()
|
|
|
|
facebook_refresh_date = mongo.DateTimeField()
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'social_services',
|
|
|
|
'indexes': ['user_id', 'twitter_friend_ids', 'facebook_friend_ids', 'twitter_uid', 'facebook_uid'],
|
|
|
|
'allow_inheritance': False,
|
|
|
|
}
|
2011-12-22 13:36:03 -08:00
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
user = User.objects.get(pk=self.user_id)
|
|
|
|
return {
|
|
|
|
'autofollow': self.autofollow,
|
|
|
|
'friends': self.friends(),
|
|
|
|
'services': {
|
|
|
|
'twitter': {
|
|
|
|
'twitter_username': self.twitter_username,
|
|
|
|
'twitter_picture_url': self.twitter_picture_url,
|
|
|
|
'twitter_uid': self.twitter_uid,
|
|
|
|
},
|
|
|
|
'facebook': {
|
|
|
|
'facebook_uid': self.facebook_uid,
|
|
|
|
'facebook_picture_url': self.facebook_picture_url,
|
|
|
|
},
|
|
|
|
'gravatar': {
|
|
|
|
'gravatar_picture_url': "http://www.gravatar.com/avatar/" + \
|
|
|
|
hashlib.md5(user.email).hexdigest()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 11:49:49 -08:00
|
|
|
|
2011-12-22 13:36:03 -08:00
|
|
|
def friends(self):
|
|
|
|
return []
|
|
|
|
|
2011-12-21 09:43:17 -08:00
|
|
|
def twitter_api(self):
|
|
|
|
twitter_consumer_key = settings.TWITTER_CONSUMER_KEY
|
|
|
|
twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET
|
|
|
|
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
|
|
|
|
auth.set_access_token(self.twitter_access_key, self.twitter_access_secret)
|
|
|
|
api = tweepy.API(auth)
|
|
|
|
return api
|
|
|
|
|
|
|
|
def facebook_api(self):
|
2011-12-20 11:49:49 -08:00
|
|
|
graph = facebook.GraphAPI(self.facebook_access_token)
|
2011-12-21 09:43:17 -08:00
|
|
|
return graph
|
|
|
|
|
|
|
|
def sync_twitter_friends(self):
|
|
|
|
api = self.twitter_api()
|
|
|
|
if not api:
|
|
|
|
return
|
|
|
|
|
|
|
|
friend_ids = list(unicode(friend.id) for friend in tweepy.Cursor(api.friends).items())
|
|
|
|
if not friend_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
twitter_user = api.me()
|
|
|
|
self.twitter_picture_url = twitter_user.profile_image_url
|
|
|
|
self.twitter_username = twitter_user.screen_name
|
|
|
|
self.twitter_friend_ids = friend_ids
|
|
|
|
self.twitter_refreshed_date = datetime.datetime.utcnow()
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def sync_facebook_friends(self):
|
|
|
|
graph = self.facebook_api()
|
2011-12-20 11:49:49 -08:00
|
|
|
if not graph:
|
|
|
|
return
|
|
|
|
|
|
|
|
friends = graph.get_connections("me", "friends")
|
|
|
|
if not friends:
|
|
|
|
return
|
|
|
|
|
2011-12-21 09:43:17 -08:00
|
|
|
facebook_friend_ids = [unicode(friend["id"]) for friend in friends["data"]]
|
2011-12-20 11:49:49 -08:00
|
|
|
self.facebook_friend_ids = facebook_friend_ids
|
2011-12-21 09:43:17 -08:00
|
|
|
self.facebook_refresh_date = datetime.datetime.utcnow()
|
2011-12-22 13:36:03 -08:00
|
|
|
self.facebook_picture_url = "//graph.facebook.com/%s/picture" % self.facebook_uid
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def disconnect_twitter(self):
|
|
|
|
self.twitter_uid = None
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def disconnect_facebook(self):
|
|
|
|
self.facebook_uid = None
|
2011-12-21 09:43:17 -08:00
|
|
|
self.save()
|