-
Notifications
You must be signed in to change notification settings - Fork 16
/
scrap.py
81 lines (69 loc) · 2.92 KB
/
scrap.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 argparse, sys
from gisaid_scrapper import GisaidCoVScrapper
import os
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def parse_args():
parser=argparse.ArgumentParser()
parser.add_argument('--username', '-u', help="Username for GISAID", type=str)
parser.add_argument('--password', '-p', help="Password for GISAID", type=str)
parser.add_argument('--filename', '-f', help="Path to file with credentials (alternative, default: credentials.txt)", type=str, default="credentials.txt")
parser.add_argument('--destination', '-d', help="Destination directory (default: fastas/)", type=str, default="fastas/")
parser.add_argument('--headless', '-q', help="Headless mode of scraping (experimental)", type=str2bool, nargs='?', default=False)
parser.add_argument('--whole', '-w', help="Scrap whole genomes only", type=str2bool, nargs='?', default=False)
args = parser.parse_args()
args.headless = True if args.headless is None else args.headless
args.whole = True if args.whole is None else args.whole
return parser, args
def get_credentials(args):
if "DOCKER_MODE" in os.environ:
login = os.getenv("GISAID_USER")
passwd = os.getenv("GISAID_PASS")
destination = os.getenv("DESTINATION")
whole = os.getenv("WHOLE_GENOME")==1
else:
if args.username is None or args.password is None:
if args.filename is None:
raise ValueError
try:
with open(args.filename) as f:
login = f.readline()
passwd = f.readline()
except FileNotFoundError:
print("File not found.")
raise ValueError
else:
login = args.username
passwd = args.password
whole = args.whole
destination = args.destination
return login, passwd, whole, destination
if __name__ == "__main__":
parser, args = parse_args()
try:
login, passwd, whole, destination = get_credentials(args)
except ValueError:
print(parser.format_help())
sys.exit(-1)
scrapper = GisaidCoVScrapper(args.headless, whole, destination)
scrapper.login(login, passwd)
# scrapper.load_epicov()
# page = 0
# for i in range(150):
# scrapper.go_to_next_page()
# page += 1
# while not scrapper.finished:
# print("Page:", page)
# scrapper.download_from_curr_page()
# scrapper.go_to_next_page()
# page += 1
scrapper.download_packages('metadata_tsv')
scrapper.download_packages('fasta.tar')
print("New samples:", scrapper.new_downloaded)