use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method removeDeletedNotes.
public void removeDeletedNotes() throws IOException, SAXException, ParserConfigurationException, GRTokenExpiredException {
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
Timing t = new Timing("RemoveDeletedNotes", context);
final int noOfNotesToKeep = getEntryManager().getNoOfNotesToKeep();
long[] noteIds = getStreamIDsFromGR(httpClient, GOOGLE_STATE_CREATED, null, noOfNotesToKeep);
PL.log("EntriesRetriever.getStreamIds(Notes) done.", context);
entryManager.populateTempTable(noteIds);
entryManager.removeDeletedNotes();
t.stop();
} catch (GRAnsweredBadRequestException e) {
throw new IOException("GR: Bad Request.");
} finally {
httpClient.close();
}
}
use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method fetchNewEntries.
int fetchNewEntries(final EntryManager entryManager, final SyncJob job, boolean manualSync) throws ClientProtocolException, IOException, NeedsSessionException, SAXException, IllegalStateException, ParserConfigurationException, FactoryConfigurationError, ReaderAPIException, GRTokenExpiredException {
String originalJobDescription = job.getJobDescription();
Timing t = new Timing("fetchEntries", context);
final FetchContext fetchCtx = new FetchContext();
assertSessionAvailable();
long lastUpdated = entryManager.getGRUpdated();
PL.log("Before querying GR, last updated: " + lastUpdated, context);
boolean incrementalUpdate = lastUpdated > -1l;
final String otUrlParameter = incrementalUpdate ? "&ot=" + (lastUpdated) : "";
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
if (job.isCancelled())
return fetchCtx.countFetchedEntries;
if (!entryManager.syncCurrentlyEnabled(manualSync))
return fetchCtx.countFetchedEntries;
if (getEntryManager().isNewsRobOnlySyncingEnabled()) {
job.setJobDescription("Fetching 'newsrob' articles.");
requestArticlesFromGoogleReader(job, fetchCtx, httpClient, NEWSROB_LABEL, entryManager.getNewsRobSettings().getStorageCapacity(), otUrlParameter + // +
(entryManager.shouldOnlyUnreadArticlesBeDownloaded() ? "&" + EXCLUDE_READ : ""));
// (true
// ?
// "&"
// +
// EXCLUDE_FRIENDS
// :
// "")
} else {
job.setJobDescription("Fetching reading list articles");
requestArticlesFromGoogleReader(job, fetchCtx, httpClient, GOOGLE_STATE_READING_LIST, entryManager.getNewsRobSettings().getStorageCapacity(), otUrlParameter + // +
(entryManager.shouldOnlyUnreadArticlesBeDownloaded() ? "&" + EXCLUDE_READ : ""));
// (true
// ?
// "&"
// +
// EXCLUDE_FRIENDS
// :
// "")
}
if (!entryManager.syncCurrentlyEnabled(manualSync))
return fetchCtx.countFetchedEntries;
job.setJobDescription("Fetching starred articles");
requestArticlesFromGoogleReader(job, fetchCtx, httpClient, GOOGLE_STATE_STARRED, entryManager.getNoOfStarredArticlesToKeep(), otUrlParameter);
if (!entryManager.syncCurrentlyEnabled(manualSync))
return fetchCtx.countFetchedEntries;
if (incrementalUpdate) {
job.setJobDescription("Incrementally updating article states");
performIncrementalUpdate(entryManager, job, fetchCtx, lastUpdated);
} else {
final long minutesSinceLastSync = (System.currentTimeMillis() - entryManager.getLastSyncTime()) / 1000 / 60;
if ((true || minutesSinceLastSync < MIN_EXACT_SYNC_FREQUENCY_MIN) && isLocalStorageCapacityForMoreArticlesAvailable(entryManager)) {
PL.log("EntriesRetriever: articleCount < storageCapacity == true", context);
job.setJobDescription("Fetching more unread articles");
long[] articleIds = fetchStreamIds(entryManager, GOOGLE_STATE_READING_LIST, GOOGLE_STATE_READ);
entryManager.populateTempTable(TempTable.READ, articleIds);
if (!entryManager.syncCurrentlyEnabled(manualSync))
return fetchCtx.countFetchedEntries;
fetchOlderUnreadToMatchCapacity(entryManager, job, fetchCtx);
} else
PL.log("EntriesRetriever: articleCount < storageCapacity == false", context);
}
if (fetchCtx.tempLastUpdated > lastUpdated)
maintainLastUpdated(fetchCtx.tempLastUpdated);
// job.setJobDescription("Resetting articles with submitted notes.");
// entryManager.clearNotesSubmissionStateForAllSubmittedNotes();
t.stop();
} catch (SAXException e) {
throw e;
} catch (FetchCancelledException fce) {
// user interruption -> ignored
} catch (GRAnsweredBadRequestException e) {
throw new IOException("GR: Bad Request.");
} finally {
httpClient.close();
job.setJobDescription(originalJobDescription);
}
PL.log("EntriesRetriever: Count seen entries=" + fetchCtx.countSeenEntries, context);
return fetchCtx.countFetchedEntries;
}
use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method submitSubscribe.
public boolean submitSubscribe(String url2subscribe) throws ReaderAPIException {
Timing t = new Timing("Submit Subscribe", context);
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
HttpPost editApiRequest = new HttpPost(getGoogleHost() + "/reader/api/0/subscription/edit");
// quickadd
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("client", CLIENT_NAME));
nameValuePairs.add(new BasicNameValuePair("ac", "subscribe"));
// quickadd
nameValuePairs.add(new BasicNameValuePair("s", "feed/" + url2subscribe));
HttpResponse resp = submitPostRequest(httpClient, editApiRequest, nameValuePairs, false);
return resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
} catch (Exception e) {
String message = "Problem during submission of subscribe: " + e.getMessage();
Log.e(TAG, message, e);
throw new ReaderAPIException(message, e);
} finally {
httpClient.close();
t.stop();
}
}
use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method getStateChangesFromGR.
private Collection<StateChange> getStateChangesFromGR(long lastUpdated) throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException, GRAnsweredBadRequestException {
Timing t = new Timing("EntriesRetriever.getStateChangesFromGR()", context);
String url = getGoogleHost() + "/reader/api/0/stream/items/ids";
// &s=deleted/user/-/state/com.google/starred";
url += "?s=user/-/state/com.google/starred";
url += "&s=user/-/state/com.google/read";
url += "&s=" + NEWSROB_PINNED_STATE;
// &s=deleted/user/-/state/com.google/read";
url += "&n=10000&ot=" + lastUpdated;
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
HttpRequestBase req = createGRRequest(httpClient, url);
HttpResponse response = executeGRRequest(httpClient, req, true);
throwExceptionWhenNotStatusOK(response);
final List<StateChange> stateChanges = new ArrayList<StateChange>(25);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
DefaultHandler handler = new SimpleStringExtractorHandler() {
private String currentAtomId;
// cache
String googleUserId = null;
@Override
public void receivedString(String localName, String fqn, String s) {
if ("number".equals(localName)) {
long l = Long.parseLong(s);
currentAtomId = TAG_GR_ITEM + U.longToHex(l);
} else if ("string".equals(localName)) {
boolean delete = s.startsWith("delete");
int state = -1;
if (s.endsWith("read"))
state = EntriesRetriever.StateChange.STATE_READ;
else if (s.endsWith("starred"))
state = EntriesRetriever.StateChange.STATE_STARRED;
if (state > -1) {
EntriesRetriever.StateChange sc = new EntriesRetriever.StateChange(currentAtomId, state, delete ? EntriesRetriever.StateChange.OPERATION_REMOVE : EntriesRetriever.StateChange.OPERATION_ADD);
stateChanges.add(sc);
}
}
}
};
parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
PL.log("Entries Retriever: Number of state changes=" + stateChanges.size(), context);
if (NewsRob.isDebuggingEnabled(context))
PL.log("State Changes: " + stateChanges, context);
return stateChanges;
} finally {
httpClient.close();
t.stop();
}
}
use of com.newsrob.download.NewsRobHttpClient in project newsrob by marianokamp.
the class GRAnsweredBadRequestException method submitNotes.
public void submitNotes(Job job) throws ReaderAPIException {
Timing t = new Timing("Submit Notes", context);
List<Entry> entries = getEntryManager().getEntriesWithNotesToBeSubmitted();
if (entries.isEmpty()) {
t.stop();
return;
}
NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
try {
for (Entry entry : entries) {
HttpPost editApiRequest = new HttpPost(getGoogleHost() + "/reader/api/0/item/edit?client=" + CLIENT_NAME);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("client", CLIENT_NAME));
nameValuePairs.add(new BasicNameValuePair("title", entry.getTitle()));
nameValuePairs.add(new BasicNameValuePair("url", entry.getAlternateHRef()));
String s = "This feed url can only be set after the next article from this feed is imported into NewsRob. You can force this with Clear Cache/Refresh.";
if (entry.getFeedAlternateUrl() != null)
s = entry.getFeedAlternateUrl();
nameValuePairs.add(new BasicNameValuePair("srcUrl", s));
nameValuePairs.add(new BasicNameValuePair("srcTitle", entry.getFeedTitle()));
nameValuePairs.add(new BasicNameValuePair("snippet", entry.getContent()));
submitPostRequest(httpClient, editApiRequest, nameValuePairs, false);
getEntryManager().update(entry);
}
} catch (Exception e) {
String message = "Problem during submission of note: " + e.getMessage();
Log.e(TAG, message, e);
throw new ReaderAPIException(message, e);
} finally {
httpClient.close();
t.stop();
}
}
Aggregations