11using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . Globalization ;
45using System . Linq ;
@@ -122,24 +123,36 @@ public UseregHelper(string username, string password, HttpClient client)
122123 /// <returns>The response of the website.</returns>
123124 public async Task < LogResponse > LogoutAsync ( IPAddress ip ) => LogResponse . ParseFromUsereg ( await PostAsync ( InfoUri , string . Format ( DropData , ip . ToString ( ) ) ) ) ;
124125
126+ /// <summary>
127+ /// Get login data with username and password.
128+ /// </summary>
129+ /// <returns>A dictionary contains the data.</returns>
130+ private Dictionary < string , string > GetLoginData ( )
131+ {
132+ return new Dictionary < string , string >
133+ {
134+ [ "action" ] = "login" ,
135+ [ "user_login_name" ] = Username ,
136+ [ "user_password" ] = CryptographyHelper . GetMD5 ( Password )
137+ } ;
138+ }
139+
125140 /// <summary>
126141 /// Get all connections of this user.
127142 /// </summary>
128- /// <returns><see cref="IAsyncEnumerable {NetUser}"/></returns>
129- public async IAsyncEnumerable < NetUser > GetUsersAsync ( )
143+ /// <returns><see cref="IEnumerable {NetUser}"/></returns>
144+ public async Task < IEnumerable < NetUser > > GetUsersAsync ( )
130145 {
131146 string userhtml = await GetAsync ( InfoUri ) ;
132147 var doc = new HtmlDocument ( ) ;
133148 doc . LoadHtml ( userhtml ) ;
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- }
149+ 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 )
150+ let tds = ( from td in tr . Elements ( "td" ) . Skip ( 1 )
151+ select td . FirstChild ? . InnerText ) . ToArray ( )
152+ select new NetUser (
153+ IPAddress . Parse ( tds [ 0 ] ) ,
154+ DateTime . ParseExact ( tds [ 1 ] , "yyyy-MM-dd HH:mm:ss" , CultureInfo . InvariantCulture ) ,
155+ tds [ 10 ] ) ;
143156 }
144157
145158 private long ParseFlux ( string str )
@@ -164,41 +177,27 @@ private long ParseFlux(string str)
164177 /// <summary>
165178 /// Get all details of this month.
166179 /// </summary>
167- /// <returns><see cref="IAsyncEnumerable {NetDetail}"/></returns>
168- public async IAsyncEnumerable < NetDetail > GetDetailsAsync ( )
180+ /// <returns><see cref="IEnumerable {NetDetail}"/></returns>
181+ public async Task < IEnumerable < NetDetail > > GetDetailsAsync ( )
169182 {
170183 DateTime now = DateTime . Now ;
184+ List < NetDetail > list = new List < NetDetail > ( ) ;
171185 for ( int i = 1 ; ; i ++ )
172186 {
173187 string detailhtml = await GetAsync ( string . Format ( DetailUri , now . Year , now . Month . ToString ( ) . PadLeft ( 2 , '0' ) , now . Day , i ) ) ;
174188 var doc = new HtmlDocument ( ) ;
175189 doc . LoadHtml ( detailhtml ) ;
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 (
190+ int oldsize = list . Count ;
191+ list . AddRange (
192+ from tr in doc . DocumentNode . Element ( "html" ) . Element ( "body" ) . Element ( "table" ) . Element ( "tr" ) . Elements ( "td" ) . Last ( ) . Elements ( "table" ) . Last ( ) . Elements ( "tr" ) . Skip ( 1 )
193+ let tds = ( from td in tr . Elements ( "td" ) . Skip ( 1 )
194+ select td . FirstChild ? . InnerText ) . ToArray ( )
195+ select new NetDetail (
182196 DateTime . ParseExact ( tds [ 1 ] , "yyyy-MM-dd HH:mm:ss" , CultureInfo . InvariantCulture ) ,
183- ParseFlux ( tds [ 4 ] ) ) ;
184- @continue = true ;
185- }
186- if ( ! @continue ) break ;
197+ ParseFlux ( tds [ 4 ] ) ) ) ;
198+ if ( list . Count <= oldsize ) break ;
187199 }
188- }
189-
190- /// <summary>
191- /// Get login data with username and password.
192- /// </summary>
193- /// <returns>A dictionary contains the data.</returns>
194- private Dictionary < string , string > GetLoginData ( )
195- {
196- return new Dictionary < string , string >
197- {
198- [ "action" ] = "login" ,
199- [ "user_login_name" ] = Username ,
200- [ "user_password" ] = CryptographyHelper . GetMD5 ( Password )
201- } ;
200+ return list ;
202201 }
203202 }
204203}
0 commit comments