From 23fdcca69b8ee6e82fb480d7aee34927e05024fb Mon Sep 17 00:00:00 2001 From: Maurice Laveaux Date: Mon, 26 May 2014 16:51:11 +0200 Subject: Small bugfixes to the SearchImpl * Convers ids to longs instead of ints * input and output fiel correctly named * RateLimit is handled better. Signed-off-by: Maurice Laveaux --- src/io/SearchImpl.java | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/io/SearchImpl.java b/src/io/SearchImpl.java index a062589..11effce 100644 --- a/src/io/SearchImpl.java +++ b/src/io/SearchImpl.java @@ -59,7 +59,16 @@ public class SearchImpl implements Search { public SearchImpl(TwitterApi oauth) { this.oauth = oauth; requestQueue = new LinkedBlockingQueue(); - load("queue_cache"); + load("search_cache"); + + if (thread_cached == null) { + thread_cached = new ThreadContainer(); + thread_cached.start(); + } + + if (requestQueue.size() > 0) { + System.out.println("Resumed " + requestQueue.size() + " amount of requests"); + } } @Override @@ -70,11 +79,6 @@ public class SearchImpl implements Search { } requestQueue.offer(new Request(screenName, numberOfTweets)); - - if (thread_cached == null) { - thread_cached = new ThreadContainer(); - thread_cached.start(); - } } @Override @@ -106,8 +110,8 @@ public class SearchImpl implements Search { requestQueue.offer(new Request(line)); } } catch (IOException ex) { - Logger.getLogger(SearchImpl.class.getName()).log(Level.SEVERE, - "load error", ex); + Logger.getLogger(getClass().getName()) + .log(Level.WARNING, "File named {0} did not exist will be created.", filename); } } @@ -123,7 +127,7 @@ public class SearchImpl implements Search { save("search_cache"); } } - + private class Request { public Request(String screenName, int numberOfTweets) { @@ -138,8 +142,8 @@ public class SearchImpl implements Search { String[] args = parse.split(":", 4); this.screenName = args[0]; - this.sinceId = Integer.parseInt(args[1]); - this.maxId = Integer.parseInt(args[2]); + this.sinceId = Long.parseLong(args[1]); + this.maxId = Long.parseLong(args[2]); this.numberOfTweets = Integer.parseInt(args[3]); } @@ -242,19 +246,19 @@ public class SearchImpl implements Search { getLogger().log(Level.SEVERE, "Interrupted while active", ex); } + return; } } } private void executeRequest(Request request) throws InterruptedException { try { + long waittime = 0; + Builder url = oauth.build(USER_TIMELINE_RESOURCE) .param("screen_name", request.screenName); - int count = 200; - if (count >= request.numberOfTweets) { - count = request.numberOfTweets; - } + int count = Math.min(200, request.numberOfTweets); url.param("count", Integer.toString(count)); if (request.maxId > 0) { @@ -275,7 +279,7 @@ public class SearchImpl implements Search { continue; } if (id.getAsLong() <= request.maxId) { - request.maxId = object.get("id").getAsLong(); + request.maxId = id.getAsLong(); } receivedObjects.offer(object); @@ -288,13 +292,17 @@ public class SearchImpl implements Search { } if (result.getRateLimitRemaining() == 0) { - sleep(result.getRateLimitRemainingTime()); + waittime = result.getRateLimitRemainingTime(); } } catch (RateLimitException ex) { requestQueue.offer(request); - sleep(ex.getRateLimitRemainingTime()); + waittime = ex.getRateLimitRemainingTime(); + } + + if (waittime > 0) { + System.out.println("Rate limited, waiting for " + waittime + " ms"); + sleep(waittime); } - } catch (IOException ex) { requestQueue.offer(request); getLogger().log(Level.SEVERE, null, ex); @@ -327,6 +335,7 @@ public class SearchImpl implements Search { } catch (InterruptedException ex) { // requested to stop. getLogger().log(Level.SEVERE, null, ex); + return; } } } -- cgit v1.2.1