Switch to unified view

a b/scripts/ctsampleconditions.java
1
import java.util.*;
2
import java.io.*;
3
import java.net.*;
4
public class ctsampleconditions {
5
  public static void main(String[] args) throws Exception {
6
    Scanner in = new Scanner(new File("example_ct.csv"));
7
    PrintWriter out = new PrintWriter(new File("ctsampleconds.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().split(",")[0];
14
      String source = getURLSource("https://www.clinicaltrials.gov/ct2/show/record/"+id+"?term="+id+"&rank=1");
15
      int index = source.indexOf("<td", source.indexOf("Condition&nbsp"));
16
      int end = source.indexOf("/td", index);
17
      if (index > 0) {
18
        ArrayList<Integer> startinds = new ArrayList<Integer>();
19
        ArrayList<Integer> endinds = new ArrayList<Integer>();
20
        while (index > 0 && index < end) {
21
          if (source.charAt(index-1) == 'x') {
22
            startinds.add(index);
23
            endinds.add(source.indexOf("<", index));
24
          }
25
          index = source.indexOf("\">", index+1);
26
        }
27
        ArrayList<String> strings = new ArrayList<String>();
28
        for (int i = 0; i < startinds.size(); i++) strings.add(source.substring(startinds.get(i) + 2, endinds.get(i)).replaceAll("\"", "").replaceAll("\r\n", "").replaceAll("\n\r", ""));
29
        out.print(id + ",");
30
        for (int i = 0; i < strings.size(); i++) {
31
          out.print("\"" + strings.get(i) + "\"");
32
          if (i < strings.size() - 1) out.print(",");
33
        }
34
        out.println();
35
      }
36
    }
37
    in.close();
38
    out.close();
39
  }
40
  public static String getURLSource(String url) throws IOException
41
    {
42
        URL urlObject = new URL(url);
43
        URLConnection urlConnection = urlObject.openConnection();
44
        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");
45
        try {
46
        return toString(urlConnection.getInputStream());}catch(Exception e) {return "";}
47
    }
48
  private static String toString(InputStream inputStream) throws IOException
49
    {
50
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")))
51
        {
52
            String inputLine;
53
            StringBuilder stringBuilder = new StringBuilder();
54
            while ((inputLine = bufferedReader.readLine()) != null)
55
            {
56
                stringBuilder.append(inputLine);
57
            }
58
59
            return stringBuilder.toString();
60
        }
61
    }
62
}