List of usage examples for twitter4j Status isRetweet
boolean isRetweet();
From source file:org.zoneproject.extractor.twitterreader.TwitterApi.java
License:Open Source License
/** * create an item by his twitter Status description, will add hashtags and others "metas" * @param s the twitter Status/* www . j a va2 s . c o m*/ * @param source the Uri of the source * @return the item created */ private static Item getItemFromStatus(Status s, String source) { String text = s.getText(); if (s.isRetweet()) { text = s.getRetweetedStatus().getText(); } Item res = new Item(source, "https://twitter.com/" + s.getUser().getScreenName() + "/status/" + Long.toString(s.getId()), text, text, s.getCreatedAt()); String[] hashtags = getHashTags(res.getDescription()); for (String hashtag : hashtags) { res.addProp(new Prop(ZoneOntology.PLUGIN_TWITTER_HASHTAG, hashtag, true, true)); } for (String mentioned : TwitterApi.extractor.extractMentionedScreennames(s.getText())) { res.addProp(new Prop(ZoneOntology.PLUGIN_TWITTER_MENTIONED, "@" + mentioned, true, true)); } if (s.getGeoLocation() != null) { res.addProp(new Prop(ZoneOntology.PLUGIN_TWITTER_POSITION_LONGITUDE, Double.toString(s.getGeoLocation().getLongitude()), true, true)); res.addProp(new Prop(ZoneOntology.PLUGIN_TWITTER_POSITION_LATITUDE, Double.toString(s.getGeoLocation().getLatitude()), true, true)); } res.addProp(new Prop(ZoneOntology.PLUGIN_TWITTER_AUTHOR, "@" + s.getUser().getScreenName(), true, true)); return res; }
From source file:twittermongodbapp.SaveTweets.java
static public void saveTweets(DB db, twitter4j.Twitter twitter) throws TwitterException { DBCollection collection = db.getCollection("collection2"); DBCollection savedCollection = db.getCollection("savedCollection"); DBCollection ConnectionCollection = db.getCollection(" ConnectionCollection"); BasicDBObject document0 = new BasicDBObject(); savedCollection.remove(document0);/* w ww. jav a 2 s. c om*/ ConnectionCollection.remove(document0); Status savedTweet; //Read Tweets from Database System.out.println("Saved tweets: "); DBCursor cursor = collection.find(); List<String> allUsers = new ArrayList<>(); int count = 1; while (cursor.hasNext()) { DBObject obj = cursor.next(); savedTweet = TwitterObjectFactory.createStatus(obj.toString()); String name = savedTweet.getUser().getScreenName(); String date = savedTweet.getCreatedAt().toString(); boolean exist = false; BasicDBObject document = new BasicDBObject("User", name); BasicDBObject searchDocument = new BasicDBObject("User", name); DBObject theObj = null; if (!allUsers.contains(name)) { allUsers.add(name); } else { exist = true; DBCursor cursor2 = savedCollection.find(searchDocument); if (cursor2.hasNext()) { theObj = cursor2.next(); } } // BasicDBList allHashtags = new BasicDBList(); List<String> allHashtags = new ArrayList<>(); List<String> allsortURLS = new ArrayList<>(); List<String> allcompleteURLS = new ArrayList<>(); //BasicDBList allURLS = new BasicDBList(); List<String> allmentionedUsers = new ArrayList<>(); List<String> allTweets = new ArrayList<>(); if (exist) { allHashtags = (ArrayList) theObj.get("Hashtags"); } HashtagEntity[] hashtagsEntities = savedTweet.getHashtagEntities(); for (HashtagEntity hashtagsEntitie : hashtagsEntities) { if (!allHashtags.contains(hashtagsEntitie.getText())) { allHashtags.add(hashtagsEntitie.getText()); } saveConnection(date, name, new BasicDBObject("hashtag", hashtagsEntitie.getText()), 1, count, ConnectionCollection); count++; } document.append("Hashtags", allHashtags); if (exist) { allsortURLS = (ArrayList) theObj.get("sortUrls"); allcompleteURLS = (ArrayList) theObj.get("completeUrls"); } URLEntity[] urlEntities = savedTweet.getURLEntities(); if (savedTweet.getURLEntities().length > 0) { for (int i = 0; i < savedTweet.getURLEntities().length; i++) { if (urlEntities[i].getStart() < urlEntities[i].getEnd()) { BasicDBObject document1 = new BasicDBObject("url", urlEntities[i].getURL()); String completeURL = urlEntities[i].getExpandedURL(); document1.append("completeURL", completeURL); if (!allsortURLS.contains(document1)) { allsortURLS.add(urlEntities[i].getURL()); allcompleteURLS.add(urlEntities[i].getExpandedURL()); } saveConnection(date, name, document1, 2, count, ConnectionCollection); count++; } } } document.append("sortUrls", allsortURLS); document.append("completeUrls", allsortURLS); if (exist) { allmentionedUsers = (ArrayList) theObj.get("Mentions"); } UserMentionEntity[] mentionEntities = savedTweet.getUserMentionEntities(); for (UserMentionEntity mentionEntitie : mentionEntities) { BasicDBObject document2 = new BasicDBObject("mentioned_user", mentionEntitie.getText()); if (!allmentionedUsers.contains(mentionEntitie.getText())) { allmentionedUsers.add(mentionEntitie.getText()); } saveConnection(date, name, document2, 3, count, ConnectionCollection); count++; } document.append("Mentions", allmentionedUsers); if (exist) { allTweets = (ArrayList) theObj.get("Tweets"); } String tweetText = " "; if (savedTweet.isRetweet()) { tweetText = savedTweet.getRetweetedStatus().getText(); } else { tweetText = savedTweet.getText(); } BasicDBObject document2 = new BasicDBObject("Tweet", tweetText); if (!allTweets.contains(tweetText)) { allTweets.add(tweetText); } saveConnection(date, name, document2, 4, count, ConnectionCollection); count++; document.append("Tweets", allTweets); if (exist) { savedCollection.remove(searchDocument); } savedCollection.insert(document); } JaccardSimilarity js = new JaccardSimilarity(); List<Map<List<String>, List<Float>>> list = new ArrayList<>();//This is the final list you need Map<List<String>, List<Float>> map1 = new HashMap<>();//This is one instance of the map you want to store in the above map List<String> usersNames = new ArrayList<>(); List<Float> usersResults = new ArrayList<>(); BasicDBObject doc1; BasicDBObject doc2; DBCursor cursor1; DBCursor cursor2; DBObject obj1; DBObject obj2; List<String> tl1 = new ArrayList(); List<String> tl2 = new ArrayList(); float countSimilarity = 0; for (int i = 0; i < allUsers.size(); i++) { for (int j = i + 1; j < allUsers.size(); j++) { //System.out.println(i+" "+j); System.out.print(allUsers.get(i) + " " + allUsers.get(j) + " "); usersNames.add(allUsers.get(i)); usersNames.add(allUsers.get(j)); doc1 = new BasicDBObject("User", allUsers.get(i)); doc2 = new BasicDBObject("User", allUsers.get(j)); cursor1 = savedCollection.find(doc1); cursor2 = savedCollection.find(doc2); if (cursor1.hasNext() && cursor2.hasNext()) { obj1 = cursor1.next(); obj2 = cursor2.next(); tl1 = (ArrayList) obj1.get("Hashtags"); tl2 = (ArrayList) obj2.get("Hashtags"); countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2); usersResults.add(js.findSimilarity(tl1, tl2)); System.out.print(js.findSimilarity(tl1, tl2) + " "); tl1 = (ArrayList) obj1.get("Mentions"); tl2 = (ArrayList) obj2.get("Mentions"); countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2); usersResults.add(js.findSimilarity(tl1, tl2)); System.out.print(js.findSimilarity(tl1, tl2) + " "); tl1 = (ArrayList) obj1.get("Tweets"); tl2 = (ArrayList) obj2.get("Tweets"); countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2); usersResults.add(js.findSimilarity(tl1, tl2)); System.out.print(js.findSimilarity(tl1, tl2) + " "); tl1 = (ArrayList) obj1.get("sortUrls"); tl2 = (ArrayList) obj2.get("sortUrls"); countSimilarity = countSimilarity + js.findSimilarity(tl1, tl2); usersResults.add(js.findSimilarity(tl1, tl2)); System.out.print(js.findSimilarity(tl1, tl2) + " "); usersResults.add((float) (countSimilarity / 4)); System.out.print((float) (countSimilarity / 4) + " "); System.out.println(); countSimilarity = 0; } } map1.put(usersNames, usersResults); } list.add(map1); //System.out.println(savedCollection.count()); /* // Read every Element Example DBCursor cursor2 = savedCollection.find(searchDocument); if (cursor2.hasNext()) { theObj = cursor2.next(); //String l = ( String) cursor2.one().get("exist").toString(); } BasicDBList list = new BasicDBList(); list = (BasicDBList) theObj.get("Hashtags"); BasicDBList l2 = new BasicDBList(); for (int i = 0; i < list.size(); i++) { BasicDBObject bj = (BasicDBObject) list.get(i); System.out.println(bj.getString("hashtag")); } DBCursor cursor2 = savedCollection.find(); List<String> list = new ArrayList(); while (cursor2.hasNext()) { BasicDBObject document2 = (BasicDBObject) cursor2.next(); String bj = (String) document2.get("User"); System.out.println(bj); for (int i = 0; i < list.size(); i++) { // String bj = (String) list.get(i); // System.out.println(bj.getString("hashtag")); System.out.println(bj); } */ }
From source file:uk.ac.susx.tag.method51.twitter.Tweet.java
License:Apache License
public Tweet(Status status) { this();//from w w w. j a v a 2 s. c om created = status.getCreatedAt(); id = status.getId(); text = status.getText(); inReplyToStatusId = status.getInReplyToStatusId(); inReplyToUserId = status.getInReplyToUserId(); originalText = null; retweetId = -1; isTruncated = status.isTruncated(); isRetweet = status.isRetweet(); Status entities = status; if (isRetweet) { Status origTweet = status.getRetweetedStatus(); entities = origTweet; retweetId = origTweet.getId(); originalText = text; text = origTweet.getText(); } StringBuilder sb = new StringBuilder(); for (HashtagEntity e : entities.getHashtagEntities()) { sb.append(e.getText()); sb.append(" "); } hashtags = sb.toString(); sb = new StringBuilder(); for (UserMentionEntity e : entities.getUserMentionEntities()) { sb.append(e.getScreenName()); sb.append(" "); } mentions = sb.toString(); sb = new StringBuilder(); for (URLEntity e : entities.getURLEntities()) { //String url = e.getURL(); String url = e.getExpandedURL(); if (url == null) { url = e.getURL(); if (url != null) { sb.append(url); } } else { sb.append(url); } sb.append(" "); } urls = sb.toString(); sb = new StringBuilder(); //seems to be null if no entries MediaEntity[] mediaEntities = entities.getMediaEntities(); if (mediaEntities != null) { for (MediaEntity e : mediaEntities) { String url = e.getMediaURL(); sb.append(url); sb.append(" "); } mediaUrls = sb.toString(); } else { mediaUrls = ""; } received = new Date(); source = status.getSource(); GeoLocation geoLoc = status.getGeoLocation(); Place place = status.getPlace(); if (geoLoc != null) { geoLong = geoLoc.getLongitude(); geoLat = geoLoc.getLatitude(); } else if (place != null && place.getBoundingBoxCoordinates() != null && place.getBoundingBoxCoordinates().length > 0) { GeoLocation[] locs = place.getBoundingBoxCoordinates()[0]; double avgLat = 0; double avgLon = 0; for (GeoLocation loc : locs) { avgLat += loc.getLatitude(); avgLon += loc.getLongitude(); } avgLat /= locs.length; avgLon /= locs.length; geoLat = avgLat; geoLong = avgLon; } else { geoLong = null; geoLat = null; } twitter4j.User user = status.getUser(); if (user != null) { userId = user.getId(); location = user.getLocation(); screenName = user.getScreenName(); following = user.getFriendsCount(); name = user.getName(); lang = user.getLang(); timezone = user.getTimeZone(); userCreated = user.getCreatedAt(); followers = user.getFollowersCount(); statusCount = user.getStatusesCount(); description = user.getDescription(); url = user.getURL(); utcOffset = user.getUtcOffset(); favouritesCount = user.getFavouritesCount(); this.user = new User(user); } }
From source file:uk.co.flax.ukmp.twitter.UKMPStatusListener.java
License:Apache License
@Override public void onStatus(Status status) { // Skip any DMs or RTs if (!status.getText().startsWith("@") && !status.isRetweet()) { statusQueue.offer(status);/*from w ww. j av a2s. co m*/ } }
From source file:webServices.RestServiceImpl.java
@GET @Path("/findTwittsRest") @Produces({ MediaType.TEXT_PLAIN })//from www . j a va 2 s .com public String twitterSearchRest(@QueryParam("keys") String searchKeys, @QueryParam("sinceId") long sinceId, @QueryParam("maxId") long maxId, @QueryParam("update") boolean update, @QueryParam("location") String location) { final Vector<String> results = new Vector<String>(); String output = ""; long higherStatusId = Long.MIN_VALUE; long lowerStatusId = Long.MAX_VALUE; Query searchQuery = new Query(searchKeys); searchQuery.setCount(50); searchQuery.setResultType(Query.ResultType.recent); if (sinceId != 0) { if (update) { searchQuery.setSinceId(sinceId); } higherStatusId = sinceId; } if (maxId != 0) { if (!update) { searchQuery.setMaxId(maxId); } lowerStatusId = maxId; } if (location != null) { double lat = Double.parseDouble(location.substring(0, location.indexOf(","))); double lon = Double.parseDouble(location.substring(location.indexOf(",") + 1, location.length())); searchQuery.setGeoCode(new GeoLocation(lat, lon), 10, Query.KILOMETERS); } try { QueryResult qResult = twitter.search(searchQuery); for (Status status : qResult.getTweets()) { //System.out.println(Long.toString(status.getId())+" *** "+Long.toString(status.getUser().getId())+" *** "+status.isRetweet()+" *** "+status.isRetweeted()); higherStatusId = Math.max(status.getId(), higherStatusId); lowerStatusId = Math.min(status.getId(), lowerStatusId); if (!status.isRetweet()) { if (status.getGeoLocation() != null) { System.out.println(Long.toString(status.getId()) + "@" + Double.toString(status.getGeoLocation().getLatitude()) + "," + Double.toString(status.getGeoLocation().getLongitude())); results.add(Long.toString(status.getId()) + "@" + Double.toString(status.getGeoLocation().getLatitude()) + "," + Double.toString(status.getGeoLocation().getLongitude())); } else { results.add(Long.toString(status.getId()) + "@null"); } } } } catch (TwitterException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } TwitterResults resultsObj = new TwitterResults(results.toString(), higherStatusId, lowerStatusId); ObjectMapper mapper = new ObjectMapper(); try { output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(resultsObj); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output; }