kerguelenpetrel.RespondServlet.java Source code

Java tutorial

Introduction

Here is the source code for kerguelenpetrel.RespondServlet.java

Source

/*
 * Copyright (c) 2017 daloonik
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package kerguelenpetrel;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.util.Random;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.StatusUpdate;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;

import net.jeremybrooks.knicker.KnickerException;
import net.jeremybrooks.knicker.WordsApi;

import com.rometools.rome.io.FeedException;

@SuppressWarnings("serial")

public class RespondServlet extends HttpServlet {
    private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    private static Entity lastPostIdEntity;
    private static long lastPostId = 0;
    public static final String feedsFile = "WEB-INF/StaticFiles/feeds";
    private static String[] end = { "?", "!", " :-)", "...", "?!" };
    public static final String[] separator = new String[] { "?", "!", ",", "-", " " };
    public static final Random r = new Random();

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        StringBuilder builder = new StringBuilder();
        resp.setContentType("text/plain; charset=UTF-8");

        try {
            //Get the Twitter object
            Twitter twit = TwitterFactory.getSingleton();
            ResponseList<Status> mentions = twit.getMentionsTimeline();

            lastPostIdEntity = datastore.get(KeyFactory.createKey("lastPostIDEntity", "ID"));
            lastPostId = Long.parseLong(lastPostIdEntity.getProperty("lastPostID").toString());

            if (mentions.size() == 0) {
                resp.getWriter().println("No mentions so far...\n");
                return;
            }

            for (Status mention : mentions) {
                if (lastPostId < mention.getId()) {
                    if (mention.getUser().getId() == twit.getId())
                        ; //don't respond to myself

                    else if (mention.isRetweeted())
                        mention = twit.createFavorite(mention.getId()); //mark the retweet as a favourite
                    else if (mention.getText().toLowerCase().contains("bye")) {
                        builder.setLength(0);
                        builder.append("@");
                        builder.append(mention.getUser().getScreenName());
                        builder.append(" Bye");
                    } else {
                        builder.setLength(0);
                        //Add the screen name of the person we are responding to
                        builder.append("@");
                        builder.append(mention.getUser().getScreenName() + " ");

                        //Get feed title as content
                        builder.append(getFeedTitle(resp));

                        //Get a Wordnik trend
                        builder.append(getWordnikTrend(resp));

                        /* Tweets are maximum 280 characters */
                        if (builder.length() > 280) {
                            builder.setLength(builder.lastIndexOf(" ", 270));
                            builder.append(end[(r.nextInt(end.length))]);
                        }
                    }
                    //Set the status
                    StatusUpdate status = new StatusUpdate(builder.toString());

                    //Post the status
                    twit.updateStatus(status);
                    resp.getWriter().println("Tweet posted: " + status.getStatus());
                }
            }
            //Save last post ID
            lastPostIdEntity.setProperty("lastPostID", (Long.toString(mentions.get(0).getId())));
            datastore.put(lastPostIdEntity);
        } catch (EntityNotFoundException e) {
            // Make new ResponseIDentity
            lastPostIdEntity = new Entity("lastPostIDEntity", "ID");
            lastPostIdEntity.setProperty("lastPostID", "0");
            datastore.put(lastPostIdEntity);
            resp.getWriter()
                    .println("Made new lastPostId " + lastPostIdEntity.getProperty("lastPostID").toString());
        } catch (TwitterException e) {
            resp.getWriter().println("Problem with Twitter \n");
            e.printStackTrace(resp.getWriter());
        }
    }

    public static String getFeedTitle(HttpServletResponse resp) throws IOException {
        StringBuilder builder = new StringBuilder();
        try {
            GetFeed feed = new GetFeed(feedsFile);
            builder.append(feed.title());
            builder.append(separator[(r.nextInt(separator.length))] + " ");
        } catch (FileNotFoundException e) {
            resp.getWriter().println("Input file(s) not found \n");
            e.printStackTrace(resp.getWriter());
        } catch (FeedException e) {
            resp.getWriter().println("Problem with RSS Feed \n");
            e.printStackTrace(resp.getWriter());
        }
        return builder.toString();
    }

    public static String getWordnikTrend(HttpServletResponse resp) throws IOException {
        StringBuilder trend = new StringBuilder();
        String[] results = new String[2]; //Trend will be made up of 2 words

        try {
            Properties p = new Properties();
            InputStream in = UpdateStatusServlet.class.getResourceAsStream("wordnik.properties");
            p.load(in);
            System.setProperty("WORDNIK_API_KEY", p.getProperty("WORDNIK_API_KEY"));
        } catch (FileNotFoundException e) {
            resp.getWriter().println("Wordnik property file not found \n");
            e.printStackTrace(resp.getWriter());
        }
        try {
            for (int i = 0; i < results.length; i++) {
                results[i] = WordsApi.randomWord().getWord();
                while (results[i].contains("-")) //reject words with dashes
                    results[i] = WordsApi.randomWord().getWord();
            }
            //Build the trend
            trend.append(" #");
            for (String res : results)
                trend.append(res);
        } catch (KnickerException e) {
            resp.getWriter().println("Problem with Wordnik \n");
            e.printStackTrace(resp.getWriter());
        }
        return trend.toString();
    }
}