-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiterate_files.py
81 lines (63 loc) · 2.73 KB
/
iterate_files.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import requests, subprocess, shlex, urlparse, os, sys
AUTHID='...'; AUTHPW='...'; HEADERS = {'content-type': 'application/json'}; SERVER = 'https://www.encodeproject.org/'
S3_SERVER='s3://encode-files/'
files = requests.get(
'https://www.encodeproject.org/search/?type=file&frame=embedded&limit=all',
auth=(AUTHID,AUTHPW), headers=HEADERS).json()['@graph']
statuses = {}
for f_obj in files:
status = f_obj.get('status')
if status in statuses:
statuses[status] += 1
else:
statuses.update({status: 1})
print statuses
#iter_files = [f for f in files if f.get('lab') == '/labs/brenton-graveley/']
#iter_files = [f for f in files if f.get('status') == 'uploading']
#iter_files = [f for f in files if f.get('status') == 'uploading' and f.get('lab') == '/labs/richard-myers/']
#iter_files = [f for f in files if f.get('status') == 'uploading' and f.get('lab') == '/labs/bradley-bernstein/']
iter_files = [f for f in files if f.get('status') == 'uploading' and f.get('lab') == '/labs/john-stamatoyannopoulos/']
for f_obj in iter_files:
#make the URL that will get redirected
encode_url = urlparse.urljoin(SERVER,f_obj.get('href'))
#stream=True avoids actually downloading the file, but it evaluates the redirection
r = requests.get(encode_url, auth=(AUTHID,AUTHPW), headers=HEADERS, allow_redirects=True, stream=True)
try:
r.raise_for_status
except:
print '%s href does not resolve' %(f_obj.get('accession'))
continue
#this is the actual URL after redirection
s3_url = r.url
#release the connection
r.close()
#split up the url into components
o = urlparse.urlparse(s3_url)
#pull out the filename
filename = os.path.basename(o.path)
#hack together the s3 cp url
bucket_url = S3_SERVER.rstrip('/') + o.path
#print bucket_url
#ls the file from the bucket
s3ls_string = subprocess.check_output(shlex.split('aws s3 ls %s' %(bucket_url)))
if s3ls_string.rstrip() == "":
print >> sys.stderr, "%s not in bucket" %(bucket_url)
else:
print "%s %s" %(f_obj.get('accession'), s3ls_string.rstrip())
#do the actual s3 cp
#return_value = subprocess.check_call(shlex.split('aws s3 cp %s %s' %(bucket_url, filename)))
#make sure it's here
#print subprocess.check_output(shlex.split('ls -l %s' %(filename)))
#calculate the md5
#md5 = subprocess.check_output(shlex.split('md5 -q %s' %(filename))).rstrip()
#print md5
#if str(f_obj.get('md5sum')) == str(md5):
# pass
#else:
# accession = f_obj.get('accession')
# print "%s: md5 mismatch (%s.md5=%s, calculated=%s)" %(accession, accession, f_obj.get('md5sum'), md5)
# do whatever else you want with the file
# remove the file when you're done
#return_value = subprocess.check_call(shlex.split('rm %s' %(filename)))
#I'm just breaking here so we don't download everything while we're testing
#break