Skip to content

Commit

Permalink
improve search function
Browse files Browse the repository at this point in the history
  • Loading branch information
tbo47 committed Nov 11, 2017
1 parent 25bf574 commit fcbbfcb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 26 deletions.
45 changes: 33 additions & 12 deletions amule-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,27 +772,24 @@ var AMuleCli = /** @class */ (function () {
return isPresent;
});
}
return list;
return list.children;
};
/**
* Search on the server
*
*/
AMuleCli.prototype.search = function (q, searchType, strict) {
AMuleCli.prototype.__search = function (q, network, strict) {
var _this = this;
if (searchType === void 0) { searchType = this.EC_SEARCH_TYPE.EC_SEARCH_KAD; }
if (strict === void 0) { strict = true; }
q = q.trim();
return this.sendToServer(this._getSearchStartRequest(q, searchType)).then(function (res) {
return this.sendToServer(this._getSearchStartRequest(q, network)).then(function (res) {
return new Promise(function (resolve, reject) {
if (searchType === _this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
if (network === _this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
var timeout_1 = 120, frequency = 2000, count_1 = 0, isSearchFinished_1 = false;
var intervalId_1 = setInterval(function () {
if (isSearchFinished_1) {
clearInterval(intervalId_1);
_this.fetchSearch().then(function (list) {
resolve(_this.filterResultList(list, q, strict));
});
_this.fetchSearch().then(function (list) { return resolve(_this.filterResultList(list, q, strict)); });
}
_this.sendToServer(_this._isSearchFinished()).then(function (res) {
if (res.children && res.children[0] && res.children[0].value !== 0) {
Expand All @@ -805,17 +802,41 @@ var AMuleCli = /** @class */ (function () {
}
}, frequency);
}
else if (searchType === _this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
else if (network === _this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
// TODO to improve
setTimeout(function () {
_this.fetchSearch().then(function (list) {
resolve(_this.filterResultList(list, q, strict));
});
_this.fetchSearch().then(function (list) { return resolve(_this.filterResultList(list, q, strict)); });
}, 1500);
}
});
});
};
AMuleCli.prototype._clean = function (str) {
return str
.replace(new RegExp('é', 'g'), 'e')
.replace(new RegExp('�€', 'g'), 'A')
.replace(new RegExp('è', 'g'), 'e');
};
/**
* Perform a search on the amule server.
*
* @param q
* @param network
*/
AMuleCli.prototype.search = function (q, network) {
var _this = this;
if (network === void 0) { network = this.EC_SEARCH_TYPE.EC_SEARCH_KAD; }
return this.getSharedFiles().then(function (allFiles) {
return _this.getDownloads().then(function (dlFiles) {
return _this.__search(q, network).then(function (list) {
allFiles.map(function (f) { return list.map(function (s) { return s.partfile_hash === f.partfile_hash ? s.present = true : false; }); });
dlFiles.map(function (f) { return list.map(function (s) { return s.partfile_hash === f.partfile_hash ? s.currentDl = true : false; }); });
list.map(function (e) { return e.partfile_name = _this._clean(e.partfile_name); });
return list;
});
});
});
};
AMuleCli.prototype.fetchSearch = function () {
return this.sendToServer(this.getSearchResultRequest());
};
Expand Down
49 changes: 36 additions & 13 deletions amule-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as net from 'net';

class Response {
[x: string]: any;
public header: number;
public totalSizeOfRequest: number = 0;
public opCode = null;
Expand Down Expand Up @@ -778,7 +779,7 @@ export class AMuleCli {
});
};

private filterResultList(list: Response, q: string, strict: boolean): Response {
private filterResultList(list: Response, q: string, strict: boolean): Response[] {
if (strict && list.children) {
list.children = list.children.filter(e => {
let isPresent = true;
Expand All @@ -791,26 +792,24 @@ export class AMuleCli {
return isPresent;
});
}
return list;
return list.children;
}


/**
* Search on the server
*
*/
public search(q: string, searchType: number = this.EC_SEARCH_TYPE.EC_SEARCH_KAD, strict = true): Promise<Response> {
public __search(q: string, network: number, strict = true): Promise<Response[]> {
q = q.trim();
return this.sendToServer(this._getSearchStartRequest(q, searchType)).then(res => {
return new Promise<Response>((resolve, reject) => {
if (searchType === this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
return this.sendToServer(this._getSearchStartRequest(q, network)).then(res => {
return new Promise<Response[]>((resolve, reject) => {
if (network === this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
let timeout = 120, frequency = 2000, count = 0, isSearchFinished = false;
const intervalId = setInterval(() => {
if (isSearchFinished) {
clearInterval(intervalId);
this.fetchSearch().then(list => {
resolve(this.filterResultList(list, q, strict));
})
this.fetchSearch().then(list => resolve(this.filterResultList(list, q, strict)))
}
this.sendToServer(this._isSearchFinished()).then(res => {
if (res.children && res.children[0] && res.children[0].value !== 0) {
Expand All @@ -822,18 +821,42 @@ export class AMuleCli {
clearInterval(intervalId);
}
}, frequency);
} else if (searchType === this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
} else if (network === this.EC_SEARCH_TYPE.EC_SEARCH_LOCA) {
// TODO to improve
setTimeout(() => {
this.fetchSearch().then(list => {
resolve(this.filterResultList(list, q, strict));
})
this.fetchSearch().then(list => resolve(this.filterResultList(list, q, strict)))
}, 1500);
}
});
});
}

public _clean(str: string) {
return str
.replace(new RegExp('é', 'g'), 'e')
.replace(new RegExp('�€', 'g'), 'A')
.replace(new RegExp('è', 'g'), 'e')
}

/**
* Perform a search on the amule server.
*
* @param q
* @param network
*/
public search(q: string, network: number = this.EC_SEARCH_TYPE.EC_SEARCH_KAD) {
return this.getSharedFiles().then(allFiles => {
return this.getDownloads().then(dlFiles => {
return this.__search(q, network).then(list => {
allFiles.map(f => list.map(s => s.partfile_hash === f.partfile_hash ? s.present = true : false));
dlFiles.map(f => list.map(s => s.partfile_hash === f.partfile_hash ? s.currentDl = true : false));
list.map(e => e.partfile_name = this._clean(e.partfile_name))
return list
});
});
});
}

public fetchSearch(): Promise<Response> {
return this.sendToServer(this.getSearchResultRequest());
}
Expand Down
2 changes: 1 addition & 1 deletion doc/amule-js-node-ex2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ aMule.setStringDecoder(new StringDecoder('utf8'));
aMule.connect().then(m => {
const query = 'clo2 ' + aMule.getMonth();
aMule.search(query, 2).then(result => {
const funcs = result.children.map(e => () => aMule.download(e))
const funcs = result.map(e => () => aMule.download(e))
console.log('%d results found for query: %s', funcs.length, query);
aMule.promiseSerial(funcs).then(list => {
list.map(e => console.log(e['partfile_name']))
Expand Down

0 comments on commit fcbbfcb

Please sign in to comment.