def facebook_user(self): facebook_api = None social_services = None if self.options.get('requesting_user_id', None): social_services = MSocialServices.get_user(self.options.get('requesting_user_id')) facebook_api = social_services.facebook_api() if not facebook_api: logging.debug(u' ***> [%-30s] ~FRFacebook fetch failed: %s: No facebook API for %s' % (self.feed.log_title[:30], self.feed.feed_address, self.options)) return else: usersubs = UserSubscription.objects.filter(feed=self.feed) if not usersubs: logging.debug(u' ***> [%-30s] ~FRFacebook fetch failed: %s: No subscriptions' % (self.feed.log_title[:30], self.feed.feed_address)) return for sub in usersubs: social_services = MSocialServices.get_user(sub.user_id) if not social_services.facebook_uid: continue facebook_api = social_services.facebook_api() if not facebook_api: continue else: break if not facebook_api: logging.debug(u' ***> [%-30s] ~FRFacebook fetch failed: %s: No facebook API for %s' % (self.feed.log_title[:30], self.feed.feed_address, usersubs[0].user.username)) return return facebook_api
def set_preference(request): code = 1 message = '' new_preferences = request.POST preferences = json.decode(request.user.profile.preferences) for preference_name, preference_value in new_preferences.items(): if preference_value in ['true','false']: preference_value = True if preference_value == 'true' else False if preference_name in SINGLE_FIELD_PREFS: setattr(request.user.profile, preference_name, preference_value) elif preference_name in SPECIAL_PREFERENCES: if preference_name == 'autofollow_friends': social_services = MSocialServices.get_user(request.user.pk) social_services.autofollow = preference_value social_services.save() elif preference_name == 'dashboard_date': request.user.profile.dashboard_date = datetime.datetime.utcnow() else: if preference_value in ["true", "false"]: preference_value = True if preference_value == "true" else False preferences[preference_name] = preference_value if preference_name == 'intro_page': logging.user(request, "~FBAdvancing intro to page ~FM~SB%s" % preference_value) request.user.profile.preferences = json.encode(preferences) request.user.profile.save() logging.user(request, "~FMSaving preference: %s" % new_preferences) response = dict(code=code, message=message, new_preferences=new_preferences) return response
def twitter_connect(request): twitter_consumer_key = settings.TWITTER_CONSUMER_KEY twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET oauth_token = request.GET.get('oauth_token') oauth_verifier = request.GET.get('oauth_verifier') denied = request.GET.get('denied') if denied: logging.user(request, "~BB~FRDenied Twitter connect") return {'error': 'Denied! Try connecting again.'} elif oauth_token and oauth_verifier: try: auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth.request_token = request.session['twitter_request_token'] # auth.set_request_token(oauth_token, oauth_verifier) auth.get_access_token(oauth_verifier) api = tweepy.API(auth) twitter_user = api.me() except (tweepy.TweepError, IOError) as e: logging.user(request, "~BB~FRFailed Twitter connect: %s" % e) return dict( error="Twitter has returned an error. Try connecting again.") # Be sure that two people aren't using the same Twitter account. existing_user = MSocialServices.objects.filter( twitter_uid=str(twitter_user.id)) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user( request, "~BB~FRFailed Twitter connect, another user: %s" % user.username) return dict(error=( "Another user (%s, %s) has " "already connected with those Twitter credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.twitter_uid = str(twitter_user.id) social_services.twitter_access_key = auth.access_token social_services.twitter_access_secret = auth.access_token_secret social_services.syncing_twitter = True social_services.save() SyncTwitterFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Twitter connect") return {} else: # Start the OAuth process auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth_url = auth.get_authorization_url() request.session['twitter_request_token'] = auth.request_token logging.user(request, "~BB~FRStarting Twitter connect: %s" % auth.request_token) return {'next': auth_url}
def appdotnet_connect(request): domain = Site.objects.get_current().domain args = { "client_id": settings.APPDOTNET_CLIENTID, "client_secret": settings.APPDOTNET_SECRET, "redirect_uri": "http://" + domain + reverse('appdotnet-connect'), "scope": ["email", "write_post", "follow"], } oauth_code = request.REQUEST.get('code') denied = request.REQUEST.get('denied') if denied: logging.user(request, "~BB~FRDenied App.net connect") return {'error': 'Denied! Try connecting again.'} elif oauth_code: try: adn_auth = appdotnet.Appdotnet(**args) response = adn_auth.getAuthResponse(oauth_code) adn_resp = json.decode(response) access_token = adn_resp['access_token'] adn_userid = adn_resp['user_id'] except (IOError): logging.user(request, "~BB~FRFailed App.net connect") return dict( error="App.net has returned an error. Try connecting again.") # Be sure that two people aren't using the same Twitter account. existing_user = MSocialServices.objects.filter( appdotnet_uid=unicode(adn_userid)) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user( request, "~BB~FRFailed App.net connect, another user: %s" % user.username) return dict(error=( "Another user (%s, %s) has " "already connected with those App.net credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.appdotnet_uid = unicode(adn_userid) social_services.appdotnet_access_token = access_token social_services.syncing_appdotnet = True social_services.save() SyncAppdotnetFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing App.net connect") return {} else: # Start the OAuth process adn_auth = appdotnet.Appdotnet(**args) auth_url = adn_auth.generateAuthUrl() logging.user(request, "~BB~FRStarting App.net connect") return {'next': auth_url}
def twitter_api(self, include_social_services=False): twitter_api = None social_services = None if self.options.get('requesting_user_id', None): social_services = MSocialServices.get_user( self.options.get('requesting_user_id')) try: twitter_api = social_services.twitter_api() except tweepy.error.TweepError as e: logging.debug( ' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.log_title[:30], self.address, e)) return else: usersubs = UserSubscription.objects.filter(feed=self.feed) if not usersubs: logging.debug( ' ***> [%-30s] ~FRTwitter fetch failed: %s: No subscriptions' % (self.feed.log_title[:30], self.address)) return for sub in usersubs: social_services = MSocialServices.get_user(sub.user_id) if not social_services.twitter_uid: continue try: twitter_api = social_services.twitter_api() if not twitter_api: continue else: break except tweepy.error.TweepError as e: logging.debug( ' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.log_title[:30], self.address, e)) continue if not twitter_api: logging.debug( ' ***> [%-30s] ~FRTwitter fetch failed: %s: No twitter API for %s' % (self.feed.log_title[:30], self.address, usersubs[0].user.username)) return if include_social_services: return twitter_api, social_services return twitter_api
def appdotnet_connect(request): domain = Site.objects.get_current().domain args = { "client_id": settings.APPDOTNET_CLIENTID, "client_secret": settings.APPDOTNET_SECRET, "redirect_uri": "http://" + domain + reverse('appdotnet-connect'), "scope": ["email", "write_post", "follow"], } oauth_code = request.REQUEST.get('code') denied = request.REQUEST.get('denied') if denied: logging.user(request, "~BB~FRDenied App.net connect") return {'error': 'Denied! Try connecting again.'} elif oauth_code: try: adn_auth = appdotnet.Appdotnet(**args) response = adn_auth.getAuthResponse(oauth_code) adn_resp = json.decode(response) access_token = adn_resp['access_token'] adn_userid = adn_resp['user_id'] except (IOError): logging.user(request, "~BB~FRFailed App.net connect") return dict(error="App.net has returned an error. Try connecting again.") # Be sure that two people aren't using the same Twitter account. existing_user = MSocialServices.objects.filter(appdotnet_uid=unicode(adn_userid)) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed App.net connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those App.net credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.appdotnet_uid = unicode(adn_userid) social_services.appdotnet_access_token = access_token social_services.syncing_appdotnet = True social_services.save() SyncAppdotnetFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing App.net connect") return {} else: # Start the OAuth process adn_auth = appdotnet.Appdotnet(**args) auth_url = adn_auth.generateAuthUrl() logging.user(request, "~BB~FRStarting App.net connect") return {'next': auth_url}
def fetch_user(self, username): twitter_api = None social_services = None if self.options.get('requesting_user_id', None): social_services = MSocialServices.get_user(self.options.get('requesting_user_id')) try: twitter_api = social_services.twitter_api() except tweepy.error.TweepError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.log_title[:30], self.address, e)) return
def twitter_connect(request): twitter_consumer_key = settings.TWITTER_CONSUMER_KEY twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET oauth_token = request.REQUEST.get('oauth_token') oauth_verifier = request.REQUEST.get('oauth_verifier') denied = request.REQUEST.get('denied') if denied: logging.user(request, "~BB~FRDenied Twitter connect") return {'error': 'Denied! Try connecting again.'} elif oauth_token and oauth_verifier: try: auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth.set_request_token(oauth_token, oauth_verifier) access_token = auth.get_access_token(oauth_verifier) api = tweepy.API(auth) twitter_user = api.me() except (tweepy.TweepError, IOError): logging.user(request, "~BB~FRFailed Twitter connect") return dict(error="Twitter has returned an error. Try connecting again.") # Be sure that two people aren't using the same Twitter account. existing_user = MSocialServices.objects.filter(twitter_uid=unicode(twitter_user.id)) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed Twitter connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those Twitter credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.twitter_uid = unicode(twitter_user.id) social_services.twitter_access_key = access_token.key social_services.twitter_access_secret = access_token.secret social_services.syncing_twitter = True social_services.save() SyncTwitterFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Twitter connect") return {} else: # Start the OAuth process auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret) auth_url = auth.get_authorization_url() logging.user(request, "~BB~FRStarting Twitter connect") return {'next': auth_url}
def fetch_twitter(self, address): username = None try: username_groups = re.search('twitter.com/(\w+)/?', address) if not username_groups: return username = username_groups.group(1) except IndexError: return twitter_api = None social_services = None if self.options.get('requesting_user_id', None): social_services = MSocialServices.get_user(self.options.get('requesting_user_id')) try: twitter_api = social_services.twitter_api() except tweepy.TweepError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.title[:30], address, e)) return
if self.options.get('requesting_user_id', None): social_services = MSocialServices.get_user(self.options.get('requesting_user_id')) try: twitter_api = social_services.twitter_api() except tweepy.TweepError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.title[:30], address, e)) return else: usersubs = UserSubscription.objects.filter(feed=self.feed) if not usersubs: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: No subscriptions' % (self.feed.title[:30], address)) return for sub in usersubs: social_services = MSocialServices.get_user(sub.user_id) try: twitter_api = social_services.twitter_api() except tweepy.TweepError, e: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.title[:30], address, e)) continue if not twitter_api: logging.debug(u' ***> [%-30s] ~FRTwitter fetch failed: %s: No twitter API for %s' % (self.feed.title[:30], address, usersubs[0].user.username)) return try: twitter_user = twitter_api.get_user(username) except TypeError, e:
def facebook_connect(request): facebook_app_id = settings.FACEBOOK_APP_ID facebook_secret = settings.FACEBOOK_SECRET args = { "client_id": facebook_app_id, "redirect_uri": "https://" + Site.objects.get_current().domain + '/oauth/facebook_connect', "scope": "user_friends", "display": "popup", } verification_code = request.GET.get('code') if verification_code: args["client_secret"] = facebook_secret args["code"] = verification_code uri = "https://graph.facebook.com/oauth/access_token?" + \ urllib.urlencode(args) response_text = urllib.urlopen(uri).read() response = json.decode(response_text) if "access_token" not in response: logging.user(request, "~BB~FRFailed Facebook connect, no access_token. (%s): %s" % (args, response)) return dict(error="Facebook has returned an error. Try connecting again.") access_token = response["access_token"] # Get the user's profile. graph = facebook.GraphAPI(access_token) profile = graph.get_object("me") uid = profile["id"] # Be sure that two people aren't using the same Facebook account. existing_user = MSocialServices.objects.filter(facebook_uid=uid) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed FB connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those Facebook credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.facebook_uid = uid social_services.facebook_access_token = access_token social_services.syncing_facebook = True social_services.save() SyncFacebookFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Facebook connect") return {} elif request.GET.get('error'): logging.user(request, "~BB~FRFailed Facebook connect, error: %s" % request.GET.get('error')) return {'error': '%s... Try connecting again.' % request.GET.get('error')} else: # Start the OAuth process logging.user(request, "~BB~FRStarting Facebook connect") url = "https://www.facebook.com/dialog/oauth?" + urllib.urlencode(args) return {'next': url}
def facebook_connect(request): facebook_app_id = settings.FACEBOOK_APP_ID facebook_secret = settings.FACEBOOK_SECRET args = { "client_id": facebook_app_id, "redirect_uri": "http://" + Site.objects.get_current().domain + reverse('facebook-connect'), "scope": "user_website,user_friends,publish_actions", "display": "popup", } verification_code = request.REQUEST.get('code') if verification_code: args["client_secret"] = facebook_secret args["code"] = verification_code uri = "https://graph.facebook.com/oauth/access_token?" + \ urllib.urlencode(args) response_text = urllib.urlopen(uri).read() response = urlparse.parse_qs(response_text) if "access_token" not in response: logging.user(request, "~BB~FRFailed Facebook connect") return dict(error="Facebook has returned an error. Try connecting again.") access_token = response["access_token"][-1] # Get the user's profile. graph = facebook.GraphAPI(access_token) profile = graph.get_object("me") uid = profile["id"] # Be sure that two people aren't using the same Facebook account. existing_user = MSocialServices.objects.filter(facebook_uid=uid) if existing_user and existing_user[0].user_id != request.user.pk: try: user = User.objects.get(pk=existing_user[0].user_id) logging.user(request, "~BB~FRFailed FB connect, another user: %s" % user.username) return dict(error=("Another user (%s, %s) has " "already connected with those Facebook credentials." % (user.username, user.email or "no email"))) except User.DoesNotExist: existing_user.delete() social_services = MSocialServices.get_user(request.user.pk) social_services.facebook_uid = uid social_services.facebook_access_token = access_token social_services.syncing_facebook = True social_services.save() SyncFacebookFriends.delay(user_id=request.user.pk) logging.user(request, "~BB~FRFinishing Facebook connect") return {} elif request.REQUEST.get('error'): logging.user(request, "~BB~FRFailed Facebook connect") return {'error': '%s... Try connecting again.' % request.REQUEST.get('error')} else: # Start the OAuth process logging.user(request, "~BB~FRStarting Facebook connect") url = "https://www.facebook.com/dialog/oauth?" + urllib.urlencode(args) return {'next': url}
def load_social_page(request, user_id, username=None, **kwargs): start = time.time() user = request.user social_user_id = int(user_id) social_user = get_object_or_404(User, pk=social_user_id) offset = int(request.REQUEST.get("offset", 0)) limit = int(request.REQUEST.get("limit", 6)) page = request.REQUEST.get("page") format = request.REQUEST.get("format", None) has_next_page = False feed_id = kwargs.get("feed_id") or request.REQUEST.get("feed_id") if page: offset = limit * (int(page) - 1) user_social_profile = None user_social_services = None if user.is_authenticated(): user_social_profile = MSocialProfile.get_user(user.pk) user_social_services = MSocialServices.get_user(user.pk) social_profile = MSocialProfile.get_user(social_user_id) params = dict(user_id=social_user.pk) if feed_id: params["story_feed_id"] = feed_id mstories = MSharedStory.objects(**params).order_by("-shared_date")[offset : offset + limit + 1] stories = Feed.format_stories(mstories) if len(stories) > limit: has_next_page = True stories = stories[:-1] checkpoint1 = time.time() if not stories: params = { "user": user, "stories": [], "feeds": {}, "social_user": social_user, "social_profile": social_profile, "user_social_services": user_social_services, "user_social_profile": json.encode(user_social_profile and user_social_profile.page()), } template = "social/social_page.xhtml" return render_to_response(template, params, context_instance=RequestContext(request)) story_feed_ids = list(set(s["story_feed_id"] for s in stories)) feeds = Feed.objects.filter(pk__in=story_feed_ids) feeds = dict((feed.pk, feed.canonical(include_favicon=False)) for feed in feeds) for story in stories: if story["story_feed_id"] in feeds: # Feed could have been deleted. story["feed"] = feeds[story["story_feed_id"]] shared_date = localtime_for_timezone(story["shared_date"], social_user.profile.timezone) story["shared_date"] = shared_date stories, profiles = MSharedStory.stories_with_comments_and_profiles(stories, social_user.pk, check_all=True) checkpoint2 = time.time() if user.is_authenticated(): for story in stories: if user.pk in story["share_user_ids"]: story["shared_by_user"] = True shared_story = MSharedStory.objects.get( user_id=user.pk, story_feed_id=story["story_feed_id"], story_guid=story["id"] ) story["user_comments"] = shared_story.comments stories = MSharedStory.attach_users_to_stories(stories, profiles) params = { "social_user": social_user, "stories": stories, "user_social_profile": user_social_profile, "user_social_profile_page": json.encode(user_social_profile and user_social_profile.page()), "user_social_services": user_social_services, "user_social_services_page": json.encode(user_social_services and user_social_services.to_json()), "social_profile": social_profile, "feeds": feeds, "user_profile": hasattr(user, "profile") and user.profile, "has_next_page": has_next_page, "holzer_truism": random.choice(jennyholzer.TRUISMS), # if not has_next_page else None } diff1 = checkpoint1 - start diff2 = checkpoint2 - start timediff = time.time() - start logging.user( request, "~FYLoading ~FMsocial page~FY: ~SB%s%s ~SN(%.4s seconds, ~SB%.4s/%.4s~SN)" % (social_profile.title[:22], ("~SN/p%s" % page) if page > 1 else "", timediff, diff1, diff2), ) if format == "html": template = "social/social_stories.xhtml" else: template = "social/social_page.xhtml" return render_to_response(template, params, context_instance=RequestContext(request))
try: twitter_api = social_services.twitter_api() except tweepy.error.TweepError, e: logging.debug( u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.log_title[:30], self.address, e)) return else: usersubs = UserSubscription.objects.filter(feed=self.feed) if not usersubs: logging.debug( u' ***> [%-30s] ~FRTwitter fetch failed: %s: No subscriptions' % (self.feed.log_title[:30], self.address)) return for sub in usersubs: social_services = MSocialServices.get_user(sub.user_id) if not social_services.twitter_uid: continue try: twitter_api = social_services.twitter_api() if not twitter_api: continue else: break except tweepy.error.TweepError, e: logging.debug( u' ***> [%-30s] ~FRTwitter fetch failed: %s: %s' % (self.feed.log_title[:30], self.address, e)) continue if not twitter_api: logging.debug(