44using System . Linq ;
55using System . Net ;
66using System . Net . Http ;
7- using System . Text . RegularExpressions ;
87using System . Threading . Tasks ;
98using HtmlAgilityPack ;
109
@@ -41,14 +40,28 @@ public NetUser(IPAddress address, DateTime loginTime, string client)
4140 public string Client { get ; }
4241 }
4342
43+ /// <summary>
44+ /// Flux detail.
45+ /// </summary>
4446 public class NetDetail
4547 {
48+ /// <summary>
49+ /// Initializes a new instance of <see cref="NetDetail"/> class.
50+ /// </summary>
51+ /// <param name="onlineDate">The date logging in.</param>
52+ /// <param name="flux">The flux has been used.</param>
4653 public NetDetail ( DateTime onlineDate , long flux )
4754 {
4855 OnlineDate = onlineDate ;
4956 Flux = flux ;
5057 }
58+ /// <summary>
59+ /// The date logging in.
60+ /// </summary>
5161 public DateTime OnlineDate { get ; }
62+ /// <summary>
63+ /// The flux has been used.
64+ /// </summary>
5265 public long Flux { get ; }
5366 }
5467
@@ -112,19 +125,21 @@ public UseregHelper(string username, string password, HttpClient client)
112125 /// <summary>
113126 /// Get all connections of this user.
114127 /// </summary>
115- /// <returns><see cref="IEnumerable {NetUser}"/></returns>
116- public async Task < IEnumerable < NetUser > > GetUsersAsync ( )
128+ /// <returns><see cref="IAsyncEnumerable {NetUser}"/></returns>
129+ public async IAsyncEnumerable < NetUser > GetUsersAsync ( )
117130 {
118131 string userhtml = await GetAsync ( InfoUri ) ;
119132 var doc = new HtmlDocument ( ) ;
120133 doc . LoadHtml ( userhtml ) ;
121- return from tr in doc . DocumentNode . Element ( "html" ) . Element ( "body" ) . Element ( "table" ) . Element ( "tr" ) . Elements ( "td" ) . Last ( ) . Elements ( "table" ) . ElementAt ( 1 ) . Elements ( "tr" ) . Skip ( 1 )
122- let tds = ( from td in tr . Elements ( "td" ) . Skip ( 1 )
123- select td . FirstChild ? . InnerText ) . ToArray ( )
124- select new NetUser (
125- IPAddress . Parse ( tds [ 0 ] ) ,
126- DateTime . ParseExact ( tds [ 1 ] , "yyyy-MM-dd HH:mm:ss" , CultureInfo . InvariantCulture ) ,
127- tds [ 10 ] ) ;
134+ foreach ( var tr in doc . DocumentNode . Element ( "html" ) . Element ( "body" ) . Element ( "table" ) . Element ( "tr" ) . Elements ( "td" ) . Last ( ) . Elements ( "table" ) . ElementAt ( 1 ) . Elements ( "tr" ) . Skip ( 1 ) )
135+ {
136+ var tds = ( from td in tr . Elements ( "td" ) . Skip ( 1 )
137+ select td . FirstChild ? . InnerText ) . ToArray ( ) ;
138+ yield return new NetUser (
139+ IPAddress . Parse ( tds [ 0 ] ) ,
140+ DateTime . ParseExact ( tds [ 1 ] , "yyyy-MM-dd HH:mm:ss" , CultureInfo . InvariantCulture ) ,
141+ tds [ 10 ] ) ;
142+ }
128143 }
129144
130145 private long ParseFlux ( string str )
@@ -146,28 +161,30 @@ private long ParseFlux(string str)
146161 return ( long ) flux ;
147162 }
148163
149- public async Task < IEnumerable < NetDetail > > GetDetailsAsync ( )
164+ /// <summary>
165+ /// Get all details of this month.
166+ /// </summary>
167+ /// <returns><see cref="IAsyncEnumerable{NetDetail}"/></returns>
168+ public async IAsyncEnumerable < NetDetail > GetDetailsAsync ( )
150169 {
151170 DateTime now = DateTime . Now ;
152- int i = 1 ;
153- List < NetDetail > list = new List < NetDetail > ( ) ;
154- while ( true )
171+ for ( int i = 1 ; ; i ++ )
155172 {
156173 string detailhtml = await GetAsync ( string . Format ( DetailUri , now . Year , now . Month . ToString ( ) . PadLeft ( 2 , '0' ) , now . Day , i ) ) ;
157174 var doc = new HtmlDocument ( ) ;
158175 doc . LoadHtml ( detailhtml ) ;
159- int oldsize = list . Count ;
160- list . AddRange (
161- from tr in doc . DocumentNode . Element ( "html" ) . Element ( "body" ) . Element ( "table" ) . Element ( "tr" ) . Elements ( "td" ) . Last ( ) . Elements ( "table" ) . Last ( ) . Elements ( "tr" ) . Skip ( 1 )
162- let tds = ( from td in tr . Elements ( "td" ) . Skip ( 1 )
163- select td . FirstChild ? . InnerText ) . ToArray ( )
164- select new NetDetail (
176+ bool @continue = false ;
177+ foreach ( var tr in doc . DocumentNode . Element ( "html" ) . Element ( "body" ) . Element ( "table" ) . Element ( "tr" ) . Elements ( "td" ) . Last ( ) . Elements ( "table" ) . Last ( ) . Elements ( "tr" ) . Skip ( 1 ) )
178+ {
179+ var tds = ( from td in tr . Elements ( "td" ) . Skip ( 1 )
180+ select td . FirstChild ? . InnerText ) . ToArray ( ) ;
181+ yield return new NetDetail (
165182 DateTime . ParseExact ( tds [ 1 ] , "yyyy-MM-dd HH:mm:ss" , CultureInfo . InvariantCulture ) ,
166- ParseFlux ( tds [ 4 ] ) ) ) ;
167- if ( list . Count <= oldsize ) break ;
168- i ++ ;
183+ ParseFlux ( tds [ 4 ] ) ) ;
184+ @continue = true ;
185+ }
186+ if ( ! @continue ) break ;
169187 }
170- return list ;
171188 }
172189
173190 /// <summary>
0 commit comments