diff --git a/pom.xml b/pom.xml index 4c05f3fd..75b09df8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,5 @@ - + + 4.0.0 ru.fizteh.fivt.students @@ -45,6 +45,7 @@ preidman nikitarykov duha666 + spumote diff --git a/spumote/pom.xml b/spumote/pom.xml new file mode 100644 index 00000000..399956e1 --- /dev/null +++ b/spumote/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + ru.fizteh.fivt.students + parent + 1.0-SNAPSHOT + + ru.fizteh.fivt.students + spumote + 1.0-SNAPSHOT + spumote + http://maven.apache.org + + UTF-8 + + + + org.jsoup + jsoup + 1.6.3 + + + com.h2database + h2 + 1.4.190 + + + com.google.guava + guava + 12.0 + + + com.beust + jcommander + 1.48 + + + org.twitter4j + twitter4j-core + [4.0,) + + + org.twitter4j + twitter4j-stream + 4.0.4 + + + com.google.maps + google-maps-services + 0.1.1 + + + junit + junit + 4.12 + test + + + org.json + json + 20090211 + + + diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/CommandLineParser.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/CommandLineParser.java new file mode 100644 index 00000000..8abd82e0 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/CommandLineParser.java @@ -0,0 +1,53 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 18.12.15. + */ +import com.beust.jcommander.Parameter; + +public class CommandLineParser { + @Parameter(names = { "--query", "-q"}, description = "Ключевое слово для поиска.") + private String query = null; + + @Parameter(names = {"--place", "-p"}, description = "Искать только по заданному региону") + private String place = null; + + @Parameter(names = {"--stream", "-s"}, description = + "Выводить результаты поиска равномерно с задержкой в 1 секунду.") + private boolean stream = false; + + @Parameter(names = {"--hideRetweets"}, description = "Не показывать ретвиты.") + private boolean hideRetweets = false; + + @Parameter(names = {"--limit", "-l"}, description = "Выводить только заданное число твитов.") + private int limitTweets = 5; + + @Parameter(names = {"--help", "-h"}, description = "Справка.") + private boolean help = false; + + public String getQuery() { + return query; + } + + public String getPlace() { + return place; + } + + public boolean isStream() { + return stream; + } + + public boolean isHideRetweets() { + return hideRetweets; + } + + public int getLimitTweets() { + return limitTweets; + } + + public boolean isHelp() { + return help; + } +} + + diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/Location.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/Location.java new file mode 100644 index 00000000..5d690ee6 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/Location.java @@ -0,0 +1,65 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 18.12.15. + */ + +import com.google.maps.GeoApiContext; +import com.google.maps.GeocodingApi; +import com.google.maps.model.Bounds; +import com.google.maps.model.GeocodingResult; +import com.google.maps.model.LatLng; + +import java.io.*; +import java.util.Scanner; +import static java.lang.Math.*; + +public class Location { + + private static final double RADIUS_OF_EARTH = 6371; + + private GeocodingResult geocodingResults; + private double radius; + + Location(String place) { + String apiKey = null; + try { + File keyFile = new File("googlemaps.properties"); + apiKey = new Scanner(keyFile).useDelimiter("\\Z").next(); + } catch (FileNotFoundException exception) { + } + + GeoApiContext context = new GeoApiContext().setApiKey(apiKey); + + try { + geocodingResults = GeocodingApi.geocode(context, place).await()[0]; + } catch (Exception exception) { + } + + radius = calculateRadius(); + } + + private double calculateRadius() { + LatLng point1 = geocodingResults.geometry.bounds.northeast; + LatLng point2 = geocodingResults.geometry.bounds.southwest; + double rad = 180.0 / PI; + double a = cos(point1.lat / rad) * cos(point1.lng / rad) * cos(point2.lat / rad) * cos(point2.lng / rad); + double b = cos(point1.lat / rad) * sin(point1.lng / rad) * cos(point2.lat / rad) * sin(point2.lng / rad); + double c = sin(point1.lat / rad) * sin(point2.lat / rad); + + return RADIUS_OF_EARTH * acos(a + b + c); + } + + public LatLng getLocation() { + return geocodingResults.geometry.location; + } + + public double getRadius() { + return radius; + } + + public final Bounds getBounds() { + return geocodingResults.geometry.bounds; + } +} + diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/PrintTweet.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/PrintTweet.java new file mode 100644 index 00000000..9da9ee48 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/PrintTweet.java @@ -0,0 +1,61 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 18.12.15. + */ +import twitter4j.Status; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; +import java.util.Date; + +public class PrintTweet { + + public static String printDate(Date date) { + + String ans; + + LocalDateTime currentTime = LocalDateTime.now(); + LocalDateTime tweetTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + + if (ChronoUnit.MINUTES.between(tweetTime, currentTime) < 2) { + ans = "[только что]"; + return ans; + } + if (ChronoUnit.HOURS.between(tweetTime, currentTime) < 1) { + ans = "[" + ChronoUnit.MINUTES.between(tweetTime, currentTime) + " минут назад]"; + return ans; + } + if (ChronoUnit.DAYS.between(tweetTime, currentTime) < 1) { + ans = "[" + ChronoUnit.HOURS.between(tweetTime, currentTime) + " часов назад]"; + return ans; + } + if (ChronoUnit.DAYS.between(tweetTime, currentTime) == 1) { + ans = "[вчера]"; + return ans; + } + ans = "[" + ChronoUnit.DAYS.between(tweetTime, currentTime) + " дней назад]"; + return ans; + } + + public static String printTweet(Status status) { + + String ans = "@" + status.getUser().getScreenName() + ": "; + String text = status.getText(); + if (status.isRetweet()) { + int index = text.indexOf("@"); + text = text.substring(index, text.length()); + index = text.indexOf(" "); + String retweetedName = text.substring(0, index - 1); + text = text.substring(index + 1, text.length()); + ans += "ретвитнул " + retweetedName + ": "; + } + ans += text; + if (!status.isRetweet() && status.getRetweetCount() > 0) { + ans += " (" + status.getRetweetCount() + " ретвитов)"; + } + + return ans; + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/TwitterStream.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/TwitterStream.java new file mode 100644 index 00000000..0f4bc435 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/TwitterStream.java @@ -0,0 +1,98 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 18.12.15. + */ + +import com.beust.jcommander.JCommander; +import twitter4j.*; + +import java.util.List; + +public class TwitterStream { + private CommandLineParser commandLineParser; + + public static void main(String[] args) { + TwitterStream twitterStream = new TwitterStream(args); + if (twitterStream.commandLineParser != null) { + if (twitterStream.commandLineParser.isStream()) { + runStream(twitterStream.commandLineParser); + } else { + runSearch(twitterStream.commandLineParser); + } + } + } + + public TwitterStream(String[] args) { + commandLineParser = new CommandLineParser(); + JCommander jCommander = new JCommander(commandLineParser, args); + if (commandLineParser.isHelp()) { + jCommander.usage(); + } + } + + public static void runStream(CommandLineParser commandLineParser) { + twitter4j.TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); + StatusListener statusListener = new StatusAdapter() { + + @Override + public void onStatus(Status tweet) { + if (!commandLineParser.isHideRetweets() || !tweet.isRetweet()) { + System.out.println(PrintTweet.printTweet(tweet)); + + try { + Thread.sleep(1000); + } catch (InterruptedException exception) { + Thread.currentThread().interrupt(); + System.err.println("Thread error: " + exception.toString()); + exception.printStackTrace(System.err); + System.exit(1); + } + } + } + + @Override + public void onException(Exception exception) { + System.err.println("Stream error: " + exception.toString()); + exception.printStackTrace(System.err); + System.exit(1); + } + }; + + twitterStream.addListener(statusListener); + FilterQuery filterQuery = new FilterQuery(); + filterQuery.track(commandLineParser.getQuery()); + if (commandLineParser.getPlace() != null) { + Location findPlace = new Location((commandLineParser.getPlace())); + double[][] bounds = {{findPlace.getBounds().southwest.lng, findPlace.getBounds().southwest.lat}, + {findPlace.getBounds().northeast.lng, findPlace.getBounds().northeast.lat}}; + filterQuery.locations(bounds); + } + twitterStream.filter(filterQuery); + } + + public static void runSearch(CommandLineParser commandLineParser) { + Twitter twitter = new TwitterFactory().getInstance(); + Query query = new Query(); + query.setQuery(commandLineParser.getQuery()); + query.setCount(commandLineParser.getLimitTweets()); + + if (commandLineParser.getPlace() != null) { + Location googleFindPlace; + googleFindPlace = new Location(commandLineParser.getPlace()); + GeoLocation geoLocation = new GeoLocation(googleFindPlace.getLocation().lat, + googleFindPlace.getLocation().lng); + query.setGeoCode(geoLocation, googleFindPlace.getRadius(), Query.KILOMETERS); + } + + try { + List tweets = twitter.search(query).getTweets(); + for (Status tweet: tweets) { + System.out.print(PrintTweet.printDate(tweet.getCreatedAt())); + System.out.println(PrintTweet.printTweet(tweet)); + System.out.println("----------------------------------" + + "------------------------------------------------------"); + } + } catch (TwitterException exception) { } + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/reverser/Reverser.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/reverser/Reverser.java new file mode 100644 index 00000000..752dcb9f --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/reverser/Reverser.java @@ -0,0 +1,12 @@ +package ru.fizteh.fivt.students.spumote.reverser; + +/** + * Created by spumote on 14.12.15. + */ +public class Reverser { + public static void main(String[] args) { + for (int i = args.length - 1; i >= 0; i--) { + System.out.print(args[i] + " "); + } + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsBlockingQueue.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsBlockingQueue.java new file mode 100644 index 00000000..32edd0dc --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsBlockingQueue.java @@ -0,0 +1,40 @@ +package ru.fizteh.fivt.students.spumote.threads; + +/** + * Created by spumote on 17.12.15. + */ + +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +class ThreadsBlockingQueue { + private int maxQueueSize; + private Queue queue; + ThreadsBlockingQueue(int maxSize) { + maxQueueSize = maxSize; + queue = new LinkedList<>(); + } + public synchronized void offer(List e) throws InterruptedException { + while (queue.size() + e.size() > maxQueueSize) { + wait(); + Thread.sleep(100); + } + for (int i = 0; i < e.size(); ++i) { + queue.add(e.get(i)); + } + notifyAll(); + } + public synchronized List take(int n) throws InterruptedException { + while (queue.size() < n) { + wait(); + Thread.sleep(100); + } + List ans = new LinkedList<>(); + for (int i = 0; i < n; ++i) { + ans.add(queue.remove()); + } + notifyAll(); + return ans; + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCall.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCall.java new file mode 100644 index 00000000..bd7d542d --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCall.java @@ -0,0 +1,80 @@ +package ru.fizteh.fivt.students.spumote.threads; + +/** + * Created by spumote on 14.12.15. + */ +import java.util.Random; + +public class ThreadsCall { + + public static class Runner { + private int curNum = 0; + private boolean allReady = false; + private boolean alive = true; + private Random random; + + class ThClass extends Thread { + private volatile int number; + + @Override + public void run() { + while (alive) { + if (curNum + 1 == number) { + int x = random.nextInt(9) + 1; + if (x == 1) { + allReady = false; + System.out.println("No"); + } else { + System.out.println("Yes"); + } + curNum++; + } else { + try { + Thread.sleep(100); + } catch (InterruptedException e) { } + } + } + } + + ThClass() { + } + + ThClass(int num) { + this.number = num; + } + } + + void run(int n) { + random = new Random(); + curNum = n; + ThClass[] threads = new ThClass[n]; + for (int i = 0; i < n; i++) { + threads[i] = new ThClass(i + 1); + threads[i].start(); + } + while (true) { + if (curNum == n) { + if (allReady) { + break; + } + allReady = true; + System.out.println("Are you ready?"); + curNum = 0; + } else { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { } + } + } + alive = false; + } + } + + public static void main(String[] args) { + int n = 0; + if (args.length > 0) { + n = Integer.parseInt(args[0]); + } + new Runner().run(n); + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCounter.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCounter.java new file mode 100644 index 00000000..8525171d --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCounter.java @@ -0,0 +1,63 @@ +package ru.fizteh.fivt.students.spumote.threads; + +/** + * Created by spumote on 14.12.15. + */ + +public class ThreadsCounter { + + public static class Runner { + private int curNum = 0; + + class ThClass extends Thread { + private volatile int number; + + @Override + public void run() { + while (true) { + if (curNum + 1 == number) { + System.out.println("Thread-" + number); + curNum++; + } else { + try { + Thread.sleep(100); + } catch (InterruptedException e) { } + } + } + } + + ThClass() { + } + + ThClass(int num) { + this.number = num; + } + } + + void run(int n) { + curNum = n; + ThClass[] threads = new ThClass[n]; + for (int i = 0; i < n; i++) { + threads[i] = new ThClass(i + 1); + threads[i].start(); + } + while (true) { + if (curNum == n) { + curNum = 0; + } else { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { } + } + } + } + } + + public static void main(String[] args) { + int n = 0; + if (args.length > 0) { + n = Integer.parseInt(args[0]); + } + new Runner().run(n); + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLineParser.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLineParser.java new file mode 100644 index 00000000..c55e7c8b --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLineParser.java @@ -0,0 +1,53 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +/** + * Created by spumote on 17.12.15. + */ + +import com.beust.jcommander.Parameter; + +public class CommandLineParser { + @Parameter(names = { "--query", "-q"}, description = "Ключевое слово для поиска.") + private String query = null; + + @Parameter(names = {"--place", "-p"}, description = "Искать только по заданному региону") + private String place = null; + + @Parameter(names = {"--stream", "-s"}, description = + "Выводить результаты поиска равномерно с задержкой в 1 секунду.") + private boolean stream = false; + + @Parameter(names = {"--hideRetweets"}, description = "Не показывать ретвиты.") + private boolean hideRetweets = false; + + @Parameter(names = {"--limit", "-l"}, description = "Выводить только заданное число твитов.") + private int limitTweets = 5; + + @Parameter(names = {"--help", "-h"}, description = "Справка.") + private boolean help = false; + + public String getQuery() { + return query; + } + + public String getPlace() { + return place; + } + + public boolean isStream() { + return stream; + } + + public boolean isHideRetweets() { + return hideRetweets; + } + + public int getLimitTweets() { + return limitTweets; + } + + public boolean isHelp() { + return help; + } +} + diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/Location.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/Location.java new file mode 100644 index 00000000..c2666306 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/Location.java @@ -0,0 +1,70 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +/** + * Created by spumote on 17.12.15. + */ + +import com.google.maps.GeoApiContext; +import com.google.maps.GeocodingApi; +import com.google.maps.model.Bounds; +import com.google.maps.model.GeocodingResult; +import com.google.maps.model.LatLng; + +import java.io.*; +import java.util.Scanner; +import static java.lang.Math.*; + +public class Location { + + private static final double RADIUS_OF_EARTH = 6371; + + private GeocodingResult geocodingResults; + private double radius; + + Location(String place) { + String apiKey = null; + try { + File keyFile = new File("googlemaps.properties"); + apiKey = new Scanner(keyFile).useDelimiter("\\Z").next(); + } catch (FileNotFoundException exception) { + System.err.println("Can't find or read googleApiKey " + exception.toString()); + exception.printStackTrace(System.err); + System.exit(1); + } + + GeoApiContext context = new GeoApiContext().setApiKey(apiKey); + + try { + geocodingResults = GeocodingApi.geocode(context, place).await()[0]; + } catch (Exception exception) { + System.err.println("Break in get geocoding: " + exception.toString()); + exception.printStackTrace(System.err); + System.exit(1); + } + + radius = calculateRadius(); + } + + private double calculateRadius() { + LatLng point1 = geocodingResults.geometry.bounds.northeast; + LatLng point2 = geocodingResults.geometry.bounds.southwest; + double rad = 180.0 / PI; + double a = cos(point1.lat / rad) * cos(point1.lng / rad) * cos(point2.lat / rad) * cos(point2.lng / rad); + double b = cos(point1.lat / rad) * sin(point1.lng / rad) * cos(point2.lat / rad) * sin(point2.lng / rad); + double c = sin(point1.lat / rad) * sin(point2.lat / rad); + + return RADIUS_OF_EARTH * acos(a + b + c); + } + + public LatLng getLocation() { + return geocodingResults.geometry.location; + } + + public double getRadius() { + return radius; + } + + public final Bounds getBounds() { + return geocodingResults.geometry.bounds; + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/PrintTweet.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/PrintTweet.java new file mode 100644 index 00000000..1f3a9569 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/PrintTweet.java @@ -0,0 +1,62 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +/** + * Created by spumote on 17.12.15. + */ + +import twitter4j.Status; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; +import java.util.Date; + +public class PrintTweet { + + public static void printDate(Date date) { + + System.out.print("["); + + LocalDateTime currentTime = LocalDateTime.now(); + LocalDateTime tweetTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + + if (ChronoUnit.MINUTES.between(tweetTime, currentTime) < 2) { + System.out.print("только что]"); + return; + } + if (ChronoUnit.HOURS.between(tweetTime, currentTime) < 1) { + System.out.print(ChronoUnit.MINUTES.between(tweetTime, currentTime) + " минут назад]"); + return; + } + if (ChronoUnit.DAYS.between(tweetTime, currentTime) < 1) { + System.out.print(ChronoUnit.HOURS.between(tweetTime, currentTime) + " чаов назад]"); + return; + } + if (ChronoUnit.DAYS.between(tweetTime, currentTime) == 1) { + System.out.print("вчера]"); + return; + } + System.out.print(ChronoUnit.DAYS.between(tweetTime, currentTime) + " дней назад]"); + } + + public static void printTweet(Status status) { + + System.out.print("@" + status.getUser().getScreenName() + ": "); + String text = status.getText(); + if (status.isRetweet()) { + int index = text.indexOf("@"); + text = text.substring(index, text.length()); + index = text.indexOf(" "); + String retweetedName = text.substring(0, index - 1); + text = text.substring(index + 1, text.length()); + System.out.print("ретвитнул " + retweetedName + ": "); + } + System.out.print(text); + if (!status.isRetweet() && status.getRetweetCount() > 0) { + System.out.print(" (" + status.getRetweetCount() + " ретвитов)"); + } + System.out.println(); + + System.out.println("----------------------------------------------------------------------------------------"); + } +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/TwitterStream.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/TwitterStream.java new file mode 100644 index 00000000..102e952a --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/TwitterStream.java @@ -0,0 +1,96 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +/** + * Created by spumote on 17.12.15. + */ + +import com.beust.jcommander.JCommander; +import twitter4j.*; + +import java.util.List; + +public class TwitterStream { + private CommandLineParser commandLineParser; + + public static void main(String[] args) { + TwitterStream twitterStream = new TwitterStream(args); + if (twitterStream.commandLineParser != null) { + if (twitterStream.commandLineParser.isStream()) { + runStream(twitterStream.commandLineParser); + } else { + runSearch(twitterStream.commandLineParser); + } + } + } + + public TwitterStream(String[] args) { + commandLineParser = new CommandLineParser(); + JCommander jCommander = new JCommander(commandLineParser, args); + if (commandLineParser.isHelp()) { + jCommander.usage(); + } + } + + public static void runStream(CommandLineParser commandLineParser) { + twitter4j.TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); + StatusListener statusListener = new StatusAdapter() { + + @Override + public void onStatus(Status tweet) { + if (!commandLineParser.isHideRetweets() || !tweet.isRetweet()) { + PrintTweet.printTweet(tweet); + + try { + Thread.sleep(1000); + } catch (InterruptedException exception) { + Thread.currentThread().interrupt(); + System.err.println("Thread error: " + exception.toString()); + exception.printStackTrace(System.err); + System.exit(1); + } + } + } + + @Override + public void onException(Exception exception) { + System.err.println("Stream error: " + exception.toString()); + exception.printStackTrace(System.err); + System.exit(1); + } + }; + + twitterStream.addListener(statusListener); + FilterQuery filterQuery = new FilterQuery(); + filterQuery.track(commandLineParser.getQuery()); + if (commandLineParser.getPlace() != null) { + Location findPlace = new Location((commandLineParser.getPlace())); + double[][] bounds = {{findPlace.getBounds().southwest.lng, findPlace.getBounds().southwest.lat}, + {findPlace.getBounds().northeast.lng, findPlace.getBounds().northeast.lat}}; + filterQuery.locations(bounds); + } + twitterStream.filter(filterQuery); + } + + public static void runSearch(CommandLineParser commandLineParser) { + Twitter twitter = new TwitterFactory().getInstance(); + Query query = new Query(); + query.setQuery(commandLineParser.getQuery()); + query.setCount(commandLineParser.getLimitTweets()); + + if (commandLineParser.getPlace() != null) { + Location googleFindPlace; + googleFindPlace = new Location(commandLineParser.getPlace()); + GeoLocation geoLocation = new GeoLocation(googleFindPlace.getLocation().lat, + googleFindPlace.getLocation().lng); + query.setGeoCode(geoLocation, googleFindPlace.getRadius(), Query.KILOMETERS); + } + + try { + List tweets = twitter.search(query).getTweets(); + for (Status tweet: tweets) { + PrintTweet.printDate(tweet.getCreatedAt()); + PrintTweet.printTweet(tweet); + } + } catch (TwitterException exception) { } + } +} diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/AppTest.java b/spumote/src/test/java/ru/fizteh/fivt/students/AppTest.java new file mode 100644 index 00000000..5e698088 --- /dev/null +++ b/spumote/src/test/java/ru/fizteh/fivt/students/AppTest.java @@ -0,0 +1,38 @@ +package ru.fizteh.fivt.students; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestCommandLineParser.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestCommandLineParser.java new file mode 100644 index 00000000..8906b90c --- /dev/null +++ b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestCommandLineParser.java @@ -0,0 +1,57 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 18.12.15. + */ + +import com.beust.jcommander.JCommander; +import org.junit.Assert; +import org.junit.Test; + +public class TestCommandLineParser { + + @Test + public void testCaseIsHelp() { + CommandLineParser commandLineParser = new CommandLineParser(); + new JCommander(commandLineParser, new String[]{"-h", "-q", "fivt"}); + Assert.assertEquals(true, commandLineParser.isHelp()); + } + + @Test + public void testCaseIsNotHelp() { + CommandLineParser commandLineParser = new CommandLineParser(); + new JCommander(commandLineParser, new String[]{"-q", "fivt"}); + Assert.assertEquals(false, commandLineParser.isHelp()); + } + + @Test + public void testCaseIsStream() { + CommandLineParser commandLineParser = new CommandLineParser(); + new JCommander(commandLineParser, new String[]{"--stream", "-q", "fivt"}); + Assert.assertEquals(true, commandLineParser.isStream()); + } + + @Test + public void testCaseAllFirst() { + CommandLineParser commandLineParser = new CommandLineParser(); + new JCommander(commandLineParser, new String[]{"-q", "MimimiCat", "-s", "--place", "Kazan", "--hideRetweets"}); + Assert.assertEquals(5, commandLineParser.getLimitTweets()); + Assert.assertEquals("MimimiCat", commandLineParser.getQuery()); + Assert.assertEquals(false, commandLineParser.isHelp()); + Assert.assertEquals(true, commandLineParser.isStream()); + Assert.assertEquals(true, commandLineParser.isHideRetweets()); + Assert.assertEquals("Kazan", commandLineParser.getPlace()); + } + + @Test + public void testCaseAllSecond() { + CommandLineParser commandLineParser = new CommandLineParser(); + new JCommander(commandLineParser, new String[]{"--limit", "3", "-q", "Java", "--place", "California"}); + Assert.assertEquals(3, commandLineParser.getLimitTweets()); + Assert.assertEquals("Java", commandLineParser.getQuery()); + Assert.assertEquals(false, commandLineParser.isHelp()); + Assert.assertEquals(false, commandLineParser.isStream()); + Assert.assertEquals(false, commandLineParser.isHideRetweets()); + Assert.assertEquals("California", commandLineParser.getPlace()); + } +} diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestPrintTweet.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestPrintTweet.java new file mode 100644 index 00000000..32c71784 --- /dev/null +++ b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestPrintTweet.java @@ -0,0 +1,54 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 19.12.15. + */ + +import org.junit.Assert; +import org.junit.Test; +import twitter4j.Status; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; +import java.util.Date; + +public class TestPrintTweet { + @Test + public void testDateCase1() { + String needed = "[только что]"; + Date date = new Date(System.currentTimeMillis()); + Assert.assertEquals(needed, PrintTweet.printDate(date)); + } + @Test + public void testDateCase2() { + String needed = "[4 минут назад]"; + Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 4); + Assert.assertEquals(needed, PrintTweet.printDate(date)); + } + @Test + public void testDateCase3() { + String needed = "[1 часов назад]"; + Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60); + Assert.assertEquals(needed, PrintTweet.printDate(date)); + } + @Test + public void testDateCase4() { + String needed = "[вчера]"; + Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 30); + Assert.assertEquals(needed, PrintTweet.printDate(date)); + } + @Test + public void testDateCase5() { + String needed = "[3 дней назад]"; + Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 80); + Assert.assertEquals(needed, PrintTweet.printDate(date)); + } + + @Test + public void testPrintCase() { + String needed = "[3 дней назад]"; + Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 80); + Assert.assertEquals(needed, PrintTweet.printDate(date)); + } + +} diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/threads/BlockingQueueTest.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/threads/BlockingQueueTest.java new file mode 100644 index 00000000..08305970 --- /dev/null +++ b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/threads/BlockingQueueTest.java @@ -0,0 +1,54 @@ +package ru.fizteh.fivt.students.spumote.threads; + +/** + * Created by spumote on 17.12.15. + */ + +import java.util.LinkedList; +import java.util.List; + +public class BlockingQueueTest { + private static ThreadsBlockingQueue queue = new ThreadsBlockingQueue(20); + + static class OfferTest implements Runnable { + private List list = new LinkedList(); + OfferTest(int n) { + for (int i = 0; i < n; i++) { + list.add(i + 1); + } + } + @Override + public void run() { + try { + queue.offer(list); + System.out.println("Offer"); + } catch (InterruptedException e) { } + } + } + + static class TakeTest implements Runnable { + private int listSize; + private List list; + TakeTest(int n) { + listSize = n; + } + + @Override + public void run() { + try { + list = queue.take(listSize); + System.out.print("Take: "); + for (int i = 0; i < list.size(); i++) + System.out.print(list.get(i) + " "); + System.out.println(); + } catch (InterruptedException e) { } + } + } + + public static void main(String[] args) { + new Thread(new TakeTest(3)).start(); + new Thread(new OfferTest(4)).start(); + // new Thread(new TakeTest(9)).start(); + //new Thread(new OfferTest(8)).start(); + } +}