Get a list of all forwarded users of a Sina Weibo (using the Repost_timeline interface)

Source: Internet
Author: User
Tags json

When you use Sina Weibo's API to do projects, you need to get a list of all the users who have forwarded a particular microblog. decided to use Statuses/repost_timeline to get a forwarding list, but found two problems:

1: Sina Weibo has limited access to this interface and can only return the latest 2000 data. The official document says: This interface only returns the latest 2000 data. Practice has found that once the number of fetches is about to exceed 2000, the outgoing request will return the error message as shown in "Code 1".

2: Even if I want to compromise, with only 2000 data, Using the official javasdk of Sina Weibo to provide the corresponding test class Getreposttimeline.java, each request can only return 20 data, want to repeatedly request to scrape up all the user list but found that multiple requests returned to the results have been repeated (and the test permissions per hour only valuable 150 times the number of requests, do not want this Waste).


After several attempts, we found that the second problem can be resolved through the two parameters of the Repost_timeline interface:

The Count single page returns the maximum number of record bars, not exceeding 200, and the default is 20.

Page Returns the page number of the result, which defaults to 1.

That is, whenever you modify a single page to return a record bar number of 200, and then loop 10 times to send a request to the server, each request to change the page number to return the results, you can access the number of users forwarding a tweet (2000) list. The code is verified to be feasible, the specific code see "Code 2":

As a rule, not only did you get nearly 2000 data, but the number of requests was minimized. The same changes apply to Sina Weibo's other APIs with Count and page parameters. But the demand for "a list of all forwarded users who want to get a tweet so far" has not been met, after all, they only provide the latest 2000 data, multiple requests to fill all the user list is not. Please reader answer ....

//code 1 weibo4j.model.weiboexception:a jsonobject text must begin with ' {' to character 1:[] at weibo4j . http. Response.asjsonobject (response.java:201) at Weibo4j.model.Status.constructWapperStatus (status.java:263) at weibo4j . Timeline.getreposttimeline (timeline.java:308) at VisProject.GetRepostList.main (getrepostlist.java:33) caused by: Weibo4j.org.json.jsonexception:a Jsonobject text must begin with ' {' to character 1 at Weibo4j.org.json.JSONTokener.synt Axerror (jsontokener.java:410) at weibo4j.org.json.jsonobject.<init> (jsonobject.java:179) at Weibo4j.org.json.jsonobject.<init> (jsonobject.java:402) at Weibo4j.http.Response.asJSONObject ( response.java:199) ... 3 more 
<pre code_snippet_id= "221346" snippet_file_name= "blog_20140306_1_2099870" name= "code" class= "java" >//codes 2

Import java.util.List; Import weibo4j.
Timeline;
Import weibo4j.model.Paging;
Import Weibo4j.model.Status;
Import Weibo4j.model.StatusWapper;

Import weibo4j.model.WeiboException;   public class Getrepostlist {public static void main (string[] args) {//authorization String Access_token = "";
		Here put before the Accesstoken Timeline TM = new Timeline ();
		
		Tm.client.setToken (Access_token);   String id = "";
		Here put the ID of the forwarded Weibo//Set forwarding list page Paging page = new paging (); Page.setcount (200);   Set to the maximum size of each page int i=0; Test with int flag = 200;
			The control loop jumps out, reduces the API request for (int pageindex=1; pageindex<10; pageindex++) {page.setpage (pageIndex);
				try {if (Flag < 190) break;
				Statuswapper status = Tm.getreposttimeline (ID, page); 
                                list<status> resultlist = status.getstatuses ();
			  Flag = Resultlist.size ();      if (resultlist!= null && resultlist.size () > 0) {//If the obtained content is empty, exit for (Status S:resu  Ltlist) {System.out.println (S.getuser (). GetName ());
				    The operation of each forwarding item is placed in this for each function, showing the user name of the forwarding user i++;
			}}else{break;}
			catch (Weiboexception e) {e.printstacktrace ();
	} System.out.println ("The number of Retwitter catched this time is:" +i); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.