aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/twitter/Twitter.java
blob: 4a1004620ce676fc726173621a06d291ee6309a3 (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
package cgeo.geocaching.twitter;

import cgeo.geocaching.Settings;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgTrackable;
import cgeo.geocaching.cgeoapplication;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter.Format;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.OAuth;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.utils.Log;

import org.apache.http.HttpResponse;


public final class Twitter {
    public static final int MAX_TWEET_SIZE = 140;

    public static void postTweet(final cgeoapplication app, final String status, final Geopoint coords) {
        if (app == null) {
            return;
        }
        if (!Settings.isTwitterLoginValid()) {
            return;
        }

        try {
            Parameters parameters = new Parameters("status", status);
            if (coords != null) {
                parameters.put(
                        "lat", coords.format(Format.LAT_DECDEGREE_RAW),
                        "long", coords.format(Format.LON_DECDEGREE_RAW),
                        "display_coordinates", "true");
            }

            OAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret());
            final HttpResponse httpResponse = Network.postRequest("http://api.twitter.com/1/statuses/update.json", parameters);
            if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) {
                Log.i("Tweet posted");
            } else {
                Log.e("Tweet could not be posted");
            }
        } catch (Exception e) {
            Log.e("cgBase.postTweet: " + e.toString());
        }
    }

    public static String appendHashTag(final String status, final String tag) {
        String result = status;
        if (result.length() + 2 + tag.length() <= 140) {
            result += " #" + tag;
        }
        return result;
    }

    public static void postTweetCache(String geocode) {
        final cgCache cache = cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB);
        String status;
        final String url = cache.getUrl();
        if (url.length() >= 100) {
            status = "I found " + url;
        }
        else {
            String name = cache.getName();
            status = "I found " + name + " (" + url + ")";
            if (status.length() > MAX_TWEET_SIZE) {
                name = name.substring(0, name.length() - (status.length() - MAX_TWEET_SIZE) - 3) + "...";
            }
            status = "I found " + name + " (" + url + ")";
            status = appendHashTag(status, "cgeo");
            status = appendHashTag(status, "geocaching");
        }
    
        postTweet(cgeoapplication.getInstance(), status, null);
    }

    public static void postTweetTrackable(String geocode) {
        final cgTrackable trackable = cgeoapplication.getInstance().getTrackableByGeocode(geocode);
        String name = trackable.getName();
        if (name.length() > 82) {
            name = name.substring(0, 79) + "...";
        }
        StringBuilder builder = new StringBuilder("I touched ");
        builder.append(name);
        if (trackable.getUrl() != null) {
            builder.append(" (").append(trackable.getUrl()).append(')');
        }
        builder.append('!');
        String status = appendHashTag(builder.toString(), "cgeo");
        status = appendHashTag(status, "geocaching");
        postTweet(cgeoapplication.getInstance(), status, null);
    }
}