예제 #1
0
  private void parseItemIdList(Reader in, IItemIdListHandler handler)
      throws JsonParseException, IOException, RemoteException {
    JsonFactory f = new JsonFactory();
    JsonParser jp = f.createParser(in);

    String currName;

    List<String> idList = new ArrayList<String>();

    jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
    while (jp.nextToken() != JsonToken.END_OBJECT) {
      currName = jp.getCurrentName();
      jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
      if (currName.equals("itemRefs")) { // contains an object
        // start items
        while (jp.nextToken() != JsonToken.END_ARRAY) {
          currName = jp.getCurrentName();
          if (currName == null) continue;
          jp.nextToken();
          if (currName.equals("id")) {
            idList.add(Utils.dec2Hex(jp.getText())); // convert dec to hex
          } else {
            jp.skipChildren();
          }
        }

        handler.items(idList);

      } else {
        jp.skipChildren();
      }
    }
  }
예제 #2
0
  // NOTE: /api/0/stream/contents
  private Reader readStreamContents(
      long syncTime, long startTime, IItemListHandler handler, String continuation)
      throws IOException, ReaderException, RemoteException {
    initAuth();

    StringBuilder buff = new StringBuilder(URL_API_STREAM_CONTENTS.length() + 128);
    buff.append(URL_API_STREAM_CONTENTS);
    String stream = handler.stream();
    if (stream != null) {
      if (stream.equals(STATE_READING_LIST)) stream = STATE_GOOGLE_READING_LIST;
      else if (stream.equals(STATE_STARRED)) stream = STATE_GOOGLE_STARRED;
      buff.append("/").append(Utils.encode(stream));
    }
    buff.append("?client=newsplus&ck=").append(syncTime);
    if (handler.excludeRead()) {
      buff.append("&xt=").append(STATE_GOOGLE_READ);
    }
    if (continuation != null && continuation.length() > 0) {
      buff.append("&c=").append(continuation);
    }
    if (startTime > 0) {
      buff.append("&ot=").append(startTime);
    }
    int limit = handler.limit();
    limit = (limit > SYNC_MAX_ITEMS || limit == 0) ? SYNC_MAX_ITEMS : limit;
    if (limit > 0) {
      // google only allows max 1000 at once
      buff.append("&n=").append(limit > SYNC_MAX_ITEMS ? SYNC_MAX_ITEMS : limit);
    }
    buff.append("&r=").append(handler.newestFirst() ? "n" : "o");

    return doGetReader(buff.toString());
  }
예제 #3
0
  public InputStream doPostInputStream(String url, List<NameValuePair> params)
      throws IOException, ReaderException {
    //		Log.d(TAG, "[DEBUG] POST: " + url);
    //		Log.d(TAG, "[DEBUG] PARAMS: " + params);
    //		Log.d(TAG, "[DEBUG] Authorization: " + "GoogleLogin auth=" + this.auth);
    HttpPost post = new HttpPost(url);
    post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

    if (this.auth != null) post.setHeader("Authorization", "GoogleLogin auth=" + this.auth);

    // gzip
    post.setHeader("User-agent", "gzip");
    post.setHeader("Accept-Encoding", "gzip");

    HttpResponse res = getClient().execute(post);
    int resStatus = res.getStatusLine().getStatusCode();

    if (resStatus == HttpStatus.SC_FORBIDDEN) throw new ReaderLoginException("Login failure");
    else if (resStatus == HttpStatus.SC_UNAUTHORIZED)
      throw new ReaderLoginException("Authentication fails (" + resStatus + "): " + url);
    else if (resStatus != HttpStatus.SC_OK) {
      throw new ReaderException("Invalid http status " + resStatus + ": " + url);
    }

    final HttpEntity entity = res.getEntity();
    if (entity == null) {
      throw new ReaderException("null response entity");
    }

    InputStream is = null;

    // create the appropriate stream wrapper based on the encoding type
    String encoding = Utils.getHeaderValue(entity.getContentEncoding());
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
      is = new GZIPInputStream(entity.getContent());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
      is = new InflaterInputStream(entity.getContent(), new Inflater(true));
    } else {
      is = entity.getContent();
    }

    return new FilterInputStream(is) {
      @Override
      public void close() throws IOException {
        super.close();
        entity.consumeContent();
      }
    };
  }
