|
a |
|
b/scripts/ctrpfullscrape.java |
|
|
1 |
import java.util.*; |
|
|
2 |
import java.io.*; |
|
|
3 |
import java.net.*; |
|
|
4 |
public class ctrpfullscrape { |
|
|
5 |
public static void main(String[] args) throws Exception { |
|
|
6 |
Scanner in = new Scanner(new File("fullctrpids.csv")); |
|
|
7 |
PrintWriter out = new PrintWriter(new File("ctrpdata.csv")); |
|
|
8 |
String useless = in.nextLine(); |
|
|
9 |
int count = 0; |
|
|
10 |
while(in.hasNext()) { |
|
|
11 |
System.out.println(count); |
|
|
12 |
count++; |
|
|
13 |
String id = in.nextLine(); |
|
|
14 |
String source = getURLSource("https://clinicaltrialsapi.cancer.gov/v1/clinical-trials?nct_id=" + id); |
|
|
15 |
int index = source.indexOf("display_order"); |
|
|
16 |
if (index > 0) { |
|
|
17 |
int partsbeg = source.indexOf("minimum_target_accrual_number"); |
|
|
18 |
int partsend = source.indexOf(",", partsbeg); |
|
|
19 |
int biobeg = source.indexOf("biomarkers\":"); |
|
|
20 |
int bioend = partsbeg; |
|
|
21 |
String parts = source.substring(partsbeg + 31, partsend); |
|
|
22 |
String bios = source.substring(biobeg + 12, bioend - 2); |
|
|
23 |
out.print(id + "," + parts + ",\"" + bios + "\""); |
|
|
24 |
out.println(); |
|
|
25 |
} |
|
|
26 |
} |
|
|
27 |
in.close(); |
|
|
28 |
out.close(); |
|
|
29 |
} |
|
|
30 |
public static String getURLSource(String url) throws IOException |
|
|
31 |
{ |
|
|
32 |
URL urlObject = new URL(url); |
|
|
33 |
URLConnection urlConnection = urlObject.openConnection(); |
|
|
34 |
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); |
|
|
35 |
try { |
|
|
36 |
return toString(urlConnection.getInputStream());}catch(Exception e) {return "";} |
|
|
37 |
} |
|
|
38 |
private static String toString(InputStream inputStream) throws IOException |
|
|
39 |
{ |
|
|
40 |
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) |
|
|
41 |
{ |
|
|
42 |
String inputLine; |
|
|
43 |
StringBuilder stringBuilder = new StringBuilder(); |
|
|
44 |
while ((inputLine = bufferedReader.readLine()) != null) |
|
|
45 |
{ |
|
|
46 |
stringBuilder.append(inputLine); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
return stringBuilder.toString(); |
|
|
50 |
} |
|
|
51 |
} |
|
|
52 |
} |