Skip to content
40 changes: 40 additions & 0 deletions projects/liza22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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.mipt.diht.students</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>ru.mipt.diht.students</groupId>
<artifactId>liza22</artifactId>
<version>1.0-SNAPSHOT</version>
<name>liza22</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</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,)</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency> </dependencies>
</project>
11 changes: 11 additions & 0 deletions projects/liza22/src/main/java/ru/mipt/diht/students/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.mipt.diht.students;

/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twitter.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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>com.twitter.stream</groupId>
<artifactId>twitterstream</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>

<jcommander-version>1.48</jcommander-version>
<twitter4j-version>4.0.4</twitter4j-version>
</properties>

<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>${jcommander-version}</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>${twitter4j-version}</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>${twitter4j-version}</version>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>TwitterStream</mainClass>
<classpathPrefix>lib/*d</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import com.beust.jcommander.JCommander;
import config.Arguments;
import config.Constants;
import config.TwitterConfig;
import core.handling.TweetHandler;
import core.handling.TweetHandlerFactory;
import core.providing.TweetsProvider;
import core.providing.TweetsProviderFactory;
import model.Mode;

import java.io.*;

/**
* Main Class.
*/

public class TwitterStream {

public static final int LINE_LENGTH = 1024;

public static void main(final String[] argsString) {
extractArguments(argsString);
Arguments arguments = Arguments.getInstance();

/* In case of HELP page is requested,
* just print HELP file content and exit application
*/
if (arguments.isHelpRequest()) {
printHelp(System.out);
System.exit(0);
}
try {
// read and initialize twitter configuration
TwitterConfig twitterConfig = new TwitterConfig();
// define working mode of application
Mode workingMode;
if (arguments.isStreamMode()) {
workingMode = Mode.STREAM;
} else {
workingMode = Mode.QUERY;
}
// get tweets provider for selected working mode and initialize this one
TweetsProvider tweetsProvider = TweetsProviderFactory.getProvider(workingMode);
tweetsProvider.init(twitterConfig);
// get tweet handler for selected working mode
TweetHandler tweetHandler = TweetHandlerFactory.getHandler(workingMode);
// start tweets providing and handling
tweetsProvider.provide(tweetHandler);
} catch (Exception e) {
System.err.println("Application TwitterStream has been occasionally crashed "
+ "with error = \"" + e.getMessage() + "\"");
System.err.println("Application will be terminated with error code.");
System.exit(1);
}
}

/**
* Transforms arguments string to Arguments object with parsed fields.
* @param argsString arguments strings from the input
*/
private static void extractArguments(final String[] argsString) {
JCommander jCommander = new JCommander();
jCommander.addObject(Arguments.getInstance());
jCommander.parse(argsString);
}

/**
* Prints the content of HELP file to the passed output stream.
* @param out stream where HELP page will be printed
*/
private static void printHelp(OutputStream out) {
try {
byte[] buffer = new byte[LINE_LENGTH];
try (InputStream input = TwitterStream.class.getClassLoader().getResourceAsStream(Constants.HELP_FILE)) {
int length = input.read(buffer);
while (length != -1) {
out.write(buffer, 0, length);
length = input.read(buffer);
}
}
} catch (IOException e) {
System.err.println("Problem with reading help file: \"" + e.getMessage() + "\"");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package config;

import com.beust.jcommander.Parameter;

import java.util.List;

/**
* Arguments storage.
* Used JCommander library.
*
* @see <a href="http://jcommander.org/">http://jcommander.org/</a>
*/
public final class Arguments {
private static final Arguments INSTANCE = new Arguments();

private Arguments() { }

public static Arguments getInstance() {
return INSTANCE;
}

@Parameter(names = {"--query", "-q"},
required = true,
description = "Query or keywords for stream")
private List<String> keywords;

@Parameter(names = {"--place", "-p"},
required = true,
description = "Location for search tweets")
private String place;

@Parameter(names = {"--stream", "-s"},
description = "Stream mode when tweets printed with delay")
private boolean streamMode;

@Parameter(names = "--hideRetweets",
description = "Hides retweets at all")
private boolean hideRetweets;

@Parameter(names = {"--limit", "-l"},
description = "Limits the number of printed tweets")
private Integer limitOfTweets = Constants.NO_TWEETS_LIMIT;

@Parameter(names = {"--help", "-h"},
help = true,
description = "Requests the help page")
private boolean helpRequest;

@Parameter(names = {"--verbose", "-v"},
description = "Verbose mode to print more information")
private boolean verbose;

/**
* Array of keywords in case of tweets stream requested.
* @return array of keywords to be tracked
*/
public String[] getKeywords() {
String[] keywordsArray = new String[keywords.size()];
return keywords.toArray(keywordsArray);
}

/**
* Suppose that query for Search tweets is element with 0 index.
* @return search tweets query
*/
public String getQuery() {
return keywords.get(0);
}

public String getPlace() {
return place;
}

public boolean isStreamMode() {
return streamMode;
}

public boolean hideRetweets() {
return hideRetweets;
}

public Integer getLimitOfTweets() {
return limitOfTweets;
}

public boolean isHelpRequest() {
return helpRequest;
}

public boolean isVerboseMode() {
return verbose;
}

@Override
public String toString() {
return "Arguments{"
+ "keywords=" + keywords
+ ", place='" + place
+ '\''
+ ", streamMode=" + streamMode
+ ", hideRetweets=" + hideRetweets
+ ", limitOfTweets=" + limitOfTweets
+ ", helpRequest=" + helpRequest
+ ", verboseMode=" + verbose
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config;

/**
* Application constants storage.
*/
public final class Constants {
/**
* Reconnect timeout to twitter in seconds .
*/
public static final int RECONNECT_TIMEOUT_SECS = 10;
/**
* Default value of limit argument.
*/
public static final int NO_TWEETS_LIMIT = -1;
/**
* Delay between two printing of tweets.
*/
public static final int PRINT_TWEET_DELAY_SECS = 1;
/**
* Message which is printed in verbose mode when no any tweet to print for stream.
*/
public static final String NO_TWEET_MESSAGE = "...";
/**
* Name of resource - twitter config file.
*/
public static final String TWITTER_CONFIG_FILE = "twitter.cfg";
/**
* Name of resource - help content file.
*/
public static final String HELP_FILE = "help.txt";
}
Loading