-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetData.py
More file actions
40 lines (35 loc) · 1.54 KB
/
getData.py
File metadata and controls
40 lines (35 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python3
# =================================================
# Download user data, decopmress and write to file.
# =================================================
import requests
import time
import bz2
from time import localtime, strftime
def getData(prefix,url):
s = requests.session()
adapter = requests.adapters.HTTPAdapter(
max_retries=5) # Configure adapter and retries
s.mount(prefix, adapter) # Mount adapter
response = s.get(url, timeout=5) # Get response from server
if response.status_code == requests.codes.ok: # Verify server response
binary = bz2.decompress(response.content) # Decompress data
decoded = binary.decode() # Decode binary data
archive = 'archive/allUsers/' + strftime("%d %b %y", localtime())
current = 'current/allUsers'
with open(current, 'w') as currentFile, open(archive, 'w') as archiveFile:
currentFile.write(decoded) # Dump all users data to file
archiveFile.write(decoded) # Dump all users data to archive
print('\nSuccess')
else:
print('\nError')
return(response.status_code)
# MAIN===========================================================================
tzero = time.time() # Set inital time for runtime test
prefix = ''
url = ''
status = getData(prefix,url)
print(status)
print('Algo runtime is %s seconds' %
(time.time() - tzero),'\n') # Final code runtime
# ===============================================================================