Turn asynchronous functions to their synchronous counterpart

Recently, asynchronous programming becomes a hit as slow jobs can perform while not blocking other jobs to be done. However, sometimes we want something to be done in a synchronous way like getting a single value from a query (you know callbacks are clumsy). In order to solve the  problem, turning asynchronous functions to synchronous can solve the problem.

To turn asynchronous functions to synchronous, we need to set up a loop checking the result, therefore cause blocking.

Here’s the code: (we use javascript here because it’s¬†straightforward)

function somethingSync(args){
    var ret; //the result-holding variable
    //doing something async here...
    somethingAsync(args,function(result){
        ret = result;
    });
    while(ret === undefined){} //wait for the result until it's available, cause the blocking
    return ret;
}

Create a simple web spider in node.js

I’ve seen many web crawlers written in other languages like PHP, Python, Ruby, etc. However, there’s no one that notices node.js is also good in doing these kind of things. So I utilizes¬†asynchronous network I/O provided by node.js to create a fast, stable web crawler (spider) in node.js and it’s very simple.¬†Here I’ll reveal the logic of a web crawler and allow you to create your own.

A web spider is a long-running program that finds websites and record them. After that we can search the records generated by web spiders to find information we want. It’s the basic unit of a search engine.

First, we need to search for links from a base webpage and record the webpage to the database. Then, follow the links found and repeat the first step to discover more webpages.

Here are some modules that helps us coding a web spider:

  1. request (helps fetching webpages)
  2. cheerio (helps parsing webpages)
  3. mysql (for indexing pages)
  4. async (helper library)

And here’s the code:







Here’s how it works:

First, it crawls the base page specified in config.json first. Then it tries to find all links from the page content and add them to the queue, then the first item on the queue will be executed. This process continues again and again…

Also, we store the queue in a MySQL database for scalability. This script can be spawned across multiple machines.

My own Text-To-Speech (TTS) Service!

Update: Multi-language support added.

I’m now release my own Text-To-Speech Service made with node.js. I think an easy-to-use TTS service is necessary¬†for web applications nowadays. However, creating your own one isn’t easy. So I created one based on eSpeak and hosted an API for you to use easily.

The API is fairly simple. Just issue a HTTP GET or HTTP POST request to the address below (https protocol also supported):

http://licson-node.hp.af.cm/tts

And it’ll return a wave¬†file representing the result. Here’s some parameters you need to provide with the request.

  1. text – The text to read. (required)
  2. speed РThe speed of reading. (optional) Default: 180 (Range 10-300)
  3. pitch – The pitch of reading. (optional) Default: 50 (Range 0-100)
  4. wordgap РThe addtional time delay between each word. (optional) Default: 0
  5. lang РThe language of the text. (optional) Default: en/en  See all language options below.

Here’s an example of using the API through HTTP GET requests:

http://licson-node.hp.af.cm/tts?text=Hi!+I+can't+wait+to+meet+you.&speed=185&lang=en/en-us

Supported languages:

  1. ca (Catalan)
  2. cs (Czech)
  3. de (German)
  4. el (Greek)
  5. en/en (English)
  6. en/en-n (English, regional)
  7. en/en-rp (English, regional)
  8. en/en-sc (English, Scottish)
  9. en/en-us (English, US)
  10. en/en-wm (English, regional)
  11. eo (Esperanto)
  12. es (Spanish)
  13. es-la (Spanish, Latin America)
  14. fi (Finnish)
  15. fr (French)
  16. hu (Hungarian)
  17. it (Italian)
  18. kn (Kannada)
  19. la (Latin)
  20. lv (Latvian)
  21. nl (Dutch)
  22. pl (Polish)
  23. pt (Portuguese, Brazil)
  24. pt-pt (Portuguese, European)
  25. ro (Romanian)
  26. sk (Slovak)
  27. sv (Swedish)
  28. tr (Turkish)

These parameters can be sent as query parameters or form body when using HTTP POST. Beware that long passages should be splitted in parts to avoid long processing times. If you need to render a long passage, send the text with HTTP POST because query parameters have a length limit of 2,000 characters.

Here’s an example of the speech:¬†http://ow.ly/kHtxK. If you like, please help me do this survey in order to improve the service.