From 45be93bb0d50b373a52848a992afdd082e6c920e Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Mon, 14 Dec 2015 01:53:33 +0300 Subject: [PATCH 01/12] kolya --- pom.xml | 6 ++- spumote/pom.xml | 26 +++++++++++++ .../java/ru/fizteh/fivt/students/AppTest.java | 38 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 spumote/pom.xml create mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/AppTest.java diff --git a/pom.xml b/pom.xml index 4c05f3fd..1529b536 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,5 @@ - + + 4.0.0 ru.fizteh.fivt.students @@ -23,6 +23,7 @@ egiby akormushin + spumote okalitova xmanatee mamaevads @@ -45,6 +46,7 @@ preidman nikitarykov duha666 + spumote diff --git a/spumote/pom.xml b/spumote/pom.xml new file mode 100644 index 00000000..19479264 --- /dev/null +++ b/spumote/pom.xml @@ -0,0 +1,26 @@ + + + 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 + + + + junit + junit + 3.8.1 + test + + + 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 ); + } +} From 40695e69f9bc6aa970265db3cca0150d5e6facd5 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Mon, 14 Dec 2015 02:52:13 +0300 Subject: [PATCH 02/12] ko --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1529b536..75b09df8 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,6 @@ egiby akormushin - spumote okalitova xmanatee mamaevads From bff36b94cf4411f4d6496b6afa8131772a90abc7 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Mon, 14 Dec 2015 02:54:41 +0300 Subject: [PATCH 03/12] ko --- .../fivt/students/spumote/reverser/Reverser.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/reverser/Reverser.java 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] + " "); + } + } +} From 1f57fc4a109097ef68a70b9b0e448c2e5dbae104 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Tue, 15 Dec 2015 01:30:00 +0300 Subject: [PATCH 04/12] "+threads" --- .../students/spumote/threads/threads.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java new file mode 100644 index 00000000..ba12a035 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java @@ -0,0 +1,56 @@ +package ru.fizteh.fivt.students.spumote.threads; + +/** + * Created by spumote on 15.12.15. + */ + +import java.lang.*; + +class ThClass extends Thread { + private volatile int number; + + @Override + public void run() { + while (true) { + if (threads.curNum + 1 == number) { + System.out.println("Thread-" + number); + threads.curNum++; + } + else { + try{ + Thread.sleep(100); + } catch(InterruptedException e){} + } + } + } + + ThClass() {} + ThClass(int _num) { + this.number = _num; + } +} + +public class threads { + public static int curNum = 0; + + public static void main(String[] args) { + int n = 0; + if (args.length > 0) + n = Integer.parseInt(args[0]); + 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){} + } + } + } +} \ No newline at end of file From 3fa0f96c69eb3fb7c1f24733df81a8b7c061df2f Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Thu, 17 Dec 2015 03:39:47 +0300 Subject: [PATCH 05/12] task5 --- .../spumote/threads/ThreadsBlockingQueue.java | 40 ++++++++++ .../students/spumote/threads/ThreadsCall.java | 80 +++++++++++++++++++ .../spumote/threads/ThreadsCounter.java | 63 +++++++++++++++ .../students/spumote/threads/threads.java | 56 ------------- .../spumote/threads/BlockingQueueTest.java | 54 +++++++++++++ 5 files changed, 237 insertions(+), 56 deletions(-) create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsBlockingQueue.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCall.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/ThreadsCounter.java delete mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java create mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/threads/BlockingQueueTest.java 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/threads/threads.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java deleted file mode 100644 index ba12a035..00000000 --- a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/threads/threads.java +++ /dev/null @@ -1,56 +0,0 @@ -package ru.fizteh.fivt.students.spumote.threads; - -/** - * Created by spumote on 15.12.15. - */ - -import java.lang.*; - -class ThClass extends Thread { - private volatile int number; - - @Override - public void run() { - while (true) { - if (threads.curNum + 1 == number) { - System.out.println("Thread-" + number); - threads.curNum++; - } - else { - try{ - Thread.sleep(100); - } catch(InterruptedException e){} - } - } - } - - ThClass() {} - ThClass(int _num) { - this.number = _num; - } -} - -public class threads { - public static int curNum = 0; - - public static void main(String[] args) { - int n = 0; - if (args.length > 0) - n = Integer.parseInt(args[0]); - 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){} - } - } - } -} \ No newline at end of file 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..23ef092e --- /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(); + } +} From 3908a1320961b7630c73a5db08405607ff17d499 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Fri, 18 Dec 2015 03:40:29 +0300 Subject: [PATCH 06/12] task5 --- .../fizteh/fivt/students/spumote/temp/TwitterStream.java | 7 +++++++ .../fivt/students/spumote/twitterstream/CommandLine.java | 7 +++++++ .../fivt/students/spumote/twitterstream/TwitterStream.java | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/TwitterStream.java diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java new file mode 100644 index 00000000..c33b2453 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java @@ -0,0 +1,7 @@ +package ru.fizteh.fivt.students.spumote.temp; + +/** + * Created by spumote on 18.12.15. + */ +public class TwitterStream { +} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java new file mode 100644 index 00000000..22cdef48 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java @@ -0,0 +1,7 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +/** + * Created by spumote on 17.12.15. + */ +public class CommandLine { +} 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..668b4802 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/TwitterStream.java @@ -0,0 +1,7 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +/** + * Created by spumote on 17.12.15. + */ +public class TwitterStream { +} From 04a93b193be35fa8b384f95b3af7094465b7448b Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Fri, 18 Dec 2015 03:43:51 +0300 Subject: [PATCH 07/12] task5 --- spumote/pom.xml | 20 ++++ .../students/spumote/temp/TwitterStream.java | 7 -- .../spumote/twitterstream/CommandLine.java | 7 -- .../twitterstream/CommandLineParser.java | 48 ++++++++++ .../spumote/twitterstream/Location.java | 66 ++++++++++++++ .../spumote/twitterstream/PrintTweet.java | 58 ++++++++++++ .../spumote/twitterstream/TwitterStream.java | 91 ++++++++++++++++++- .../spumote/threads/BlockingQueueTest.java | 4 +- 8 files changed, 282 insertions(+), 19 deletions(-) delete mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java delete mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLineParser.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/Location.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/PrintTweet.java diff --git a/spumote/pom.xml b/spumote/pom.xml index 19479264..358cbbd9 100644 --- a/spumote/pom.xml +++ b/spumote/pom.xml @@ -22,5 +22,25 @@ 3.8.1 test + + 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 + diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java deleted file mode 100644 index c33b2453..00000000 --- a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/temp/TwitterStream.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.fizteh.fivt.students.spumote.temp; - -/** - * Created by spumote on 18.12.15. - */ -public class TwitterStream { -} diff --git a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java deleted file mode 100644 index 22cdef48..00000000 --- a/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLine.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.fizteh.fivt.students.spumote.twitterstream; - -/** - * Created by spumote on 17.12.15. - */ -public class CommandLine { -} 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..677b3cab --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/CommandLineParser.java @@ -0,0 +1,48 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +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..04952035 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/Location.java @@ -0,0 +1,66 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +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..83191e62 --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/twitterstream/PrintTweet.java @@ -0,0 +1,58 @@ +package ru.fizteh.fivt.students.spumote.twitterstream; + +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 index 668b4802..51984fd3 100644 --- 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 @@ -1,7 +1,92 @@ 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/spumote/threads/BlockingQueueTest.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/threads/BlockingQueueTest.java index 23ef092e..08305970 100644 --- 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 @@ -48,7 +48,7 @@ public void run() { 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(); + // new Thread(new TakeTest(9)).start(); + //new Thread(new OfferTest(8)).start(); } } From 9dfb2616156f2038bc9ddd3cfd35305b5efcec5e Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Fri, 18 Dec 2015 03:49:48 +0300 Subject: [PATCH 08/12] task1 --- .../students/spumote/twitterstream/CommandLineParser.java | 5 +++++ .../fizteh/fivt/students/spumote/twitterstream/Location.java | 4 ++++ .../fivt/students/spumote/twitterstream/PrintTweet.java | 4 ++++ .../fivt/students/spumote/twitterstream/TwitterStream.java | 4 ++++ 4 files changed, 17 insertions(+) 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 index 677b3cab..c55e7c8b 100644 --- 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 @@ -1,5 +1,9 @@ package ru.fizteh.fivt.students.spumote.twitterstream; +/** + * Created by spumote on 17.12.15. + */ + import com.beust.jcommander.Parameter; public class CommandLineParser { @@ -46,3 +50,4 @@ 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 index 04952035..c2666306 100644 --- 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 @@ -1,5 +1,9 @@ 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; 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 index 83191e62..1f3a9569 100644 --- 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 @@ -1,5 +1,9 @@ package ru.fizteh.fivt.students.spumote.twitterstream; +/** + * Created by spumote on 17.12.15. + */ + import twitter4j.Status; import java.time.LocalDateTime; 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 index 51984fd3..102e952a 100644 --- 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 @@ -1,5 +1,9 @@ package ru.fizteh.fivt.students.spumote.twitterstream; +/** + * Created by spumote on 17.12.15. + */ + import com.beust.jcommander.JCommander; import twitter4j.*; From e70e1c923f50bab1d038c623dcf80c7c11070321 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Sat, 19 Dec 2015 04:04:51 +0300 Subject: [PATCH 09/12] twitterstream + moduletests --- spumote/pom.xml | 27 +++-- .../library/CommandLineParser.java | 53 ++++++++++ .../spumote/moduletests/library/Location.java | 71 ++++++++++++++ .../moduletests/library/PrintTweet.java | 61 ++++++++++++ .../moduletests/library/TwitterStream.java | 98 +++++++++++++++++++ .../library/TestCommandLineParser.java | 57 +++++++++++ .../moduletests/library/TestLocation.java | 28 ++++++ .../moduletests/library/TestPrintTweet.java | 54 ++++++++++ 8 files changed, 443 insertions(+), 6 deletions(-) create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/CommandLineParser.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/Location.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/PrintTweet.java create mode 100644 spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/TwitterStream.java create mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestCommandLineParser.java create mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java create mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestPrintTweet.java diff --git a/spumote/pom.xml b/spumote/pom.xml index 358cbbd9..4a8f5413 100644 --- a/spumote/pom.xml +++ b/spumote/pom.xml @@ -16,12 +16,16 @@ UTF-8 - - junit - junit - 3.8.1 - test - + + org.jsoup + jsoup + 1.6.3 + + + com.google.guava + guava + 12.0 + com.beust jcommander @@ -42,5 +46,16 @@ 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..1e14564e --- /dev/null +++ b/spumote/src/main/java/ru/fizteh/fivt/students/spumote/moduletests/library/Location.java @@ -0,0 +1,71 @@ +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) { + 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/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/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/TestLocation.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java new file mode 100644 index 00000000..f81666c3 --- /dev/null +++ b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java @@ -0,0 +1,28 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 19.12.15. + */ + +import org.junit.Assert; +import org.junit.Test; +import com.google.maps.model.LatLng; + + +public class TestLocation { + @Test + public void testCase1() { + String place = "Kazan"; + LatLng expectedLocation = new LatLng(55.7887400, 49.1221400); + LatLng testLocation = new Location(place).getLocation(); + Assert.assertEquals(expectedLocation, testLocation); + } + + @Test + public void testCase2() { + String place = "Moscow"; + LatLng expectedLocation = new LatLng(55.7522200, 37.6155600); + LatLng testLocation = new Location(place).getLocation(); + Assert.assertEquals(expectedLocation, testLocation); + } +} 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)); + } + +} From d1bff865cd2cf2ed00b32c17a3eda958fa866891 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Sat, 19 Dec 2015 04:14:01 +0300 Subject: [PATCH 10/12] twitterstream + moduletests --- .../moduletests/library/TestLocation.java | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java deleted file mode 100644 index f81666c3..00000000 --- a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java +++ /dev/null @@ -1,28 +0,0 @@ -package ru.fizteh.fivt.students.spumote.moduletests.library; - -/** - * Created by spumote on 19.12.15. - */ - -import org.junit.Assert; -import org.junit.Test; -import com.google.maps.model.LatLng; - - -public class TestLocation { - @Test - public void testCase1() { - String place = "Kazan"; - LatLng expectedLocation = new LatLng(55.7887400, 49.1221400); - LatLng testLocation = new Location(place).getLocation(); - Assert.assertEquals(expectedLocation, testLocation); - } - - @Test - public void testCase2() { - String place = "Moscow"; - LatLng expectedLocation = new LatLng(55.7522200, 37.6155600); - LatLng testLocation = new Location(place).getLocation(); - Assert.assertEquals(expectedLocation, testLocation); - } -} From c96b4263a21a7ec176fb7268cc2bb903f3d365c7 Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Sat, 19 Dec 2015 08:21:56 +0300 Subject: [PATCH 11/12] added TestLocation --- spumote/pom.xml | 5 +++ .../spumote/moduletests/library/Location.java | 6 ---- .../moduletests/library/TestLocation.java | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java diff --git a/spumote/pom.xml b/spumote/pom.xml index 4a8f5413..399956e1 100644 --- a/spumote/pom.xml +++ b/spumote/pom.xml @@ -21,6 +21,11 @@ jsoup 1.6.3 + + com.h2database + h2 + 1.4.190 + com.google.guava guava 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 index 1e14564e..5d690ee6 100644 --- 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 @@ -27,9 +27,6 @@ public class Location { 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); @@ -37,9 +34,6 @@ public class Location { 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(); diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java new file mode 100644 index 00000000..d603131e --- /dev/null +++ b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java @@ -0,0 +1,33 @@ +package ru.fizteh.fivt.students.spumote.moduletests.library; + +/** + * Created by spumote on 19.12.15. + */ + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Ignore; +import com.google.maps.model.LatLng; +import static java.lang.Math.*; + +public class TestLocation { + public final double eps = 1e-5; + + @Test + public void testCase1() { + String place = "Kazan"; + LatLng expectedLocation = new LatLng(55.7887400, 49.1221400); + LatLng testLocation = new Location(place).getLocation(); + Assert.assertEquals(true, abs(expectedLocation.lng - testLocation.lng) < eps); + Assert.assertEquals(true, abs(expectedLocation.lat - testLocation.lat) < eps); + } + + @Ignore + @Test + public void testCase2() { + String place = "Moscow"; + LatLng expectedLocation = new LatLng(55.7522200, 37.6155600); + LatLng testLocation = new Location(place).getLocation(); + Assert.assertEquals(expectedLocation, testLocation); + } +} \ No newline at end of file From 1486f6063f3fa01a176c2b6cc98cfd8a6aa64d8f Mon Sep 17 00:00:00 2001 From: Nail Mingaliev Date: Sat, 19 Dec 2015 08:30:29 +0300 Subject: [PATCH 12/12] delete TestLocation --- .../moduletests/library/TestLocation.java | 33 ------------------- 1 file changed, 33 deletions(-) delete mode 100644 spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java diff --git a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java b/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java deleted file mode 100644 index d603131e..00000000 --- a/spumote/src/test/java/ru/fizteh/fivt/students/spumote/moduletests/library/TestLocation.java +++ /dev/null @@ -1,33 +0,0 @@ -package ru.fizteh.fivt.students.spumote.moduletests.library; - -/** - * Created by spumote on 19.12.15. - */ - -import org.junit.Assert; -import org.junit.Test; -import org.junit.Ignore; -import com.google.maps.model.LatLng; -import static java.lang.Math.*; - -public class TestLocation { - public final double eps = 1e-5; - - @Test - public void testCase1() { - String place = "Kazan"; - LatLng expectedLocation = new LatLng(55.7887400, 49.1221400); - LatLng testLocation = new Location(place).getLocation(); - Assert.assertEquals(true, abs(expectedLocation.lng - testLocation.lng) < eps); - Assert.assertEquals(true, abs(expectedLocation.lat - testLocation.lat) < eps); - } - - @Ignore - @Test - public void testCase2() { - String place = "Moscow"; - LatLng expectedLocation = new LatLng(55.7522200, 37.6155600); - LatLng testLocation = new Location(place).getLocation(); - Assert.assertEquals(expectedLocation, testLocation); - } -} \ No newline at end of file