Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.fizteh.fivt.students</groupId>
Expand Down Expand Up @@ -45,6 +45,7 @@
<module>preidman</module>
<module>nikitarykov</module>
<module>duha666</module>
<module>spumote</module>
</modules>

<dependencies>
Expand Down
66 changes: 66 additions & 0 deletions spumote/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ru.fizteh.fivt.students</groupId>
<artifactId>spumote</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spumote</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>12.0</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}


Original file line number Diff line number Diff line change
@@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Status> 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) { }
}
}
Original file line number Diff line number Diff line change
@@ -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] + " ");
}
}
}
Loading