summaryrefslogtreecommitdiff
path: root/src/mining/Search.java
blob: 68f2a48079b35113dd6c192693dbf829d385b17f (plain)
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
111
112
113
114
115
116
117
118
package mining;

import data.Profile;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Retrieve data from twitter.com by explicitly searching for it.
 */
public class Search {

    // the class to use for authentication of the http connection.
    private final Authentication m_auth;

    public Search(Authentication auth) {
        m_auth = auth;

        getProfile("maskman113");
    }

    /**
     * Retrieves the profile information of the user
     *
     * @param username of the user whose profile needs to be retrieved
     * @return the profile information as profile object.
     */
    public Profile getProfile(String username) {
        BufferedReader bRead = null;
        JSONObject profile = null;

        boolean retry = false;

        try {
            System.out.println("Receiving profile of " + username);

            // Step 1: Open a http connection to 
            // https://api.twitter.com/1.1/users/show.json?screen_name=<username>
            // with a read timeout of 5 seconds.
            URL url = new URL("https://api.twitter.com/1.1/users/show.json?screen_name=" + username);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(5000);

            // Step 2: Sign the request using the OAuth Secret
            m_auth.sign(urlConnection);

            // try to connect to this url address.
            urlConnection.connect();

            // obtain the respons of the connection.
            int responseCode = urlConnection.getResponseCode();

            if (responseCode == 404 || responseCode == 401) {
                // Could not find host. 
                System.err.println("Connection failed, error: " + responseCode);
            } else if (responseCode == 500 || responseCode == 502 || responseCode == 503) {
                // Internal Server Error.
                disconnect(urlConnection, responseCode);
            } // Server quota has been reached.
            else if (responseCode == 429) {
                disconnect(urlConnection, responseCode);
                try {
                    // Sleep until more requests are available.
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
                }
                retry = true;
            }

            if (retry) {
                //recreate the connection because only the amount of requests was too much.
                urlConnection.connect();
            }

            StringBuilder content = new StringBuilder();

            urlConnection.disconnect();

            try {
                profile = new JSONObject(content.toString());

                System.out.println(profile.toString());
            } catch (JSONException ex) {
                System.err.println(ex);
            }
        } catch (OAuthException | IOException ex) {
            System.err.println(ex);
        }

        return new Profile();
    }

    public void searchTags() {

    }

    private void disconnect(HttpURLConnection urlConnection, int responseCode) {
        try {
            // Try to disconnect the connection.
            urlConnection.disconnect();
            System.out.println(responseCode);
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            // Closing the connection failed....
            System.err.println("Closing connection failed: " + ex.getMessage());
        }
    }
}