2
2
import * as net from 'net' ;
3
3
4
4
class Response {
5
+ [ x : string ] : any ;
5
6
public header : number ;
6
7
public totalSizeOfRequest : number = 0 ;
7
8
public opCode = null ;
@@ -778,7 +779,7 @@ export class AMuleCli {
778
779
} ) ;
779
780
} ;
780
781
781
- private filterResultList ( list : Response , q : string , strict : boolean ) : Response {
782
+ private filterResultList ( list : Response , q : string , strict : boolean ) : Response [ ] {
782
783
if ( strict && list . children ) {
783
784
list . children = list . children . filter ( e => {
784
785
let isPresent = true ;
@@ -791,26 +792,24 @@ export class AMuleCli {
791
792
return isPresent ;
792
793
} ) ;
793
794
}
794
- return list ;
795
+ return list . children ;
795
796
}
796
797
797
798
798
799
/**
799
800
* Search on the server
800
801
*
801
802
*/
802
- public search ( q : string , searchType : number = this . EC_SEARCH_TYPE . EC_SEARCH_KAD , strict = true ) : Promise < Response > {
803
+ public __search ( q : string , network : number , strict = true ) : Promise < Response [ ] > {
803
804
q = q . trim ( ) ;
804
- return this . sendToServer ( this . _getSearchStartRequest ( q , searchType ) ) . then ( res => {
805
- return new Promise < Response > ( ( resolve , reject ) => {
806
- if ( searchType === this . EC_SEARCH_TYPE . EC_SEARCH_KAD ) {
805
+ return this . sendToServer ( this . _getSearchStartRequest ( q , network ) ) . then ( res => {
806
+ return new Promise < Response [ ] > ( ( resolve , reject ) => {
807
+ if ( network === this . EC_SEARCH_TYPE . EC_SEARCH_KAD ) {
807
808
let timeout = 120 , frequency = 2000 , count = 0 , isSearchFinished = false ;
808
809
const intervalId = setInterval ( ( ) => {
809
810
if ( isSearchFinished ) {
810
811
clearInterval ( intervalId ) ;
811
- this . fetchSearch ( ) . then ( list => {
812
- resolve ( this . filterResultList ( list , q , strict ) ) ;
813
- } )
812
+ this . fetchSearch ( ) . then ( list => resolve ( this . filterResultList ( list , q , strict ) ) )
814
813
}
815
814
this . sendToServer ( this . _isSearchFinished ( ) ) . then ( res => {
816
815
if ( res . children && res . children [ 0 ] && res . children [ 0 ] . value !== 0 ) {
@@ -822,18 +821,42 @@ export class AMuleCli {
822
821
clearInterval ( intervalId ) ;
823
822
}
824
823
} , frequency ) ;
825
- } else if ( searchType === this . EC_SEARCH_TYPE . EC_SEARCH_LOCA ) {
824
+ } else if ( network === this . EC_SEARCH_TYPE . EC_SEARCH_LOCA ) {
826
825
// TODO to improve
827
826
setTimeout ( ( ) => {
828
- this . fetchSearch ( ) . then ( list => {
829
- resolve ( this . filterResultList ( list , q , strict ) ) ;
830
- } )
827
+ this . fetchSearch ( ) . then ( list => resolve ( this . filterResultList ( list , q , strict ) ) )
831
828
} , 1500 ) ;
832
829
}
833
830
} ) ;
834
831
} ) ;
835
832
}
836
833
834
+ public _clean ( str : string ) {
835
+ return str
836
+ . replace ( new RegExp ( 'é' , 'g' ) , 'e' )
837
+ . replace ( new RegExp ( '�' , 'g' ) , 'A' )
838
+ . replace ( new RegExp ( 'è' , 'g' ) , 'e' )
839
+ }
840
+
841
+ /**
842
+ * Perform a search on the amule server.
843
+ *
844
+ * @param q
845
+ * @param network
846
+ */
847
+ public search ( q : string , network : number = this . EC_SEARCH_TYPE . EC_SEARCH_KAD ) {
848
+ return this . getSharedFiles ( ) . then ( allFiles => {
849
+ return this . getDownloads ( ) . then ( dlFiles => {
850
+ return this . __search ( q , network ) . then ( list => {
851
+ allFiles . map ( f => list . map ( s => s . partfile_hash === f . partfile_hash ? s . present = true : false ) ) ;
852
+ dlFiles . map ( f => list . map ( s => s . partfile_hash === f . partfile_hash ? s . currentDl = true : false ) ) ;
853
+ list . map ( e => e . partfile_name = this . _clean ( e . partfile_name ) )
854
+ return list
855
+ } ) ;
856
+ } ) ;
857
+ } ) ;
858
+ }
859
+
837
860
public fetchSearch ( ) : Promise < Response > {
838
861
return this . sendToServer ( this . getSearchResultRequest ( ) ) ;
839
862
}
0 commit comments