Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 0444f9db275d2e81872d6a3f4f83ff47142b6852 @jexp committed Jul 27, 2012
@@ -0,0 +1,8 @@
+*.db
+target
+*.iml
+*.ipr
+*.iws
+.idea
+.DS_Store
+
43 pom.xml
@@ -0,0 +1,43 @@
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.neo4j</groupId>
+ <artifactId>twitter-graph</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <name>Twitter-Graph</name>
+ <description>
+ </description>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.neo4j</groupId>
+ <artifactId>neo4j</artifactId>
+ <version>1.8.M06</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-neo4j</artifactId>
+ <version>2.1.0.RC2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>3.1.0.RELEASE</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.social</groupId>
+ <artifactId>spring-social-twitter</artifactId>
+ <version>1.0.2.RELEASE</version>
+ </dependency>
+ </dependencies>
+ <repositories>
+ <repository>
+ <id>spring</id>
+ <url>http://repo.springsource.org/libs-milestones</url>
+ </repository>
+ </repositories>
+</project>
@@ -0,0 +1,57 @@
+package org.neo4j.twitter_graph;
+
+import org.neo4j.twitter_graph.domain.Tweet;
+import org.neo4j.twitter_graph.services.TwitterService;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author mh
+ * @since 25.07.12
+ */
+public class TwitterGraph {
+ public static void main(String[] args) throws InterruptedException {
+ if (args.length < 2) {
+ System.out.println("Usage: TwitterGraph embedded|server #tag");
+ return;
+ }
+ final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:TwitterGraph-"+args[0]+".xml");
+ addShutdownHook(ctx);
+ final TwitterService service = ctx.getBean(TwitterService.class);
+ Long lastTweetId=args.length==3 ? Long.parseLong(args[2]) : null;
+ while (true) {
+ final String search = args[1];
+ final List<Tweet> tweets = service.importTweets(search,lastTweetId);
+ if (!tweets.isEmpty()) {
+ lastTweetId = maxTweetId(tweets);
+ service.connectFollowers();
+ }
+ Thread.sleep(TimeUnit.MINUTES.toMillis(5));
+ }
+ }
+
+ private static Long maxTweetId(List<Tweet> tweets) {
+ final Tweet maxTweet = Collections.max(tweets, new TweetComparator());
+ return maxTweet!=null ? maxTweet.getTweetId() : null;
+ }
+
+ private static void addShutdownHook(final ClassPathXmlApplicationContext ctx) {
+ Runtime.getRuntime().addShutdownHook(new Thread(){
+ @Override
+ public void run() {
+ ctx.close();
+ }
+ });
+ }
+
+ private static class TweetComparator implements Comparator<Tweet> {
+ public int compare(Tweet o1, Tweet o2) {
+ return o1.getTweetId().compareTo(o2.getTweetId());
+ }
+ }
+}
@@ -0,0 +1,17 @@
+package org.neo4j.twitter_graph.domain;
+
+import org.springframework.data.neo4j.annotation.EndNode;
+import org.springframework.data.neo4j.annotation.GraphId;
+import org.springframework.data.neo4j.annotation.RelationshipEntity;
+import org.springframework.data.neo4j.annotation.StartNode;
+
+/**
+ * @author mh
+ * @since 26.07.12
+ */
+@RelationshipEntity
+public class Follows {
+ @GraphId Long id;
+ @StartNode User follower;
+ @EndNode User user;
+}
@@ -0,0 +1,33 @@
+package org.neo4j.twitter_graph.domain;
+
+import org.neo4j.graphdb.Direction;
+import org.springframework.data.neo4j.annotation.*;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author mh
+ * @since 24.07.12
+ */
+@NodeEntity
+public class Tag {
+ @GraphId Long id;
+ @Indexed(unique=true) String tag;
+
+ public Tag() {
+ }
+
+ public Tag(String tag) {
+ this.tag = tag;
+ }
+
+ public String getTag() {
+ return tag;
+ }
+
+ @Override
+ public String toString() {
+ return "#"+ tag;
+ }
+}
@@ -0,0 +1,76 @@
+package org.neo4j.twitter_graph.domain;
+
+import org.springframework.data.neo4j.annotation.*;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author mh
+ * @since 24.07.12
+ */
+@NodeEntity
+public class Tweet {
+ @GraphId Long id;
+
+ @Indexed(unique=true) Long tweetId;
+
+ String text;
+
+ @Fetch User sender;
+ @Fetch @RelatedTo(type="TAG") Collection<Tag> tags=new HashSet<Tag>();
+ @Fetch @RelatedTo(type="MENTION") private Set<User> mentions=new HashSet<User>();
+ @Fetch @RelatedTo(type="SOURCE") private Tweet source;
+
+ public Tweet() {
+ }
+
+ public Tweet(long tweetId, User sender, String text) {
+ this.tweetId = tweetId;
+ this.sender = sender;
+ this.text = text;
+ }
+
+ public void addMention(User mention) {
+ this.mentions.add(mention);
+ }
+ public Long getId() {
+ return id;
+ }
+
+ public Long getTweetId() {
+ return tweetId;
+ }
+
+ public User getSender() {
+ return sender;
+ }
+
+ @Override
+ public String toString() {
+ return "Tweet " + tweetId +
+ ": " + text +
+ " by " + sender;
+ }
+
+ public Set<User> getMentions() {
+ return mentions;
+ }
+
+ public Collection<Tag> getTags() {
+ return tags;
+ }
+
+ public void addTag(Tag tag) {
+ tags.add(tag);
+ }
+
+ public void setSource(Tweet source) {
+ this.source = source;
+ }
+
+ public Tweet getSource() {
+ return source;
+ }
+}
@@ -0,0 +1,37 @@
+package org.neo4j.twitter_graph.domain;
+
+import org.springframework.data.neo4j.annotation.*;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author mh
+ * @since 24.07.12
+ */
+@NodeEntity
+public class User {
+ @GraphId Long id;
+
+ @Indexed(unique=true) String user;
+
+ public User() {
+ }
+
+ public User(String user) {
+ this.user = user;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ @Override
+ public String toString() {
+ return "@"+ user;
+ }
+}
@@ -0,0 +1,11 @@
+package org.neo4j.twitter_graph.repositories;
+
+import org.neo4j.twitter_graph.domain.Tag;
+import org.springframework.data.neo4j.repository.GraphRepository;
+
+/**
+ * @author mh
+ * @since 24.07.12
+ */
+public interface TagRepository extends GraphRepository<Tag> {
+}
@@ -0,0 +1,15 @@
+package org.neo4j.twitter_graph.repositories;
+
+import org.neo4j.twitter_graph.domain.Tweet;
+import org.springframework.data.neo4j.repository.GraphRepository;
+
+import java.util.Collection;
+
+/**
+ * @author mh
+ * @since 24.07.12
+ */
+public interface TweetRepository extends GraphRepository<Tweet> {
+ Tweet findByTweetId(Long id);
+ Collection<Tweet> findByTagsTag(String tag);
+}
@@ -0,0 +1,21 @@
+package org.neo4j.twitter_graph.repositories;
+
+import org.neo4j.twitter_graph.domain.User;
+import org.springframework.data.neo4j.annotation.Query;
+import org.springframework.data.neo4j.repository.GraphRepository;
+import org.springframework.data.neo4j.repository.RelationshipOperationsRepository;
+
+import java.util.List;
+
+/**
+ * @author mh
+ * @since 24.07.12
+ */
+public interface UserRepository extends GraphRepository<User>, RelationshipOperationsRepository<User> {
+
+ @Query("START me=node:User(userName={0})" +
+ "MATCH me-[:POSTED]->tweet-[:MENTIONS]->user" +
+ "WHERE user.country = ”SE” and not me-[:FOLLOWS]->user" +
+ "RETURN user")
+ List<User> suggestFriends(User user);
+}
Oops, something went wrong.

0 comments on commit 0444f9d

Please sign in to comment.