def _oauth_init(self): self._oauth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) self._oauth.set_access_token( social_keys.TWITTER_APP_ACCESS_TOKEN, social_keys.TWITTER_APP_ACCESS_TOKEN_SECRET) self._oauth_api = API(self._oauth)
def _stream_init(self): api1 = API() headers = {} headers["Accept-Encoding"] = "deflate, gzip" stream_auth = BasicAuthHandler(social_keys.TWITTER_HTTP_AUTH_U, social_keys.TWITTER_HTTP_AUTH_P) l = TestStreamListener(api=api1) self._stream = Stream(auth=stream_auth, listener=l, secure=True, headers=headers)
def get(self): auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) auth.set_access_token(social_keys.TWITTER_APP_ACCESS_TOKEN, social_keys.TWITTER_APP_ACCESS_TOKEN_SECRET) api = API(auth) api_is_working = api.test() dt = datetime.datetime.fromtimestamp(time.time()) logging.info("current time is: %s" % dt) one_day = datetime.timedelta(days=1) yesterday = dt - one_day logging.info("yesterday is: %s" % yesterday) logging.info("today is: %s" % dt) day_filter = datetime.datetime(yesterday.year, yesterday.month, yesterday.day, hour=0, minute=0) day_today = datetime.datetime(dt.year, dt.month, dt.day, hour=0, minute=0) logging.info("day filter point is: %s" % day_filter) logging.info("tomorrow move to point is: %s" % day_today) q = model.SocialPostsForUsers.all() q.filter("day_created >=", day_filter) #TODO the resubmits aren't being pulled here an need to be user_list = [] post_list = [] queries = helpers.Queries() query_count = q.count(limit=10) logging.info("query count limit check: %s" % query_count) if query_count > 10: counter = 0 for result in q.run(config=queries.get_db_run_config_eventual()): if counter % 1000 == 0: self.perform_mix(user_list, post_list, api, day_today) user_list = [] post_list = [] user_list.append(result.social_user) post_list.append(result) counter = counter + 1 self.perform_mix(user_list, post_list, api, day_today) status_text = "there were %s mix ups on %s" % (counter, day_filter) logging.info(status_text) try: api.update_status(status=status_text, source="twixmit") except TweepError, e: logging.error("TweepError: %s", e)
def perform_mix(self, user_list, post_list, api, move_to): logging.info("list sized for users and posts are: %s" % len(user_list)) if len(user_list) > 1: logging.info("starting user and post list shuffle") random.shuffle(user_list) random.shuffle(post_list) logging.info("done with user and post list shuffle") logging.info("starting mix assignments") for index in range(len(user_list)): logging.info("next user index is %s" % index) user = user_list[index] post = post_list[index] logging.info("next user to is: %s" % user.user_id) logging.info("next post is: %s" % post.key()) status_text = "i just mixed the post from @%s to @%s" % ( post.social_user.shortcut_social_username, user.shortcut_social_username) logging.info(status_text) posted_to_twitter = True try: per_user_auth = OAuthHandler( social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) per_user_auth.set_access_token(user.access_token_key, user.access_token_secret) per_user_api = API(per_user_auth) if per_user_api.test(): per_user_api.update_status(status=post.text, source="twixmit") else: logging.error("failed to access api with user: %s" % user.user_id) api.update_status( status="hey @%s i can't access your twitter feed!" % user.shortcut_social_username, source="twixmit") except TweepError, e: logging.error("TweepError: %s", e) posted_to_twitter = False mix_model = model.SocialPostMixesForUsers( origin_post=post, posted_to_user=user, posted_from_user=post.social_user, posted_to_twitter=posted_to_twitter) mix_model.put() if post.resubmit == True: post.day_created = move_to post.put()
def get(self): verifier = self.request.GET.get('oauth_verifier') user = users.get_current_user() if not user: logging.warning("current user is not logged in") self.redirect("/") logging.info("running callback for user: %s" % user.user_id()) social_users = model.SocialKeysForUsers.all() social_users.filter("user_id =", user.user_id()) user_model = social_users.get() if not user_model == None and user_model.request_token_key and user_model.request_token_secret: try: auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) auth.set_request_token(user_model.request_token_key, user_model.request_token_secret) auth.get_access_token(verifier) user_model.access_token_key = auth.access_token.key user_model.access_token_secret = auth.access_token.secret api = API(auth) api_is_working = api.test() user_model.shortcut_social_username = api.me().screen_name user_model.put() memcache.add("twitter_user:%s" % user.user_id(), user_model.shortcut_social_username, 60) #self.response.out.write("twitter user name: %s\n" % user_model.shortcut_social_username) logging.debug("user access tokens have been set") self.redirect("/") except TweepError: logging.error( "TweepError error API is could not fetch me: %s" % user.user_id()) user_model.access_token_key = None user_model.access_token_secret = None user_model.put() self.redirect(URL_STATIC_ERROR_DEFAULT) except CapabilityDisabledError: logging.error( "Capability Disabled Error could not write for: %s" % user.user_id()) self.redirect(URL_STATIC_ERROR_DEFAULT) else: logging.warning( "user model is not setup correctly: %s for user % " % (user_model, user.user_id())) self.redirect("/")
def get_template_state_for_user(self): _template_values = {} user = users.get_current_user() user_model = None if user: logging.info("user: %s", user.nickname) social_users = model.SocialKeysForUsers.all() social_users.filter("user_id =", user.user_id()) user_model = social_users.get() if not user_model == None: if user_model.access_token_key and user_model.access_token_secret: _template_values["needs_twitter_auth"] = False auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) auth.set_access_token(user_model.access_token_key, user_model.access_token_secret) api = API(auth) api_is_working = api.test() if not api_is_working: logging.warning("api is NOT working: %s", api_is_working) try: twitter_user = memcache.get("twitter_user:%s" % user.user_id()) if twitter_user == None: twitter_user = api.me() memcache.add("twitter_user:%s" % user.user_id(), twitter_user, 60) logging.info(twitter_user) _template_values["twitter_user"] = twitter_user except TweepError: logging.error( "TweepError error has occured, clearing access tokents" ) user_model.access_token_key = None user_model.access_token_secret = None user_model.put() _template_values["needs_twitter_auth"] = True else: _template_values["needs_twitter_auth"] = True else: _template_values["needs_twitter_auth"] = True user_model = model.SocialKeysForUsers(user_id=user.user_id()) user_model.put() else: _template_values["needs_twitter_auth"] = True logging.warning("user is empty") redirect_url = None if _template_values["needs_twitter_auth"] and not user_model == None: auth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET, "https://%s/callback" % self.request.host) redirect_url = auth.get_authorization_url() user_model.request_token_key = auth.request_token.key user_model.request_token_secret = auth.request_token.secret user_model.put() _template_values["redirect_url"] = redirect_url _template_values["logout_url"] = users.create_logout_url("/") _template_values["login_url"] = users.create_login_url("/") _template_values["user"] = user _template_values["user_model"] = user_model util = helpers.Util() _template_values["next_mix_run_time"] = util.get_next_mix_runtime() _template_values[ "current_server_time"] = util.get_current_time_for_show() return _template_values