def address(request, address, data_type): #Check for rate limiting if too many requests max_allowable_requests = 3600 ip = util.get_client_ip(request) Rate_Limit.objects.create(ip=ip, function='address') limit = Rate_Limit.objects.filter(ip=ip, function='address').count() if limit > max_allowable_requests: error_text = _("Slow down <span class='notranslate'>Rainman</span> - you've made too many requests in the last hour") return HttpResponse(error_text, mimetype="text/plain", status=429) if data_type != "json": error_text = _("Invalid data type") return HttpResponse(error_text, mimetype="text/plain", status=400) if re.search("\W", address) is not None: error_text = _("Address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) address_is_valid = util.validate_bitcoin_address(address) if not address_is_valid: error_text = _("Address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) response_data = {} received = Received_Amount.objects.filter(prediction__receive_address=address).aggregate(Sum('amount'))["amount__sum"] returned_pending = Returned_Amount.objects.filter(returned_tx_to_returned_amount_link__isnull=True, to_prediction__return_address=address).aggregate(Sum('amount'))["amount__sum"] returned = Returned_Amount.objects.filter(returned_tx_to_returned_amount_link__isnull=False, to_prediction__return_address=address).aggregate(Sum('amount'))["amount__sum"] response_data["received"] = float(received) if received is not None else 0 response_data["returned_pending"] = float(returned_pending) if returned_pending is not None else 0 response_data["returned"] = float(returned) if returned is not None else 0 return HttpResponse(json.dumps(response_data), mimetype="application/json")
def address(request, address, data_type): #Check for rate limiting if too many requests max_allowable_requests = 3600 ip = util.get_client_ip(request) Rate_Limit.objects.create(ip=ip, function='address') limit = Rate_Limit.objects.filter(ip=ip, function='address').count() if limit > max_allowable_requests: error_text = _( "Slow down <span class='notranslate'>Rainman</span> - you've made too many requests in the last hour" ) return HttpResponse(error_text, mimetype="text/plain", status=429) if data_type != "json": error_text = _("Invalid data type") return HttpResponse(error_text, mimetype="text/plain", status=400) if re.search("\W", address) is not None: error_text = _("Address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) address_is_valid = util.validate_bitcoin_address(address) if not address_is_valid: error_text = _("Address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) response_data = {} received = Received_Amount.objects.filter( prediction__receive_address=address).aggregate( Sum('amount'))["amount__sum"] returned_pending = Returned_Amount.objects.filter( returned_tx_to_returned_amount_link__isnull=True, to_prediction__return_address=address).aggregate( Sum('amount'))["amount__sum"] returned = Returned_Amount.objects.filter( returned_tx_to_returned_amount_link__isnull=False, to_prediction__return_address=address).aggregate( Sum('amount'))["amount__sum"] response_data["received"] = float(received) if received is not None else 0 response_data["returned_pending"] = float( returned_pending) if returned_pending is not None else 0 response_data["returned"] = float(returned) if returned is not None else 0 return HttpResponse(json.dumps(response_data), mimetype="application/json")
def make_new_prediction(request): #Check for rate limiting if too many predictions max_allowable_predictions = 100 ip = util.get_client_ip(request) Rate_Limit.objects.create(ip=ip, function='make_new_prediction') limit = Rate_Limit.objects.filter(ip=ip, function='make_new_prediction').count() if limit > max_allowable_predictions: error_text = _("Slow down <span class='notranslate'>Rainman</span> - you've made too many predictions in the last hour") return HttpResponse(error_text, mimetype="text/plain", status=429) lt = request.POST["lt"] == "lt" price = util.price(request.POST["price"]) if price is None: error_text = _("Invalid price") return HttpResponse(error_text, mimetype="text/plain", status=400) unix_date = float(request.POST["time_to_check"]) / 1e3 utc_date = util.unix_time_to_datetime_utc(unix_date) if utc_date is None: error_text = _("Invalid date") return HttpResponse(error_text, mimetype="text/plain", status=400) if not utc_date.second == 0 and utc_date.microsecond == 0: error_text = _("Date must not include seconds") return HttpResponse(error_text, mimetype="text/plain", status=400) if not util.datetime_is_in_the_future(utc_date, FUTURE_WINDOW): error_text = _("Must be at least two hours into the future") return HttpResponse(error_text, mimetype="text/plain", status=400) return_address = request.POST["return_address"] if re.search("\W", return_address) is not None: error_text = _("Return address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) return_address_is_valid = util.validate_bitcoin_address(return_address) if not return_address_is_valid: error_text = _("Return address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) account_name = str(int(lt)) + "-" + str(price) + "-" + str(util.datetime_to_unix_time(utc_date)) + "-" + return_address receive_address = util.get_bitcoin_address(account_name) future_price = { "target_price": price, "time_to_match_price": utc_date, "time_window_closes": utc_date - datetime.timedelta(0, FUTURE_WINDOW) } existing_future_price_obj = Future_Price.objects.filter(**future_price) if existing_future_price_obj.count() > 0: future_price_obj = existing_future_price_obj[0] else: future_price_obj = Future_Price(**future_price) future_price_obj.save() new_prediction = { "future_price": future_price_obj, "receive_address": receive_address, "price_will_be_less_than_target": lt, "return_address": return_address, } prediction_obj = Prediction(**new_prediction) prediction_obj.save() response_data = { "address": receive_address } return HttpResponse(json.dumps(response_data), mimetype="application/json")
def make_new_prediction(request): #Check for rate limiting if too many predictions max_allowable_predictions = 100 ip = util.get_client_ip(request) Rate_Limit.objects.create(ip=ip, function='make_new_prediction') limit = Rate_Limit.objects.filter(ip=ip, function='make_new_prediction').count() if limit > max_allowable_predictions: error_text = _( "Slow down <span class='notranslate'>Rainman</span> - you've made too many predictions in the last hour" ) return HttpResponse(error_text, mimetype="text/plain", status=429) lt = request.POST["lt"] == "lt" price = util.price(request.POST["price"]) if price is None: error_text = _("Invalid price") return HttpResponse(error_text, mimetype="text/plain", status=400) unix_date = float(request.POST["time_to_check"]) / 1e3 utc_date = util.unix_time_to_datetime_utc(unix_date) if utc_date is None: error_text = _("Invalid date") return HttpResponse(error_text, mimetype="text/plain", status=400) if not utc_date.second == 0 and utc_date.microsecond == 0: error_text = _("Date must not include seconds") return HttpResponse(error_text, mimetype="text/plain", status=400) if not util.datetime_is_in_the_future(utc_date, FUTURE_WINDOW): error_text = _("Must be at least two hours into the future") return HttpResponse(error_text, mimetype="text/plain", status=400) return_address = request.POST["return_address"] if re.search("\W", return_address) is not None: error_text = _("Return address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) return_address_is_valid = util.validate_bitcoin_address(return_address) if not return_address_is_valid: error_text = _("Return address is invalid") return HttpResponse(error_text, mimetype="text/plain", status=400) account_name = str(int(lt)) + "-" + str(price) + "-" + str( util.datetime_to_unix_time(utc_date)) + "-" + return_address receive_address = util.get_bitcoin_address(account_name) future_price = { "target_price": price, "time_to_match_price": utc_date, "time_window_closes": utc_date - datetime.timedelta(0, FUTURE_WINDOW) } existing_future_price_obj = Future_Price.objects.filter(**future_price) if existing_future_price_obj.count() > 0: future_price_obj = existing_future_price_obj[0] else: future_price_obj = Future_Price(**future_price) future_price_obj.save() new_prediction = { "future_price": future_price_obj, "receive_address": receive_address, "price_will_be_less_than_target": lt, "return_address": return_address, } prediction_obj = Prediction(**new_prediction) prediction_obj.save() response_data = {"address": receive_address} return HttpResponse(json.dumps(response_data), mimetype="application/json")