/// <summary> /// 获取采样方法对应的采样数量 /// </summary> /// <param name="sampleMethod">采样方法</param> /// <returns>目标获取数量</returns> public static int GetAimCount(Enums.SampleMethod sampleMethod) { switch (sampleMethod) { case Enums.SampleMethod.All: return(Int32.MaxValue); case Enums.SampleMethod.First100: return(100); case Enums.SampleMethod.First5000: return(5000); default: return(0); } }
/// <summary> /// 获取用户所关注的人的ID列表 /// </summary> /// <param name="userID">用户ID</param> /// <param name="sampleMethod">采样数量</param> /// <returns>ID列表</returns> public static IEnumerable <string> GetFriendsIDs(string userID, Enums.SampleMethod sampleMethod) { if (sampleMethod == Enums.SampleMethod.None) { return(null); } int target = Utilities.GetAimCount(sampleMethod); target = Math.Min(target, 5000); RequireAccPermission(1); var users = defaultClient.API.Entity.Friendships.FriendIDs(userID, null, target); return(users.Users); }
/// <summary> /// 获取用户至多5000个粉丝 /// </summary> /// <param name="AuthorID">用户ID</param> /// <param name="sampleMethod">采样数量</param> /// <param name="result">返回结果</param> public static void GetFollowers(string AuthorID, Enums.SampleMethod sampleMethod, List <NetDimension.Weibo.Entities.user.Entity> result) { int target = Utilities.GetAimCount(sampleMethod); int cur = 1; do { RequireAccPermission(1); var info = defaultClient.API.Entity.Friendships.Followers(AuthorID, null, 200, cur); foreach (var user in info.Users) { result.Add(user); } cur = int.Parse(info.NextCursor); } while (cur != 0 && result.Count < target); }
/// <summary> /// 获取用户带有地点信息的微博 /// </summary> /// <param name="author">用户ID</param> /// <param name="result">返回结果</param> /// <param name="sampleMethod">采样数量</param> public static void GetUserStatusLocationHistory(Author author, List <dynamic> result, Enums.SampleMethod sampleMethod) { if (sampleMethod == Enums.SampleMethod.None) { return; } int cur = 1, target = Utilities.GetAimCount(sampleMethod); string sinceID = author.Location_LastSinceID; do { try { RequireAccPermission(1); dynamic locationlist = defaultClient.API.Dynamic.Place.UserTimeline(author.AuthorID, sinceID, null, 50, cur++); try { if (locationlist == null || locationlist.statuses == null) { return; } } catch (Exception) { return; } foreach (dynamic status in locationlist.statuses) { result.Add(status); try { if (string.IsNullOrEmpty(author.Location_LastSinceID) || Int64.Parse(status.id) > Int64.Parse(author.Location_LastSinceID)) { author.Location_LastSinceID = status.id; } } catch (Exception) { throw new WeiboException("ID数字格式错误"); } } } catch (WeiboException ex) { if (ex.ErrorCode == "00000" || ex.ErrorCode == "23201") { break; } else { throw ex; } } } while (result.Count < target); }
/// <summary> /// 获取用户前若干条微博 /// </summary> /// <param name="userID">用户ID</param> /// <param name="sampleMethod">采样方法</param> /// <returns>微博集合</returns> public static void GetAuthorLatestStatus(Author author, List <NetDimension.Weibo.Entities.status.Entity> result, Enums.SampleMethod sampleMethod) { if (sampleMethod == Enums.SampleMethod.None) { return; } int page = 1, target = Utilities.GetAimCount(sampleMethod), totalNum = target; string sinceID = author.LastSinceID; bool finished = false; do { RequireAccPermission(1); var statuslist = defaultClient.API.Entity.Statuses.UserTimeline(author.AuthorID, null, sinceID, null, 50, page++); if (statuslist == null || statuslist.Statuses == null) { break; //抓取结束条件 } totalNum = Math.Min(totalNum, statuslist.TotalNumber); finished = true; foreach (var status in statuslist.Statuses) { result.Add(status); try { if (string.IsNullOrEmpty(author.LastSinceID) || Int64.Parse(status.ID) > Int64.Parse(author.LastSinceID)) { author.LastSinceID = status.ID; } } catch (Exception) { throw new WeiboException("ID数字格式错误"); }; finished = false; } } while (!finished && result.Count < target && result.Count < totalNum); }
/// <summary> /// 获取CBD附近发微博的人 /// </summary> /// <param name="longitude">CBD经度</param> /// <param name="latitude">CBD纬度</param> /// <param name="range">CBD辐射半径</param> /// <param name="startTime">开始时间</param> /// <param name="endTime">结束时间</param> /// <returns>该位置附近特定时间段发微博的人及其所发微博ID</returns> public static void GetUsersNearCBD(double longitude, double latitude, int range, Int64 startTime, Int64 endTime, Enums.SampleMethod sampleMethod, List <dynamic> users, List <dynamic> statuses) { int cur = 1, target = Utilities.GetAimCount(sampleMethod); do { try { RequireAccPermission(1); dynamic userlist = defaultClient.API.Dynamic.Place.NearByUsers((float)latitude, (float)longitude, range, 50, cur++, startTime, endTime); try { if (userlist == null || userlist.users == null) { break; } foreach (dynamic user in userlist.users) { users.Add(user); try { dynamic tmp = user.status; statuses.Add(tmp); } catch (Exception) { statuses.Add(null); } } } catch (Exception) { break; } } catch (JsonReaderException) { break; } catch (WeiboException ex) { //如果返回内容为空则代表结束 if (ex.ErrorCode == "00000" || ex.ErrorCode == "23201") { break; } else { throw ex; } } } while (users.Count < target); }