Skip to content

Commit fcbbfcb

Browse files
committed
improve search function
1 parent 25bf574 commit fcbbfcb

File tree

3 files changed

+70
-26
lines changed

3 files changed

+70
-26
lines changed

amule-js.js

+33-12
Original file line numberDiff line numberDiff line change
@@ -772,27 +772,24 @@ var AMuleCli = /** @class */ (function () {
772772
return isPresent;
773773
});
774774
}
775-
return list;
775+
return list.children;
776776
};
777777
/**
778778
* Search on the server
779779
*
780780
*/
781-
AMuleCli.prototype.search = function (q, searchType, strict) {
781+
AMuleCli.prototype.__search = function (q, network, strict) {
782782
var _this = this;
783-
if (searchType === void 0) { searchType = this.EC_SEARCH_TYPE.EC_SEARCH_KAD; }
784783
if (strict === void 0) { strict = true; }
785784
q = q.trim();
786-
return this.sendToServer(this._getSearchStartRequest(q, searchType)).then(function (res) {
785+
return this.sendToServer(this._getSearchStartRequest(q, network)).then(function (res) {
787786
return new Promise(function (resolve, reject) {
788-
if (searchType === _this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
787+
if (network === _this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
789788
var timeout_1 = 120, frequency = 2000, count_1 = 0, isSearchFinished_1 = false;
790789
var intervalId_1 = setInterval(function () {
791790
if (isSearchFinished_1) {
792791
clearInterval(intervalId_1);
793-
_this.fetchSearch().then(function (list) {
794-
resolve(_this.filterResultList(list, q, strict));
795-
});
792+
_this.fetchSearch().then(function (list) { return resolve(_this.filterResultList(list, q, strict)); });
796793
}
797794
_this.sendToServer(_this._isSearchFinished()).then(function (res) {
798795
if (res.children && res.children[0] && res.children[0].value !== 0) {
@@ -805,17 +802,41 @@ var AMuleCli = /** @class */ (function () {
805802
}
806803
}, frequency);
807804
}
808-
else if (searchType === _this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
805+
else if (network === _this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
809806
// TODO to improve
810807
setTimeout(function () {
811-
_this.fetchSearch().then(function (list) {
812-
resolve(_this.filterResultList(list, q, strict));
813-
});
808+
_this.fetchSearch().then(function (list) { return resolve(_this.filterResultList(list, q, strict)); });
814809
}, 1500);
815810
}
816811
});
817812
});
818813
};
814+
AMuleCli.prototype._clean = function (str) {
815+
return str
816+
.replace(new RegExp('é', 'g'), 'e')
817+
.replace(new RegExp('�€', 'g'), 'A')
818+
.replace(new RegExp('è', 'g'), 'e');
819+
};
820+
/**
821+
* Perform a search on the amule server.
822+
*
823+
* @param q
824+
* @param network
825+
*/
826+
AMuleCli.prototype.search = function (q, network) {
827+
var _this = this;
828+
if (network === void 0) { network = this.EC_SEARCH_TYPE.EC_SEARCH_KAD; }
829+
return this.getSharedFiles().then(function (allFiles) {
830+
return _this.getDownloads().then(function (dlFiles) {
831+
return _this.__search(q, network).then(function (list) {
832+
allFiles.map(function (f) { return list.map(function (s) { return s.partfile_hash === f.partfile_hash ? s.present = true : false; }); });
833+
dlFiles.map(function (f) { return list.map(function (s) { return s.partfile_hash === f.partfile_hash ? s.currentDl = true : false; }); });
834+
list.map(function (e) { return e.partfile_name = _this._clean(e.partfile_name); });
835+
return list;
836+
});
837+
});
838+
});
839+
};
819840
AMuleCli.prototype.fetchSearch = function () {
820841
return this.sendToServer(this.getSearchResultRequest());
821842
};

amule-ts.ts

+36-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as net from 'net';
33

44
class Response {
5+
[x: string]: any;
56
public header: number;
67
public totalSizeOfRequest: number = 0;
78
public opCode = null;
@@ -778,7 +779,7 @@ export class AMuleCli {
778779
});
779780
};
780781

781-
private filterResultList(list: Response, q: string, strict: boolean): Response {
782+
private filterResultList(list: Response, q: string, strict: boolean): Response[] {
782783
if (strict && list.children) {
783784
list.children = list.children.filter(e => {
784785
let isPresent = true;
@@ -791,26 +792,24 @@ export class AMuleCli {
791792
return isPresent;
792793
});
793794
}
794-
return list;
795+
return list.children;
795796
}
796797

797798

798799
/**
799800
* Search on the server
800801
*
801802
*/
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[]> {
803804
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) {
807808
let timeout = 120, frequency = 2000, count = 0, isSearchFinished = false;
808809
const intervalId = setInterval(() => {
809810
if (isSearchFinished) {
810811
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)))
814813
}
815814
this.sendToServer(this._isSearchFinished()).then(res => {
816815
if (res.children && res.children[0] && res.children[0].value !== 0) {
@@ -822,18 +821,42 @@ export class AMuleCli {
822821
clearInterval(intervalId);
823822
}
824823
}, frequency);
825-
} else if (searchType === this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
824+
} else if (network === this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
826825
// TODO to improve
827826
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)))
831828
}, 1500);
832829
}
833830
});
834831
});
835832
}
836833

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+
837860
public fetchSearch(): Promise<Response> {
838861
return this.sendToServer(this.getSearchResultRequest());
839862
}

doc/amule-js-node-ex2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ aMule.setStringDecoder(new StringDecoder('utf8'));
2020
aMule.connect().then(m => {
2121
const query = 'clo2 ' + aMule.getMonth();
2222
aMule.search(query, 2).then(result => {
23-
const funcs = result.children.map(e => () => aMule.download(e))
23+
const funcs = result.map(e => () => aMule.download(e))
2424
console.log('%d results found for query: %s', funcs.length, query);
2525
aMule.promiseSerial(funcs).then(list => {
2626
list.map(e => console.log(e['partfile_name']))

0 commit comments

Comments
 (0)