Search in sources :

Example 1 with NewsRobHttpClient

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();
    }
}
Also used : NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) Timing(com.newsrob.util.Timing) IOException(java.io.IOException)

Example 2 with NewsRobHttpClient

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;
}
Also used : NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) Timing(com.newsrob.util.Timing) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 3 with NewsRobHttpClient

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();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) Timing(com.newsrob.util.Timing) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(java.text.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with NewsRobHttpClient

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();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) DefaultHandler(org.xml.sax.helpers.DefaultHandler) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 5 with NewsRobHttpClient

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();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) ArrayList(java.util.ArrayList) Timing(com.newsrob.util.Timing) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(java.text.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

NewsRobHttpClient (com.newsrob.download.NewsRobHttpClient)12 Timing (com.newsrob.util.Timing)9 ArrayList (java.util.ArrayList)8 HttpResponse (org.apache.http.HttpResponse)7 IOException (java.io.IOException)6 HttpPost (org.apache.http.client.methods.HttpPost)6 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 SAXException (org.xml.sax.SAXException)4 SimpleStringExtractorHandler (com.newsrob.util.SimpleStringExtractorHandler)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MalformedURLException (java.net.MalformedURLException)3 ParseException (java.text.ParseException)3 ExecutionException (java.util.concurrent.ExecutionException)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 ClientProtocolException (org.apache.http.client.ClientProtocolException)3 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)3 DefaultHandler (org.xml.sax.helpers.DefaultHandler)3