def cb_usr_code(request): user=User.objects.filter(username=request.session['user_id']).values()[0] request.session.__delitem__('user_id') userauth=authenticate(request,username=user['username'],password=user['password']) login(request,userauth) ClientId = settings.SOCIAL_AUTH_COINBASE_KEY clientsecret = settings.SOCIAL_AUTH_COINBASE_SECRET code1=request.GET.get('code') r2=requests.post("https://api.coinbase.com/oauth/token",data={"grant_type":"authorization_code","code":code1,"client_id":ClientId,"client_secret":clientsecret,"redirect_uri":"http://www.coinqual.com/coinbase/"}) cbAFtoken=r2.json() usrid=User.objects.filter(username=request.user).values()[0]['id'] #client=OAuthClient(cbAFtoken['access_token'],cbAFtoken['refresh_token']) try: if len(user_token.objects.filter(user_id=usrid).values()[0]['access_token']) >2: user_token.objects.filter(user_id=usrid).update(access_token=cbAFtoken['access_token'],refresh_token=cbAFtoken['refresh_token']) clito=user_token.objects.filter(user_id=usrid).values()[0] client=OAuthClient(clito['access_token'],clito['refresh_token']) userc=client.get_current_user().name price=client.get_spot_price().amount except: save_user_token=user_token(user_id=int(usrid),access_token=cbAFtoken['access_token'],refresh_token=cbAFtoken['refresh_token']) save_user_token.save() clito=user_token.objects.filter(user_id=usrid).values()[0] client=OAuthClient(clito['access_token'],clito['refresh_token']) userc=client.get_current_user()['email'] price=client.get_spot_price().amount # if request.user is not 'ananymous' or request.user is not None: # if client is None: # user_token(access_token=abAFtoken['access_token'],refresh_token=abAFtoken['refresh_token'],user_id=request.user) # user = client.get_current_user() # user_as_json_string = json.dumps(user) # CBtoken(access_token=cbAFtoken['access_token'],refresh_token=cbAFtoken['refresh_token']).save() us=request.user context={ 'us':us, 'code1':code1, 'user':userc, 'price':price,} # return redirect('main') return render(request,'polls/home.html',context)
def print_price(client: OAuthClient, base: str, currency: str) -> None: currency_pair = '{}-{}'.format(base, currency) print(f'Price for {currency_pair}:') print('Buy price:', client.get_buy_price(currency_pair=currency_pair).amount) print('Sell price:', client.get_sell_price(currency_pair=currency_pair).amount) print('Spot price:', client.get_spot_price(currency_pair=currency_pair).amount)
def forcoin(request): ClientId = "77d5af4941ffd6d5f0c9149e2e303a1c28d9f44c09b63694d6f2f608c60947c0" Clientsecret = "4395c36b93aa972ab4c6da2278cdca48f93e8551d0b13ee502552e249db90eb4" code = request.GET.get('code', '') r2 = requests.post("https://api.coinbase.com/oauth/token", data={ "grant_type": "authorization_code", "code": code, "client_id": ClientId, "client_secret": Clientsecret, "redirect_uri": "http://127.0.0.1:8000/Ali/forcoin/" }) r3 = r2.json() a = OAuthClient(r3['access_token'], r3['refresh_token']) b = a.get_spot_price().amount c = a.get_historic_prices(currency_pair='BTC-USD', period='day')['prices'] user = a.get_current_user().name useremail = a.get_current_user().email access = a.refresh()['access_token'] refresh = a.refresh()['refresh_token'] price = [] price2 = [] time = [] dif1 = [] for i in range(1, 21): price.append(float(c[i]['price'])) price2.append(float(c[i - 1]['price'])) dif1.append(round(price[i - 1] - price2[i - 1], 2)) time.append(c[i]['time']) coinpt = zip(price, dif1, time) context = { 'code': code, 'price': b, 'coinprice': price, 'time': time, 'coinpt': coinpt, 'user': user, 'useremail': useremail, 'access': access, 'refresh': refresh, } return render(request, 'Ali/forcoin.html', context)