Guest User

Untitled

a guest
Oct 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. # get_data.py
  2. # ...
  3.  
  4. # Continually fetch new trades until our LIMIT is reached
  5. next_trade_id = None
  6. iter_count = 0
  7. while True:
  8.  
  9. # Fetch a set of trades
  10. params = {}
  11. if next_trade_id:
  12. params['after'] = next_trade_id
  13. r = requests.get(URL, params=params)
  14.  
  15. # Transform each received trade into our desired format
  16. new_trades = [transform_trade(trade) for trade in r.json()]
  17.  
  18. # Append the new_trades to the dataframe
  19. trades.append(new_trades, ignore_index=True)
  20.  
  21. # Update the user
  22. new_trade_count = len(new_trades)
  23. print(f'Fetched {new_trade_count} trades, total count: {trade_count}...')
  24.  
  25. # Check the number of iterations
  26. iter_count += 1
  27. if iter_count >= ITER_LIMIT:
  28. print(f'Reached iteration limit {ITER_LIMIT}, stopping trade fetching...')
  29. break
  30.  
  31. # Get the cursor to the next page
  32. if NEXT_PAGE_HEADER not in r.headers:
  33. print('No more pages, stopping trade fetching...')
  34. break
  35. next_trade_id = r.headers[NEXT_PAGE_HEADER]
  36.  
  37. # Check the length of the dataframe to see if we've reached our goal yet
  38. trade_count = len(trades.index)
  39. if trade_count >= LIMIT:
  40. print('Reached trade limit, stopping trade fetching...')
  41. break
Add Comment
Please, Sign In to add comment