List of usage examples for twitter4j Status getUser
User getUser();
From source file:adapter.TwitterLocationAdapter.java
License:Apache License
@Override public void run() { while (true) { try {/*ww w.ja v a 2 s . com*/ Status status = this.messageQueue.take(); Event event = new Event(); Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(), status.getUser().getScreenName(), status.getUser().getLang(), status.getUser().getFollowersCount(), status.getUser().getFriendsCount(), status.getHashtagEntities(), status.getFavoriteCount(), status.getRetweetCount(), status.getGeoLocation()); eventCount++; // cantReplicas: Cantidad de PEs que se quieren generar para el // proximo operador // Nota: recuerden que la topologa no necesariamente deba ser // de este estilo // tambin poda ser por un hash int cantReplicas = 10; event.put("levelTweet", Integer.class, eventCount % cantReplicas); event.put("id", Integer.class, eventCount); event.put("tweet", Tweet.class, tweet); getRemoteStream().put(event); } catch (Exception e) { logger.error("Error: " + e); logger.error("Error al crear evento"); } } }
From source file:ai.ilikeplaces.logic.Listeners.ListenerMain.java
License:Open Source License
/** * @param request__//from w ww .j a v a 2s.com * @param response__ */ @Override public void processRequest(final ItsNatServletRequest request__, final ItsNatServletResponse response__) { new AbstractListener(request__, response__) { protected String location; protected String superLocation; protected Long WOEID; /** * Initialize your document here by appending fragments */ @Override @SuppressWarnings("unchecked") @_todo(task = "If location is not available, it should be added through a w" + "id" + "get(or fragment maybe?)") protected final void init(final ItsNatHTMLDocument itsNatHTMLDocument__, final HTMLDocument hTMLDocument__, final ItsNatDocument itsNatDocument__, final Object... initArgs) { final SmartLogger sl = SmartLogger.start(Loggers.LEVEL.DEBUG, "Location Page.", 60000, null, true); //this.location = (String) request_.getServletRequest().getAttribute(RBGet.config.getString("HttpSessionAttr.location")); getLocationSuperLocation: { final String[] attr = ((String) request__.getServletRequest() .getAttribute(RBGet.globalConfig.getString(HTTP_SESSION_ATTR_LOCATION))).split("_"); location = attr[0]; if (attr.length == 3) { superLocation = attr[2]; } else { superLocation = EMPTY; } tryLocationId: { try { final String w = request__.getServletRequest().getParameter(Location.WOEID); if (w != null) { WOEID = Long.parseLong(request__.getServletRequest().getParameter(Location.WOEID)); } } catch (final NumberFormatException e) { Loggers.USER_EXCEPTION.error(WRONG_WOEID_FORMAT, e); } } } sl.appendToLogMSG(RETURNING_LOCATION + location + TO_USER); final ResourceBundle gUI = ResourceBundle.getBundle(AI_ILIKEPLACES_RBS_GUI); layoutNeededForAllPages: { setLoginWidget: { try { new SignInOn(request__, $(Main_login_widget), new SignInOnCriteria().setHumanId(new HumanId(getUsername())) .setSignInOnDisplayComponent( SignInOnCriteria.SignInOnDisplayComponent.MOMENTS)) { }; } catch (final Throwable t) { sl.l(ERROR_IN_UC_SET_LOGIN_WIDGET, t); } } SEO: { try { setMainTitle: { $(mainTitle).setTextContent( MessageFormat.format(gUI.getString("woeidpage.title"), location)); } setMetaDescription: { $(mainMetaDesc).setAttribute(MarkupTag.META.content(), MessageFormat.format(gUI.getString("woeidpage.desc"), location)); } } catch (final Throwable t) { sl.l(ERROR_IN_UC_SEO, t); } } signOnDisplayLink: { try { if (getUsername() != null) { $(Main_othersidebar_identity).setTextContent( gUI.getString(AI_ILIKEPLACES_LOGIC_LISTENERS_LISTENER_MAIN_0004) + getUsernameAsValid()); } else { $(Main_othersidebar_identity).setTextContent( gUI.getString(AI_ILIKEPLACES_LOGIC_LISTENERS_LISTENER_MAIN_0005) + location); } } catch (final Throwable t) { sl.l(ERROR_IN_UC_SIGN_ON_DISPLAY_LINK, t); } } setProfileLink: { try { if (getUsername() != null) { $(Main_othersidebar_profile_link).setAttribute(MarkupTag.A.href(), Controller.Page.Profile.getURL()); } else { $(Main_othersidebar_profile_link).setAttribute(MarkupTag.A.href(), Controller.Page.signup.getURL()); } } catch (final Throwable t) { sl.l(ERROR_IN_UC_SET_PROFILE_LINK, t); } } setProfilePhotoLink: { try { if (getUsername() != null) { /** * TODO check for db failure */ String url = DB.getHumanCRUDHumanLocal(true) .doDirtyRHumansProfilePhoto(new HumanId(getUsernameAsValid())) .returnValueBadly(); url = url == null ? null : RBGet.globalConfig.getString(PROFILE_PHOTOS) + url; if (url != null) { $(Main_profile_photo).setAttribute(MarkupTag.IMG.src(), url); //displayBlock($(Main_profile_photo)); } else { //displayNone($(Main_profile_photo)); } } else { //displayNone($(Main_profile_photo)); } } catch (final Throwable t) { sl.l(ERROR_IN_UC_SET_PROFILE_PHOTO_LINK, t); } } } final Return<Location> r; if (WOEID != null) { r = DB.getHumanCRUDLocationLocal(true).doRLocation(WOEID, REFRESH_SPEC); } else { r = new ReturnImpl<Location>(ExceptionCache.UNSUPPORTED_OPERATION_EXCEPTION, "Search Unavailable", false); //r = DB.getHumanCRUDLocationLocal(true).dirtyRLocation(location, superLocation); } if (r.returnStatus() == 0 && r.returnValue() != null) { final Location existingLocation_ = r.returnValue(); GEO: { if (existingLocation_.getLocationGeo1() == null || existingLocation_.getLocationGeo2() == null) { final Client ygpClient = YAHOO_GEO_PLANET_FACTORY .getInstance(RBGet.globalConfig.getString("where.yahooapis.com.v1.place")); final Place place = ygpClient.getPlace(existingLocation_.getLocationId().toString()); existingLocation_.setLocationGeo1(Double.toString(place.getCoordinates().getY())); existingLocation_.setLocationGeo2(Double.toString(place.getCoordinates().getX())); new Thread(new Runnable() { @Override public void run() { DB.getHumanCRUDLocationLocal(true).doULocationLatLng( new Obj<Long>(existingLocation_.getLocationId()), new Obj<Double>(place.getCoordinates().getY()), new Obj<Double>(place.getCoordinates().getX())); } }).run(); } } final Location locationSuperSet = existingLocation_.getLocationSuperSet(); GEO_WIDGET: { new ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Comment(request__, new CommentCriteria(), $(Controller.Page.Main_right_column)) { @Override protected void init(CommentCriteria commentCriteria) { final ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Place place = new ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Place( request__, new PlaceCriteria() //This Place .setPlaceNamePre("Exciting events in ") .setPlaceName(existingLocation_.getLocationName()) .setPlaceLat(existingLocation_.getLocationGeo1()) .setPlaceLng(existingLocation_.getLocationGeo2()) //Parent Place //.setPlaceSuperName(locationSuperSet.getLocationName()) //.setPlaceSuperLat(locationSuperSet.getLocationGeo1()) //.setPlaceSuperLng(locationSuperSet.getLocationGeo2()) //Parent WOEID //.setPlaceSuperWOEID(locationSuperSet.getWOEID().toString()) , $$(CommentIds.commentPerson)); place.$$displayNone(place.$$( ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Place.PlaceIds.placeWidget)); } }; } final List<String> titleManifest = new ArrayList<String>(); EVENTS_WIDGETS: { try { /* final JSONObject jsonObject = Modules.getModules().getYahooUplcomingFactory() .getInstance("http://upcoming.yahooapis.com/services/rest/") .get("", new HashMap<String, String>() { {//Don't worry, this is a static initializer of this map :) put("method", "event.search"); put("woeid", WOEID.toString()); put("format", "json"); } } ); final JSONArray events = jsonObject.getJSONObject("rsp").getJSONArray("event");*/ final JSONObject jsonObject = Modules.getModules().getEventulFactory() .getInstance("http://api.eventful.com/json/events/search/") .get("", new HashMap<String, String>() { {//Don't worry, this is a static initializer of this map :) put("location", "" + existingLocation_.getLocationGeo1() + "," + existingLocation_.getLocationGeo2()); put("within", "" + 100); } } ); Loggers.debug("Eventful Reply:" + jsonObject.toString()); // final String eventName = eventJSONObject.getString("title"); // final String eventUrl = eventJSONObject.getString("url"); // final String eventDate = eventJSONObject.getString("start_time"); // final String eventVenue = eventJSONObject.getString("venue_name"); final JSONArray events = jsonObject.getJSONObject("events").getJSONArray("event"); for (int i = 0; i < events.length(); i++) { final JSONObject eventJSONObject = new JSONObject(events.get(i).toString()); titleManifest.add(eventJSONObject.get("title").toString()); final String photoUrl; String temp = null; try { temp = eventJSONObject.getJSONObject("image").get("url").toString(); } catch (final Throwable e) { SmartLogger.g().l(e.getMessage()); } finally { photoUrl = temp; } //The pain I go through to make variables final :D new Event(request__, new EventCriteria() .setEventName(eventJSONObject.get("title").toString()) .setEventStartDate(eventJSONObject.get("start_time").toString()) .setEventPhoto(photoUrl) .setPlaceCriteria(new PlaceCriteria() .setPlaceNamePre("This event is taking place in ") .setPlaceName(existingLocation_.getLocationName()) .setPlaceLat(eventJSONObject.get("latitude").toString()) .setPlaceLng(eventJSONObject.get("longitude").toString()) //Parent Place .setPlaceSuperNamePre( existingLocation_.getLocationName() + " is located in ") .setPlaceSuperName(locationSuperSet.getLocationName()) .setPlaceSuperLat(locationSuperSet.getLocationGeo1()) .setPlaceSuperLng(locationSuperSet.getLocationGeo2()) //Parent WOEID .setPlaceSuperWOEID(locationSuperSet.getWOEID().toString()) ), $(Controller.Page.Main_right_column)); } } catch (final JSONException e) { sl.l("Error fetching data from Yahoo Upcoming: " + e.getMessage()); } } TWITTER_WIDGETS: { try { final Query _query = new Query( "fun OR happening OR enjoy OR nightclub OR restaurant OR party OR travel :)"); _query.geoCode( new GeoLocation(Double.parseDouble(existingLocation_.getLocationGeo1()), Double.parseDouble(existingLocation_.getLocationGeo2())), 40, Query.MILES); _query.setResultType(Query.MIXED); final QueryResult result = TWITTER.search(_query); //final QueryResult result = TWITTER.search(new Query("Happy").geoCode(new GeoLocation(Double.parseDouble(existingLocation_.getLocationGeo1()), Double.parseDouble(existingLocation_.getLocationGeo2())), 160, Query.MILES)); for (Status tweet : result.getTweets()) { new ai.ilikeplaces.logic.Listeners.widgets.schema.thing.Person(request__, new PersonCriteria().setPersonName(tweet.getUser().getName()) .setPersonPhoto(tweet.getUser().getProfileImageURL()) .setPersonData(tweet.getText()), $(Main_right_column)); titleManifest.add(tweet.getText()); } if (result.getTweets().size() == 0) { sl.l("No twitter results found"); } } catch (final Throwable t) { sl.l("An error occurred during twitter fetch:" + t.getMessage()); } } SEO: { setMetaGEOData: { $(Main_ICBM).setAttribute(MarkupTag.META.content(), existingLocation_.getLocationGeo1() + COMMA + existingLocation_.getLocationGeo2()); $(Main_geoposition).setAttribute(MarkupTag.META.content(), existingLocation_.getLocationGeo1() + COMMA + existingLocation_.getLocationGeo2()); $(Main_geoplacename).setAttribute(MarkupTag.META.content(), existingLocation_.getLocationName()); $(Main_georegion).setAttribute(MarkupTag.META.content(), locationSuperSet.getLocationName()); } } setLocationIdForJSReference: { try { final Element hiddenLocationIdInputTag = $(INPUT); hiddenLocationIdInputTag.setAttribute(INPUT.type(), INPUT.typeValueHidden()); hiddenLocationIdInputTag.setAttribute(INPUT.id(), JSCodeToSend.LocationId); hiddenLocationIdInputTag.setAttribute(INPUT.value(), existingLocation_.getLocationId().toString()); hTMLDocument__.getBody().appendChild(hiddenLocationIdInputTag); $(Main_location_name).setAttribute(INPUT.value(), existingLocation_.getLocationName() + ""); $(Main_super_location_name).setAttribute(INPUT.value(), locationSuperSet.getLocationName() + ""); } catch (final Throwable t) { sl.l(ERROR_IN_UC_SET_LOCATION_ID_FOR_JSREFERENCE, t); } } setLocationNameForJSReference: { try { final Element hiddenLocationIdInputTag = $(INPUT); hiddenLocationIdInputTag.setAttribute(INPUT.type(), INPUT.typeValueHidden()); hiddenLocationIdInputTag.setAttribute(INPUT.id(), JSCodeToSend.LocationName); hiddenLocationIdInputTag.setAttribute(INPUT.value(), existingLocation_.getLocationName() + OF + locationSuperSet.getLocationName()); hTMLDocument__.getBody().appendChild(hiddenLocationIdInputTag); } catch (final Throwable t) { sl.l(ERROR_IN_UC_SET_LOCATION_NAME_FOR_JSREFERENCE, t); } } setLocationAsPageTopic: { try { final StringBuilder title = new StringBuilder(); for (final String titleGuest : titleManifest) { title.append(titleGuest); } final String finalTitle = title.toString(); $(Main_center_main_location_title).setTextContent(finalTitle.isEmpty() ? (THIS_IS + existingLocation_.getLocationName() + OF + locationSuperSet) : finalTitle); for (final Element element : generateLocationLinks(DB.getHumanCRUDLocationLocal(true) .doDirtyRLocationsBySuperLocation(existingLocation_))) { $(Main_location_list).appendChild(element); displayBlock($(Main_notice_sh)); } } catch (final Throwable t) { sl.l(ERROR_IN_UC_SET_LOCATION_AS_PAGE_TOPIC, t); } } } else { noSupportForNewLocations: { try { // $(Main_notice).appendChild(($(P).appendChild( // hTMLDocument__.createTextNode(RBGet.logMsgs.getString("CANT_FIND_LOCATION") // + " Were you looking for " // )))); // for (final Element element : generateLocationLinks(DB.getHumanCRUDLocationLocal(true).dirtyRLikeLocations(location))) { // $(Main_notice).appendChild(element); // displayBlock($(Main_notice_sh)); // } NotSupportingLikeSearchTooForNow: { $(Main_notice).appendChild(($(P).appendChild(hTMLDocument__ .createTextNode(RBGet.logMsgs.getString("CANT_FIND_LOCATION"))))); } } catch (final Throwable t) { sl.l(ERROR_IN_UC_NO_SUPPORT_FOR_NEW_LOCATIONS, t); } } } sl.complete(Loggers.LEVEL.SERVER_STATUS, Loggers.DONE); } /** * Use ItsNatHTMLDocument variable stored in the AbstractListener class */ @Override protected void registerEventListeners(final ItsNatHTMLDocument itsNatHTMLDocument__, final HTMLDocument hTMLDocument__, final ItsNatDocument itsNatDocument__) { } private List<Element> generateLocationLinks(final List<Location> locationList) { final ElementComposer UList = ElementComposer.compose($(UL)).$ElementSetAttribute(MarkupTag.UL.id(), PLACE_LIST); for (Location location : locationList) { final Element link = $(A); link.setTextContent(TRAVEL_TO + location.getLocationName() + OF + location.getLocationSuperSet().getLocationName()); link.setAttribute(A.href(), PAGE + location.getLocationName() + _OF_ + location.getLocationSuperSet().getLocationName() + Parameter.get(Location.WOEID, location.getWOEID().toString(), true)); link.setAttribute(A.alt(), PAGE + location.getLocationName() + _OF_ + location.getLocationSuperSet().getLocationName()); link.setAttribute(A.title(), CLICK_TO_EXPLORE + location.getLocationName() + OF + location.getLocationSuperSet().getLocationName()); link.setAttribute(A.classs(), VTIP); final Element linkDiv = $(DIV); linkDiv.appendChild(link); UList.wrapThis(ElementComposer.compose($(LI)).wrapThis(linkDiv).get()); } final List<Element> elements = new ArrayList<Element>(); elements.add(UList.get()); return elements; } private Element generateLocationLink(final Location location) { final Element link = $(A); link.setTextContent(TRAVEL_TO + location.getLocationName() + OF + location.getLocationSuperSet().getLocationName()); link.setAttribute(A.href(), PAGE + location.getLocationName() + _OF_ + location.getLocationSuperSet().getLocationName() + Parameter.get(Location.WOEID, location.getWOEID().toString(), true)); link.setAttribute(A.alt(), PAGE + location.getLocationName() + _OF_ + location.getLocationSuperSet().getLocationName()); return link; } private Element generateSimpleLocationLink(final Location location) { final Element link = $(A); link.setTextContent(location.getLocationName()); link.setAttribute(A.href(), PAGE + location.getLocationName() + Parameter.get(Location.WOEID, location.getWOEID().toString(), true)); link.setAttribute(A.alt(), PAGE + location.getLocationName()); return link; } };//Listener }
From source file:aic2013.extractor.TwitterExtractor.java
public static void main(String[] args) throws Exception { EntityManagerFactory emf = null;//from w w w. ja v a2 s .co m MongoClient mongoClient = null; Twitter twitter = null; Neo4jConnection neo4j = null; try { // Database configuration Properties prop = new Properties(); prop.load(new FileInputStream("hibernate.properties")); // prop.load(TwitterExtractor.class.getClassLoader().getResourceAsStream("hibernate.properties")); emf = Persistence.createEntityManagerFactory("twitterdb", prop); final String mongoServer = "localhost"; mongoClient = new MongoClient(mongoServer); twitter = TwitterFactory.getSingleton(); // final String neo4jJdbc = "jdbc:neo4j:ec2-54-217-131-208.eu-west-1.compute.amazonaws.com:7474"; final String neo4jJdbc = "jdbc:neo4j://localhost:7474"; neo4j = new Driver().connect(neo4jJdbc, new Properties()); neo4j.setAutoCommit(true);//false); final TwitterDataAccess twitterDataAccess = new TwitterDataAccess(twitter); final MongoDataAccess mongoDataAccess = new MongoDataAccess(mongoClient); final Neo4jService neo4jService = new Neo4jService(neo4j); final EntityManager em = emf.createEntityManager(); final UserDataAccess userDataAcces = new UserDataAccess(em); final TopicExtractionCoordinator extractionCoordinator = new TopicExtractionCoordinatorImpl(); // final ConcurrentLinkedQueue<E> final UserService userService = new UserService(em); final Processor<Status> mongoProcessor = new Processor<Status>() { @Override public void process(final Status status) { final TwitterUser user = new TwitterUser(status.getUser()); userService.persist(user); neo4jService.transactional(new Neo4jUnitOfWork() { @Override public void process() throws SQLException { neo4jService.createPersonIfAbsent(user); // HashtagEntity[] topics = // status.getHashtagEntities(); Status originalStatus = status; if (status.isRetweet()) { originalStatus = status.getRetweetedStatus(); } extractionCoordinator.doExtraction(originalStatus, new TopicExtractionCallback() { @Override public void handleExtractionResult(Set<Topic> extractedTopics) { /* TODO: comment out outputs */ // System.out.println(status.getText()); // for (Topic t : extractedTopics) { // System.out.println(t.getName()); // } try { /* add hash tags to topics */ for (HashtagEntity tag : status.getHashtagEntities()) { extractedTopics.add(new Topic(new String[] { tag.getText() })); } for (Topic topic : extractedTopics) { neo4jService.createTopicIfAbsent(topic); if (status.isRetweet()) { neo4jService.createRelationIfAbsent("RETWEETS", user, topic); } else { neo4jService.createRelationIfAbsent("TWEETS", user, topic); } } } catch (SQLException e) { throw new RuntimeException(e); } } @Override public void handleExtractionError(ExtractionException e) { throw new RuntimeException(e); } }); } }); } }; final Processor<TwitterUser> userProcessor = new Processor<TwitterUser>() { @Override public void process(final TwitterUser user) { try { twitterDataAccess.forAllFollowers(user, new Processor<User>() { @Override public void process(User u) { final TwitterUser follower = em.find(TwitterUser.class, u.getId()); if (follower != null) { neo4jService.transactional(new Neo4jUnitOfWork() { @Override public void process() throws SQLException { neo4jService.createUniqueRelation("FOLLOWS", user, follower); } }); } } }); } catch (Exception ex) { throw new RuntimeException(ex); } } }; // invoke the processor for every entry in mongo mongoDataAccess.forAll(mongoProcessor); extractionCoordinator.awaitTermination(-1, TimeUnit.SECONDS); // invoke the processor for every entry in the rdbms // userDataAcces.forAll(userProcessor); } finally { if (emf != null && emf.isOpen()) { emf.close(); } if (mongoClient != null) { mongoClient.close(); } if (twitter != null) { twitter.shutdown(); } if (neo4j != null) { neo4j.close(); } } }
From source file:apptwitter.Metodos.java
public void verTL() throws TwitterException { List<Status> statuses = twitter.getHomeTimeline(); System.out.println("Showing home timeline."); for (Status status : statuses) { System.out.println(status.getUser().getName() + ":" + status.getText()); }//from w ww. j av a 2 s.co m }
From source file:apptwitter.Metodos.java
public void buscarTrending(String busqueda) throws TwitterException { Query query = new Query(busqueda); QueryResult result = twitter.search(query); for (Status status : result.getTweets()) { System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); }//from www .jav a2 s . c o m }
From source file:au.edu.anu.portal.portlets.tweetal.servlet.TweetalServlet.java
License:Apache License
private JSONObject getStatusJSON(Twitter twitter, Status status) throws TwitterException { JSONObject result = new JSONObject(); result.put("tweetId", Long.toString(status.getId())); result.put("isOwnTweet", status.getUser().getId() == twitter.getId()); result.put("createdAt", status.getCreatedAt().toString()); result.put("screenName", status.getUser().getScreenName()); result.put("name", status.getUser().getName()); result.put("profileImageURL", status.getUser().getProfileImageURL().toString()); String statusText = status.getText(); // escape html to prevent XSS attack statusText = StringEscapeUtils.escapeHtml(statusText); result.put("statusText", statusText); // if this tweet addresses the current user result.put("isMentionedMe", StringUtils.contains(statusText, "@" + twitter.getScreenName())); // replace links with link tags statusText = replaceLinks(statusText); // replace @username with link to the user page statusText = replaceUserReference(statusText); // replace #hashtag with link statusText = replaceHashTag(statusText); result.put("statusTextFormatted", statusText); return result; }
From source file:au.net.moon.tGetMissingUsers.ReadTwitter.java
License:Open Source License
/** * Get the user by getting a tweet with the user embedded in it. * /* www .j ava 2s .c om*/ * @param tweetId * the tweet to retrieve * @return Twitter user object */ public TUser getUserFromTweet(long tweetId) { TUser user = null; try { Status tweet = twitter.showStatus(tweetId); user = new TUser(tweet.getUser()); } catch (TwitterException e) { if (e.getStatusCode() == 404 || e.getStatusCode() == 403) { // 404 = tweet not found // 403 = tweet protected or user account suspended // just return the null user } else { e.printStackTrace(); } } return user; }
From source file:au.net.moon.tSearchArchiver.SearchArchiver.java
License:Open Source License
SearchArchiver() { Twitter twitter;//from w ww. ja v a 2s . c o m int waitBetweenRequests = 2000; // 2 sec delay between requests to avoid maxing out the API. Status theTweet; Query query; QueryResult result; // String[] searches; ArrayList<String> searchQuery = new ArrayList<String>(); ArrayList<Integer> searchId = new ArrayList<Integer>(); int searchIndex; int totalTweets; SimpleDateFormat myFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); System.out.println("tSearchArchiver: Loading search queries..."); // Set timezone to UTC for the Twitter created at dates myFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); twitterAuthorise twitterAuth = new twitterAuthorise(false); twitter = twitterAuth.getTwitter(); // Open the old twitter_archive database openSQLDataBase(); if (isDatabaseReady()) { // probably should have these in an object not separate arrays? try { rs = stmt.executeQuery("select * from searches where active = true"); // perform each search while (rs.next()) { // if (searchQuery searchQuery.add(rs.getString("query")); searchId.add(rs.getInt("id")); } if (rs.wasNull()) { System.out.println("tSearchArchiver: No searches in the table \"searches\""); System.exit(30); } else { System.out.println("tSearchArchiver: Found " + searchQuery.size() + " searches."); } } catch (SQLException e) { System.out.println("tSearchArchiver: e:" + e.toString()); } searchIndex = 0; totalTweets = 0; // set initial value of i to start from middle of search set while (searchIndex < searchQuery.size()) { query = new Query(); query.setQuery(searchQuery.get(searchIndex)); // check to see if their are any tweets already in the database for // this search //TODO: Change this to look in new raw data files for each search instead long max_tw_id = 0; try { rs = stmt.executeQuery("select max(tweet_id) as max_id from archive where search_id = " + searchId.get(searchIndex)); if (rs.next()) { max_tw_id = rs.getLong("max_id"); // System.out.println("MaxID: " + max_tw_id); query.setSinceId(max_tw_id); } } catch (SQLException e1) { System.err.println("tSearchArchiver: Error looking for maximum tweet_id for " + query.getQuery() + " in archive"); e1.printStackTrace(); } // System.out.println("Starting searching for tweets for: " + // query.getQuery()); // new style replacement for pagination // Query query = new Query("whatEverYouWantToSearch"); // do { // result = twitter.search(query); // System.out.println(result); // do something // } while ((query = result.nextQuery()) != null); // TODO: check if twitter4j is doing all the backing off handling already int tweetCount = 0; Boolean searching = true; do { // delay waitBetweenRequests milliseconds before making request // to make sure not overloading API try { Thread.sleep(waitBetweenRequests); } catch (InterruptedException e1) { System.err.println("tSearchArchiver: Sleep between requests failed."); e1.printStackTrace(); } try { result = twitter.search(query); } catch (TwitterException e) { System.out.println(e.getStatusCode()); System.out.println(e.toString()); if (e.getStatusCode() == 503) { // TODO use the Retry-After header value to delay & then // retry the request System.out .println("tSearchArchiver: Delaying for 10 minutes before making new request"); try { Thread.sleep(600000); } catch (InterruptedException e1) { System.err.println( "tSearchArchiver: Sleep for 10 minutes because of API load failed."); e1.printStackTrace(); } } result = null; } if (result != null) { List<Status> results = result.getTweets(); if (results.size() == 0) { searching = false; } else { tweetCount += results.size(); for (int j = 0; j < results.size(); j++) { theTweet = (Status) results.get(j); String cleanText = theTweet.getText(); cleanText = cleanText.replaceAll("'", "'"); cleanText = cleanText.replaceAll("\"", """); try { stmt.executeUpdate("insert into archive values (0, " + searchId.get(searchIndex) + ", '" + theTweet.getId() + "', now())"); } catch (SQLException e) { System.err.println("tSearchArchiver: Insert into archive failed."); System.err.println(searchId.get(searchIndex) + ", " + theTweet.getId()); e.printStackTrace(); } // TODO: change to storing in file instead of database try { rs = stmt.executeQuery("select id from tweets where id = " + theTweet.getId()); } catch (SQLException e) { System.err.println( "tSearchArchiver: checking for tweet in tweets archive failed."); e.printStackTrace(); } Boolean tweetNotInArchive = false; try { tweetNotInArchive = !rs.next(); } catch (SQLException e) { System.err.println( "tSearchArchiver: checking for tweet in archive failed at rs.next()."); e.printStackTrace(); } if (tweetNotInArchive) { String tempLangCode = ""; // getIsoLanguageCode() has been removed from twitter4j // looks like it might be added back in in the next version // if (tweet.getIsoLanguageCode() != null) { // if (tweet.getIsoLanguageCode().length() > 2) { // System.out // .println("tSearchArchiver Error: IsoLanguageCode too long: >" // + tweet.getIsoLanguageCode() // + "<"); // tempLangCode = tweet // .getIsoLanguageCode() // .substring(0, 2); // } else { // tempLangCode = tweet // .getIsoLanguageCode(); // } // } double myLatitude = 0; double myLongitude = 0; int hasGeoCode = 0; if (theTweet.getGeoLocation() != null) { System.out.println("GeoLocation: " + theTweet.getGeoLocation().toString()); myLatitude = theTweet.getGeoLocation().getLatitude(); myLongitude = theTweet.getGeoLocation().getLongitude(); hasGeoCode = 1; } Date tempCreatedAt = theTweet.getCreatedAt(); String myDate2 = myFormatter .format(tempCreatedAt, new StringBuffer(), new FieldPosition(0)) .toString(); totalTweets++; try { stmt.executeUpdate("insert into tweets values (" + theTweet.getId() + ", '" + tempLangCode + "', '" + theTweet.getSource() + "', '" + cleanText + "', '" + myDate2 + "', '" + theTweet.getInReplyToUserId() + "', '" + theTweet.getInReplyToScreenName() + "', '" + theTweet.getUser().getId() + "', '" + theTweet.getUser().getScreenName() + "', '" + hasGeoCode + "'," + myLatitude + ", " + myLongitude + ", now())"); } catch (SQLException e) { System.err.println("tSearchArchiver: Insert into tweets failed."); System.err.println(theTweet.getId() + ", '" + tempLangCode + "', '" + theTweet.getSource() + "', '" + cleanText + "', '" + myDate2 + "', '" + theTweet.getInReplyToUserId() + "', '" + theTweet.getInReplyToScreenName() + "', '" + theTweet.getUser().getId() + "', '" + theTweet.getUser().getScreenName()); e.printStackTrace(); } } } } } } while ((query = result.nextQuery()) != null && searching); if (tweetCount > 0) { System.out.println("tSearchArchiver: New Tweets Found for \"" + searchQuery.get(searchIndex) + "\" = " + tweetCount); } else { // System.out.println("tSearchArchiver: No Tweets Found for \"" // + searchQuery.get(searchIndex) + "\" = " + tweetCount); } try { stmt.executeUpdate("update searches SET lastFoundCount=" + tweetCount + ", lastSearchDate=now() where id=" + searchId.get(searchIndex)); } catch (SQLException e) { System.err.println("tSearchArchiver: failed to update searches with lastFoundCount=" + tweetCount + " and datetime for search: " + searchId.get(searchIndex)); e.printStackTrace(); } searchIndex++; } System.out.println("tSearchArchiver: Completed all " + searchQuery.size() + " searches"); System.out.println("tSearchArchiver: Archived " + totalTweets + " new tweets"); } }
From source file:au.net.moon.tUtils.twitterFields.java
License:Open Source License
/** * Get a <CODE>HashMap</CODE> of tweet fields by parsing a twitter4j Status * /* ww w. j a v a2s . com*/ * @param status * the twitter4j Status object * @return the tweet fields as name, value pairs in a <CODE>HashMap</CODE>. */ public static HashMap<String, String> parseStatusObj(Status status) { HashMap<String, String> splitFields = new HashMap<String, String>(); splitFields.put("createdAt", status.getCreatedAt().toString()); splitFields.put("id", Long.toString(status.getId())); splitFields.put("text", status.getText()); splitFields.put("source", status.getSource()); splitFields.put("isTruncated", status.isTruncated() ? "1" : "0"); splitFields.put("inReplyToStatusId", Long.toString(status.getInReplyToStatusId())); splitFields.put("inReplyToUserId", Long.toString(status.getInReplyToUserId())); splitFields.put("isFavorited", status.isFavorited() ? "1" : "0"); splitFields.put("inReplyToScreenName", status.getInReplyToScreenName()); if (status.getGeoLocation() != null) { splitFields.put("geoLocation", status.getGeoLocation().toString()); } else { splitFields.put("geoLocation", ""); } if (status.getPlace() != null) { splitFields.put("place", status.getPlace().toString()); } else { splitFields.put("place", ""); } splitFields.put("retweetCount", Long.toString(status.getRetweetCount())); splitFields.put("wasRetweetedByMe", status.isRetweetedByMe() ? "1" : "0"); String contributors = ""; if (status.getContributors() != null) { long[] tempContributors = status.getContributors(); for (int i = 0; i < tempContributors.length; i++) { contributors += Long.toString(tempContributors[i]); if (i != tempContributors.length - 1) { contributors += ", "; } } } splitFields.put("contributors", contributors); splitFields.put("annotations", ""); if (status.getRetweetedStatus() != null) { splitFields.put("retweetedStatus", "1"); } else { splitFields.put("retweetedStatus", "0"); } splitFields.put("userMentionEntities", status.getUserMentionEntities().toString()); splitFields.put("urlEntities", status.getURLEntities().toString()); splitFields.put("hashtagEntities", status.getHashtagEntities().toString()); splitFields.put("user", status.getUser().toString()); return splitFields; }
From source file:au.net.moon.tUtils.WriteToTwitterStreamArchiveSQL.java
License:Open Source License
/** * Add tweet into database from twitter4j.statusObj. (Used by * TwitterDiskToSQL)//from ww w . ja v a2 s. c o m * * @param tweetString * a json string tweet */ public void tweetToSQL(Status statusObj) { HashMap<String, String> tweet = twitterFields.parseStatusObj(statusObj); TUser user = new TUser(statusObj.getUser()); tweetToSQL(tweet, user); }