Advertisement
Guest User

this fell off a truck

a guest
May 19th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #! .venv/bin/python3
  2.  
  3. import psycopg2
  4. import psycopg2.extras
  5. import requests
  6. import sys
  7. import time
  8. import json
  9. import random
  10. from enum import Enum
  11.  
  12. class SL(Enum):
  13.     SHORT = 1
  14.     LONG = 2
  15.  
  16. def main():
  17.     conn = psycopg2.connect("dbname=degiro user=postgres password=pw host=localhost port=14000")
  18.     cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
  19.     cur.execute("select nextval('g_seq')")
  20.  
  21.     fetchsize = 100
  22.     sessionid = sys.argv[1]
  23.     j = get(sessionid, 0, fetchsize)
  24.     store(j, cur)
  25.  
  26.     left = j['total'] - fetchsize
  27.     offset = fetchsize
  28.  
  29.     while left > 0:
  30.         j = get(sessionid, offset, fetchsize)
  31.         store(j, cur)
  32.         left = left - fetchsize
  33.         print( f"request got offset {offset} plus {fetchsize}, now left {left}")
  34.         offset = offset + fetchsize
  35.  
  36.  
  37.     conn.commit()
  38.     cur.close()
  39.     conn.close()
  40.  
  41.  
  42. def get(sessionid, offset, limit):
  43.  
  44.     url = ( f"https://trader.degiro.nl/product_search/secure/v5/leverageds?popularOnly=false&inputAggregateTypes=issuer"
  45.             f"&inputAggregateValues=8&offset={offset}&limit={limit}&requireTotal=true&sortColumns=name&sortTypes=asc&intAccount=111111111_use_what_your_browser_puts_here"
  46.             f"&sessionId={sessionid}" )
  47.  
  48.     headers = {
  49.         'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0',
  50.         'Accept': 'application/json, text/plain, */*',
  51.         'Accept-Language': 'en-US,en;q=0.5',
  52.         'Referer': 'https://trader.degiro.nl/trader4/',
  53.         'Connection': 'keep-alive',
  54.         'Cookie': f"JSESSIONID={sessionid}",
  55.         'Pragma': 'no-cache',
  56.         'Cache-Control': 'no-cache',
  57.         'TE': 'Trailers'
  58.     }
  59.  
  60.     print (url)
  61.     r = requests.get(url, headers=headers)
  62.    
  63.     if (r.status_code != 200):
  64.         print("get failed, exiting")
  65.         sys.exit(255)
  66.  
  67.     time.sleep(random.randint(1, 9))
  68.     return r.json()
  69.  
  70. def store(j, cur):
  71.     for p in j['products']:
  72.         print(p['name'])      
  73.         cur.execute("insert into raw (t, g, j) values (current_timestamp, currval('g_seq'), %s)", (json.dumps(p),))
  74.  
  75. if __name__ == "__main__":
  76.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement