Skip to content

Instantly share code, notes, and snippets.

@eyaler
Last active August 7, 2021 19:36
Show Gist options
  • Save eyaler/b497eb96e2faea5129bccc3a2983993c to your computer and use it in GitHub Desktop.
Save eyaler/b497eb96e2faea5129bccc3a2983993c to your computer and use it in GitHub Desktop.
gsheet_fetcher.py
# to run:
# python gsheet_fetcher.py
try:
import requests # faster - need to pip install requests
get = requests.get
req = True
except:
import urllib.request
get = urllib.request.urlopen
req = False
import time
url = 'https://docs.google.com/spreadsheets/d/1pn4kw_mqTfjv-yFvCunfTH8gicSNWP46tAvgqA19gvw'
output_file = 'output.csv'
timeout = 60
sleep = 5
url = 'https://'+'/'.join(url.split('//')[-1].split('/')[:4])+'/export?format=csv'
print(url)
while True:
try:
with get(url, timeout=timeout) as r:
content = r.content if req else r.read()
print(content)
with open(output_file, 'wb') as f:
f.write(content)
except Exception as e:
print(e)
finally:
time.sleep(sleep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment