aboutsummaryrefslogtreecommitdiff
path: root/src/cal/get_tle/GetTLE.java.in
blob: 68e88d300125f1d8650f8eabd8fb88dd11c4a55a (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * GetTLE.java
 *
 * Created on January 16, 2005, 8:34 PM
 */
import java.net.URL;
import java.net.URLConnection;
import java.io.*;

import java.util.Map;
import java.util.List;
/**
 *
 * @author fred
 */
public class GetTLE {
    
    /** Creates a new instance of GetTLE */
    public GetTLE() {
    }
    
    /**
     * @param args the command line arguments
     */
    static final String host="www.space-track.org";
    static final String loginURL="/perl/login.pl";
    static final String dataURL="/perl/id_query.pl?ids=25791&timeframe=last5&common_name=yes&sort=catnum&descending=yes&ascii=yes&_submit=Submit&_submitted=1";
    static final String username="mromelfanger";
    static final String password="FuseJHU1";
  
    static final String outputFile = "/data1/fuse/calfuse/caltemp/five.tle";
    static final String logFile = "/data1/fuse/calfuse/caltemp/get_tle.logfile";
/*
    static final String outputFile = "/caltemp/five.tle";
    static final String logFile = "/caltemp/get_tle.logfile";
*/
    public static void main(String[] args) {
        // TODO code application logic here
        try {
            /*
             * Open the login url and get the session cookie back.
             */
            PrintWriter log = new PrintWriter(new OutputStreamWriter(new FileOutputStream(logFile)));
            PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile)));
            
            URL u = new URL("http://"+host+loginURL+"?username="+username+"&password="+password+"&_submitted=1&_submit=Submit");

            URLConnection c = u.openConnection();
            Map m = c.getHeaderFields();
            List l = (List) m.get("Set-Cookie");
            String session = null;
            for(int i=0;i!=l.size();i++) {
                String s = (String) l.get(i);
                if(s.startsWith("spacetrack_session=")) {
                    int end = s.indexOf(";");
                    if(end < 0)
                        end = s.length();
                    session = s.substring(0, end);
                    break;
                }
            }
            InputStream is = c.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String s;
            while((s = br.readLine()) != null)
                log.println(s);
            is.close();
            
            if(session == null) {
                out.println("Could not get session id");
                System.exit(1);
            }

            /*
             * Use the session cookie to query for the fuse data.
             */
            
            u = new URL("http://"+host+dataURL);
            c = u.openConnection();
            c.setRequestProperty("Cookie", session);
            is = c.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            /*
             * Read each line and keep what is between the pre's, add an extra
             * line at the end.
             */
            boolean collect = false;
	    out.println();
            while((s = br.readLine()) != null) {
                log.println(s);
                if(s.startsWith("<pre>")) {
/*                    br.readLine(); */
                    collect = true;
                } else if(s.startsWith("</pre>")) {
                    break;
                } else if(collect) {
                    out.println(s);
                }
            }
            out.println();
            br.close();
            is.close();
            out.close();
            log.close();

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}