예제 #4
0
 public String getUserId() throws IOException, ReaderException {
   String userId = null;
   InputStream in = doGetInputStream(URL_API_USER_INFO);
   try {
     String content = Utils.convertStreamToString(in);
     if (TextUtils.isEmpty(content)) return null;
     JSONObject jo = new JSONObject(content);
     if (jo.has("userId")) return jo.getString("userId");
   } catch (JSONException e) {
     e.printStackTrace();
   } finally {
     if (in != null) in.close();
   }
   return userId;
 }
예제 #5
0
  private Map<String, Long> parseUnreadCountList(Reader in) throws JsonParseException, IOException {
    JsonFactory f = new JsonFactory();
    JsonParser jp = f.createParser(in);

    String currName;

    String uid = null;
    Long timestamp = null;
    int len;
    String text = null;
    Map<String, Long> unreadList = new HashMap<String, Long>();

    jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
    while (jp.nextToken() != JsonToken.END_OBJECT) {
      currName = jp.getCurrentName();
      jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
      if (currName.equals("unreadcounts")) { // contains an object
        // start items
        while (jp.nextToken() != JsonToken.END_ARRAY) {
          if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
            // do nothing
          } else if (jp.getCurrentToken() == JsonToken.END_OBJECT) {
            if (!unreadList.containsKey(uid)) unreadList.put(uid, timestamp);
          }

          currName = jp.getCurrentName();
          if (currName == null) continue;

          jp.nextToken();
          if (currName.equals("id")) {
            uid = jp.getText();
          } else if (currName.equals("newestItemTimestampUsec")) {
            text = jp.getText();
            len = text.length();
            if (len > 13) text = jp.getText().substring(0, jp.getText().length() - 6);
            else if (len > 10) text = jp.getText().substring(0, jp.getText().length() - 3);
            timestamp = Utils.asLong(text); // millis -> unixtime
          } else {
            jp.skipChildren();
          }
        }
      } else {
        jp.skipChildren();
      }
    }

    return unreadList;
  }
예제 #6
0
  // NOTE: /api/0/stream/items/ids
  private Reader readStreamItemIds(long syncTime, IItemIdListHandler handler)
      throws IOException, ReaderException, RemoteException {
    initAuth();

    StringBuilder buff = new StringBuilder(URL_API_STREAM_ITEM_IDS.length() + 128);
    buff.append(URL_API_STREAM_ITEM_IDS);
    buff.append("?output=json"); // xml or json
    String uid = handler.stream();
    if (uid != null) {
      buff.append("&s=");
      buff.append(Utils.encode(uid));
    }
    if (handler.excludeRead()) {
      buff.append("&xt=").append(STATE_GOOGLE_READ);
    }
    int limit = handler.limit();
    if (limit > 0) {
      buff.append("&n=").append(limit);
    }
    buff.append("&r=").append(handler.newestFirst() ? "n" : "o");

    return doGetReader(buff.toString());
  }
