def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') if 'session' in request.POST: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.POST['session'], "'\"")) api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if x == "session": continue kwargs[x] = unquote(y) try: return callApi(func, *args, **kwargs) except Exception, e: print_exc() return HTTPError(500, dumps({"error": e.message, "traceback": format_exc()}))
def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') auth = parse_auth(request.get_header('Authorization', '')) # TODO: session as GET if 'session' in request.POST: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.POST['session'], "'\"")) elif auth: user = PYLOAD.checkAuth(auth[0], auth[1], request.environ.get('REMOTE_ADDR', None)) # if auth is correct create a pseudo session if user: s = {'uid': user.uid} api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if x == "session": continue kwargs[x] = unquote(y) try: return callApi(api, func, *args, **kwargs) except ExceptionObject, e: return HTTPError(400, dumps(e))
def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') if 'session' in request.POST: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.POST['session'], "'\"")) api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if x == "session": continue kwargs[x] = unquote(y) try: return callApi(func, *args, **kwargs) except Exception, e: print_exc() return HTTPError( 500, dumps({ "error": e.message, "traceback": format_exc() }))
def call_api(func, args=""): add_json_header(response) s = request.environ.get('beaker.session') # Accepts standard http auth auth = parse_auth(request.get_header('Authorization', '')) if 'session' in request.POST or 'session' in request.GET: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.params.get('session'), "'\"")) elif auth: user = PYLOAD.checkAuth(auth[0], auth[1], request.environ.get('REMOTE_ADDR', None)) # if auth is correct create a pseudo session if user: s = {'uid': user.uid} api = get_user_api(s) if not api: return error(401, "Unauthorized") if not PYLOAD.isAuthorized(func, api.user): return error(403, "Forbidden") if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func return error(404, "Not Found") # TODO: possible encoding # TODO Better error codes on invalid input args = [loads(unquote(arg)) for arg in args.split("/")[1:]] kwargs = {} # accepts body as json dict if request.json: kwargs = request.json # file upload, reads whole file into memory for name, f in request.files.iteritems(): kwargs["filename"] = f.filename content = StringIO() f.save(content) kwargs[name] = content.getvalue() content.close() # convert arguments from json to obj separately for x, y in request.params.iteritems(): try: if not x or not y or x == "session": continue kwargs[x] = loads(unquote(y)) except Exception, e: # Unsupported input msg = "Invalid Input %s, %s : %s" % (x, y, e.message) print_exc() print msg return error(415, msg)
def pre_processor(): s = request.environ.get("beaker.session") api = get_user_api(s) user = None status = None if api is not None: user = api.user status = api.getServerStatus() return {"user": user, "server": status, "url": request.url}
def pre_processor(): s = request.environ.get('beaker.session') api = get_user_api(s) user = None status = None if api is not None: user = api.user status = api.getServerStatus() return {"user": user, 'server': status, 'url': request.url }
def call_api(func, args=""): add_header(response) s = request.environ.get("beaker.session") # Accepts standard http auth auth = parse_auth(request.get_header("Authorization", "")) if "session" in request.POST or "session" in request.GET: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.params.get("session"), "'\"")) elif auth: user = PYLOAD.checkAuth(auth[0], auth[1], request.environ.get("REMOTE_ADDR", None)) # if auth is correct create a pseudo session if user: s = {"uid": user.uid} api = get_user_api(s) if not api: return HTTPError(401, dumps("Unauthorized"), **response.headers) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(403, dumps("Forbidden"), **response.headers) if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func return HTTPError(404, dumps("Not Found"), **response.headers) # TODO: possible encoding # TODO Better error codes on invalid input args = [loads(unquote(arg)) for arg in args.split("/")[1:]] kwargs = {} # accepts body as json dict if request.json: kwargs = request.json # convert arguments from json to obj separately for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if not x or not y or x == "session": continue kwargs[x] = loads(unquote(y)) try: result = getattr(api, func)(*args, **kwargs) # null is invalid json response if result is None: result = True return dumps(result) except ExceptionObject, e: return HTTPError(400, dumps(e), **response.headers)
def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') # Accepts standard http auth auth = parse_auth(request.get_header('Authorization', '')) if 'session' in request.POST or 'session' in request.GET: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.params.get('session'), "'\"")) elif auth: user = PYLOAD.checkAuth(auth[0], auth[1], request.environ.get('REMOTE_ADDR', None)) # if auth is correct create a pseudo session if user: s = {'uid': user.uid} api = get_user_api(s) if not api: return HTTPError(401, dumps("Unauthorized"), **response.headers) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(403, dumps("Forbidden"), **response.headers) if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func return HTTPError(404, dumps("Not Found"), **response.headers) # TODO: possible encoding # TODO Better error codes on invalid input args = [loads(unquote(arg)) for arg in args.split("/")[1:]] kwargs = {} # accepts body as json dict if request.json: kwargs = request.json # convert arguments from json to obj separately for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if not x or not y or x == "session": continue kwargs[x] = loads(unquote(y)) try: result = getattr(api, func)(*args, **kwargs) # null is invalid json response if result is None: result = True return dumps(result) except ExceptionObject, e: return HTTPError(400, dumps(e), **response.headers)
def handle_noargs(self, **options): for user in User.objects.all(): try: api = utils.get_user_api(user) me = api.me() except: continue twitter_account = TwitterAccount.objects.get(user=user) twitter_account_snapshot = TwitterAccountSnapshot( twitter_account = twitter_account, followers_count = me.followers_count, friends_count = me.friends_count, listed_count = me.listed_count, ) twitter_account_snapshot.save() print "saved snapshot for", user
def index(request): template_name = "index.html" if request.user.is_authenticated(): snapshots = TwitterAccountSnapshot.objects\ .filter(twitter_account__user=request.user)\ .order_by("-created") targets = Target.objects.filter(hunter=request.user)\ .order_by("-created") api = get_user_api(request.user) me = api.me() followers_count = me.followers_count friends_count = me.friends_count listed_count = me.listed_count ctx = locals() return direct_to_template(request, template_name, ctx)
def test_tweet(request): now = datetime.datetime.now() api = get_user_api(request.user) api.update_status("Test at %s" % now) return HttpResponseRedirect("/")
def handle_noargs(self, **options): self.log = logging.getLogger("market") profiles = UserProfile.objects.filter(marketing_on=True).order_by("-created") # profiles = UserProfile.objects.filter(user__username="******", marketing_on=True) print "=" * 70 print "[ Start marketing ]".center(70) print "=" * 70 for profile in profiles: # get the user user = profile.user # get the params of the marketing strategy self.profile = profile self.geocode = profile.geocode.strip() or None self.hits_per_query = profile.hits_per_query self.reciprocation_window = profile.reciprocation_window self.queries = [q.strip() for q in profile.queries.split("\r\n")] self.dms = [q.strip() for q in profile.direct_messages.strip().split("\r\n")] self.tweets = [q.strip() for q in profile.tweets.strip().split("\r\n")] if profile.competitors: lines = profile.competitors.strip().split("\r\n") self.competitors = [q.strip() for q in lines] else: self.competitors = [] # Remove empty string self.queries = filter(lambda x: bool(x), self.queries) self.dms = filter(lambda x: bool(x), self.dms) self.tweets = filter(lambda x: bool(x), self.tweets) self.competitors = filter(lambda x: bool(x), self.competitors) if (len(self.queries) == 0 and not self.profile.strategy == UserProfile.STEAL) or len(self.dms) == 0 or len(self.tweets) == 0: continue self.strategy = profile.strategy try: api = utils.get_user_api(user) print "[ Start marketing for %s ]" % user self.api = api self.user = user self.me = api.me() print "friends: %s, followers %s" % (self.me.friends_count, self.me.followers_count) # Do work # ---- # DB consistency operations -- make sure our sheets match # twitter sheets. we track adds, not deletes. # This takes a long time, we can move it to its own function # later to run daily instead of hourly self.add_untracked_friends() self.add_untracked_followers() #main work self.prune_losers() # import pdb; pdb.set_trace() candidates = Target.objects.filter(hunter=self.user, status=Target.ON_DECK) # don't look for new candidates unless we have less than 2000 on deck # 30k at time of writing... print "on deck count:", candidates.count() if candidates.count() < 2000: self.find_new_followers() # don't try to make contact if we already # have $follow_limit people (follow limits are # 2000 by default) FOLLOW_LIMIT = 2000 print "friend count:", self.api.me().friends_count if self.api.me().friends_count < FOLLOW_LIMIT: self.initiate_contact() print "DONE with %s" % self.me.screen_name print except Exception, e: raise