info.usbo.skypetwitter.Run.java Source code

Java tutorial

Introduction

Here is the source code for info.usbo.skypetwitter.Run.java

Source

/*
 * Copyright 2015 Pavel_Goncharenko.
 *
 * 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 info.usbo.skypetwitter;

import com.skype.Chat;
import com.skype.Skype;
import com.skype.SkypeException;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import twitter4j.MediaEntity;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.URLEntity;

/**
 *
 * @author Pavel_Goncharenko
 */
public class Run {

    private static Integer bChanged;

    private static ArrayList<Long> twitter_ids = new ArrayList<Long>();
    private static ArrayList<Long> vk_ids = new ArrayList<Long>();
    private static ArrayList<VK> vk = new ArrayList<VK>();

    private static String data_dir;

    public static void main(String[] args) throws SkypeException, IOException {
        System.out.println("Working Directory = " + System.getProperty("user.dir"));
        Properties props = new Properties();
        loadProperties(props, "twitter4j.properties");
        loadProperties(props, "skype.properties");
        loadProperties(props, "app.properties");
        String chat_group_id = props.getProperty("skype.chat_group_id");
        String twitter_user_id = props.getProperty("twitter.user");
        data_dir = props.getProperty("data.dir");
        Integer twitter_timeout = Integer.parseInt(props.getProperty("twitter.timeout"));
        System.out.println("Twitter user: " + twitter_user_id);
        System.out.println("Twitter timeout: " + twitter_timeout);
        if ("".equals(twitter_user_id)) {
            return;
        }
        if (load_file() == 0) {
            System.out.println("File not found");
            return;
        }
        while (true) {
            bChanged = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
            System.out.println("Looking at " + sdf.format(Calendar.getInstance().getTime()));
            Chat ch = Skype.chat(chat_group_id);
            Twitter twitter = new TwitterFactory().getInstance();
            try {
                List<Status> statuses;
                statuses = twitter.getUserTimeline(twitter_user_id);
                String sText;
                for (Status status : statuses) {
                    Date d = status.getCreatedAt();
                    //  ?
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(d);
                    cal.add(Calendar.HOUR_OF_DAY, 7);
                    d = cal.getTime();
                    sText = "@" + status.getUser().getScreenName() + "  " + sdf.format(d)
                            + " ( https://twitter.com/" + twitter_user_id + "/status/" + status.getId() + " ): \r\n"
                            + status.getText() + "\r\n***";
                    for (URLEntity e : status.getURLEntities()) {
                        sText = sText.replaceAll(e.getURL(), e.getExpandedURL());
                    }
                    for (MediaEntity e : status.getMediaEntities()) {
                        sText = sText.replaceAll(e.getURL(), e.getMediaURL());
                    }
                    if (twitter_ids.indexOf(status.getId()) == -1) {
                        System.out.println(sText);
                        ch.send(sText);
                        twitter_ids.add(status.getId());
                        bChanged = 1;
                    }
                }
            } catch (TwitterException te) {
                te.printStackTrace();
                System.out.println("Failed to get timeline: " + te.getMessage());
                // System.exit(-1);
            } catch (SkypeException ex) {
                ex.printStackTrace();
                System.out.println("Failed to send message: " + ex.getMessage());
            }

            // VK
            try {
                vk();
                for (VK v : vk) {
                    if (vk_ids.indexOf(v.getId()) == -1) {
                        Date d = v.getDate();
                        //  ?
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(d);
                        cal.add(Calendar.HOUR_OF_DAY, 7);
                        d = cal.getTime();
                        String sText = "@Depersonilized (VK)  " + sdf.format(d)
                                + " ( http://vk.com/Depersonilized?w=wall-0_" + v.getId() + " ): \r\n"
                                + v.getText();
                        if (!"".equals(v.getAttachment())) {
                            sText += "\r\n" + v.getAttachment();
                        }
                        sText += "\r\n***";
                        System.out.println(sText);
                        ch.send(sText);
                        vk_ids.add(v.getId());
                        bChanged = 1;
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
                System.out.println("Failed to get vk: " + e.getMessage());
                // System.exit(-1);
            } catch (SkypeException ex) {
                ex.printStackTrace();
                System.out.println("Failed to send message: " + ex.getMessage());
            }
            if (bChanged == 1) {
                save_file();
            }
            try {
                Thread.sleep(1000 * 60 * twitter_timeout);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
                System.out.println("Failed to sleep: " + ex.getMessage());
            }
        }
    }

    public static void save_file() {
        try {
            FileOutputStream fos = new FileOutputStream(data_dir + "\\twitter_ids.data");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(twitter_ids);
            oos.close();
            fos.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        try {
            FileOutputStream fos = new FileOutputStream(data_dir + "\\vk_ids.data");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(vk_ids);
            oos.close();
            fos.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public static int load_file() {
        // TWITTER
        try {
            FileInputStream fis = new FileInputStream(data_dir + "\\twitter_ids.data");
            ObjectInputStream ois = new ObjectInputStream(fis);
            twitter_ids = (ArrayList<Long>) ois.readObject();
            ois.close();
            fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return 0;
        } catch (ClassNotFoundException c) {
            System.out.println("Class not found");
            c.printStackTrace();
            return 0;
        }
        // VK
        try {
            FileInputStream fis = new FileInputStream(data_dir + "\\vk_ids.data");
            ObjectInputStream ois = new ObjectInputStream(fis);
            vk_ids = (ArrayList<Long>) ois.readObject();
            ois.close();
            fis.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return 0;
        } catch (ClassNotFoundException c) {
            System.out.println("Class not found");
            c.printStackTrace();
            return 0;
        }
        return 1;
    }

    private static void loadProperties(Properties props, String path) throws IOException {
        InputStream in = Run.class.getResourceAsStream("/" + path);
        props.load(in);
        in.close();
    }

    private static void vk() throws ParseException {
        String url = "https://api.vk.com/method/wall.get?v=5.28&domain=Depersonilized&filter=owner&extended=1";
        String line = "";
        try {
            URL url2 = new URL(url);
            BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream(), "UTF-8"));
            line = reader.readLine();
            reader.close();

        } catch (MalformedURLException e) {
            // ...
        } catch (IOException e) {
            // ...
        }
        JSONObject json = (JSONObject) new JSONParser().parse(line);
        json = (JSONObject) new JSONParser().parse(json.get("response").toString());
        JSONArray jsona = (JSONArray) new JSONParser().parse(json.get("items").toString());
        vk.clear();
        for (int i = 0; i < jsona.size(); i++) {
            vk.add(new VK(jsona.get(i).toString()));
        }
    }

}