예제 #7
0
  private String parseItemList(Reader in, IItemListHandler handler)
      throws JsonParseException, IOException, RemoteException {
    JsonFactory f = new JsonFactory();
    JsonParser jp = f.createParser(in);

    long length = 0;
    String currName;
    String mediaUrl = null;
    String mediaType = null;

    IItem entry = null;
    String continuation = null;
    List<IItem> itemList = new ArrayList<IItem>();

    List<String> excludedSubs = handler.excludedStreams(); // excluded subscriptions

    jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
    while (jp.nextToken() != JsonToken.END_OBJECT) {

      currName = jp.getCurrentName();
      jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
      if ("continuation".equals(currName)) {
        continuation = jp.getText();
      } else if ("items".equals(currName)) { // contains an object
        // start items
        while (jp.nextToken() != JsonToken.END_ARRAY) {
          // request stop
          //					if (handler.requestStop()) throw new JsonParseException(null, null);

          if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
            entry = new IItem();
          } else if (jp.getCurrentToken() == JsonToken.END_OBJECT) {
            if (entry != null && entry.uid.length() > 0) {
              if (length + entry.getLength() > MAX_TRANSACTION_LENGTH) {
                handler.items(itemList, STRATEGY_INSERT_DEFAULT);
                itemList.clear();
                length = 0;
              }

              itemList.add(entry);
              length += entry.getLength();
            }

            if (itemList.size() % 200 == 0
                || length
                    > MAX_TRANSACTION_LENGTH) { // avoid TransactionTooLargeException, android only
              // allows 1mb
              handler.items(itemList, STRATEGY_INSERT_DEFAULT);
              itemList.clear();
              length = 0;
            }
            entry = null;
          }

          currName = jp.getCurrentName();
          if (currName == null || entry == null) continue;

          jp.nextToken(); // move to value
          if (currName.equals("id")) {
            entry.uid = stripItemUid(jp.getText());
          } else if (currName.equals("crawlTimeMsec")) {
            entry.updatedTime = Long.valueOf(jp.getText()) / 1000;
          } else if (currName.equals("title")) {
            entry.title = Utils.stripTags(unEscapeEntities(jp.getText()), true);
          } else if (currName.equals("categories")) {
            while (jp.nextToken() != JsonToken.END_ARRAY) {
              String category = jp.getText();
              if (category != null && addUserLabel(category, entry)) {
                entry.addTag(category);
              }
              if (category != null && category.endsWith("/state/com.google/read")) {
                entry.read = true;
              }
            }
          } else if (currName.equals("published")) {
            entry.publishedTime = jp.getLongValue();
          } else if (currName.equals("alternate")) {
            while (jp.nextToken() != JsonToken.END_ARRAY) {
              currName = jp.getCurrentName();
              if (currName == null) continue;
              jp.nextToken();
              if (currName.equals("href")) {
                entry.link = jp.getText();
              } else {
                jp.skipChildren();
              }
            }
          } else if (currName.equals("enclosure")) {
            while (jp.nextToken() != JsonToken.END_ARRAY) {
              currName = jp.getCurrentName();
              if (currName == null) continue;
              jp.nextToken();
              if (currName.equals("href")) {
                mediaUrl = jp.getText();
              } else if (currName.equals("type")) {
                mediaType = jp.getText();
                if (mediaType.startsWith("image")) {
                  entry.addImage(mediaUrl, mediaType);
                } else if (mediaType.startsWith("video")) {
                  entry.addVideo(mediaUrl, mediaType);
                } else if (mediaType.startsWith("audio")) {
                  entry.addAudio(mediaUrl, mediaType);
                }

                mediaUrl = null;
                mediaType = null;
              } else {
                jp.skipChildren();
              }
            }
          } else if (currName.equals("summary") || currName.equals("content")) {
            while (jp.nextToken() != JsonToken.END_OBJECT) {
              currName = jp.getCurrentName();
              if (currName == null) continue;
              jp.nextToken();
              if (currName.equals("content")) {
                entry.content = unEscapeEntities(jp.getText());
              } else {
                jp.skipChildren();
              }
            }
          } else if (currName.equals("author")) {
            entry.author = jp.getText();
          } else if (currName.equals("origin")) {
            while (jp.nextToken() != JsonToken.END_OBJECT) {
              currName = jp.getCurrentName();
              if (currName == null) continue;
              jp.nextToken();
              if (currName.equals("streamId")) {
                String streamId = jp.getText();
                if (streamId != null
                    && (excludedSubs == null || !excludedSubs.contains(streamId))) {
                  entry.subUid = streamId;
                } else entry = null;
              } else {
                jp.skipChildren();
              }
            }
          } else {
            jp.skipChildren();
          }
        }

        handler.items(itemList, STRATEGY_INSERT_DEFAULT);
        itemList.clear();

      } else {
        jp.skipChildren();
      }
    }

    return continuation;
  }
예제 #8
0
 public DefaultHttpClient getClient() {
   if (client == null) client = Utils.createHttpClient();
   return client;
 }