issue114:tuto2
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue114:tuto2 [2016/11/18 08:20] – d52fr | issue114:tuto2 [2016/11/19 11:39] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
In the previous part of the series, we covered setting up a small Sqlite database, then building a command-line Free Pascal program to access it. Finally, we integrated the database code into our Free Vision application through a new Dialog type to connect to the database and display data retrieved. In this fourth part of the series, we will connect to the Internet in order to refresh the information in our database directly from the Full Circle Magazine website.** | In the previous part of the series, we covered setting up a small Sqlite database, then building a command-line Free Pascal program to access it. Finally, we integrated the database code into our Free Vision application through a new Dialog type to connect to the database and display data retrieved. In this fourth part of the series, we will connect to the Internet in order to refresh the information in our database directly from the Full Circle Magazine website.** | ||
- | Dans cette série d' | + | Dans cette série d' |
- | Dans la partir précédente | + | Dans la dernière partie |
**Tools to connect to the network | **Tools to connect to the network | ||
Ligne 21: | Ligne 21: | ||
apt install fp-units-net | apt install fp-units-net | ||
- | Ceci nous rendra plusieurs units disponibles, | + | Ceci rendra plusieurs units disponibles, |
**The well-known library libcurl is standard in many POSIX environments, | **The well-known library libcurl is standard in many POSIX environments, | ||
“A free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. libcurl supports HTTPS certificates, | “A free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. libcurl supports HTTPS certificates, | ||
+ | |||
+ | La bibliothèque bien connue libcurl est standard dans de nombreux environnements POSIX et le projet Free Pascal l'a rendue disponible dans une unit appropriée. La bibliothèque CURL ou C-URL (prononcez " | ||
+ | |||
+ | « Une bibliothèque de transfert d'URL côté client, libre et facile à utiliser, supportant DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. libcurl supporte les certificats HTTPS, HTTP POST, HTTP PUT, le téléversement FTP, Kerberos, SPNEGO, le téléversement basé sur le format HTTP, les proxy, les cookies, l' | ||
**However, most Ubuntu installations contain the libcurl library itself, but not the header files. In order to use it in conjunction with a compiled language such as Free Pascal, we will need to install the corresponding header files as well. The actual version may change across distro versions, but in Ubuntu 16.04 and Linux Mint 18 the following command should set you up: | **However, most Ubuntu installations contain the libcurl library itself, but not the header files. In order to use it in conjunction with a compiled language such as Free Pascal, we will need to install the corresponding header files as well. The actual version may change across distro versions, but in Ubuntu 16.04 and Linux Mint 18 the following command should set you up: | ||
Ligne 36: | Ligne 40: | ||
What we are basically looking for is a list of recent publications of Full Circle Magazine. This is quite a simple task for a human reader: simply navigate to the web page, and read the articles choosing those with “Full Circle Magazine” in the title (as opposed to “Weekly News”) or something else. However, it is more difficult for a computer program than for a human to do this, since it must somehow learn to distinguish between articles and background elements, images, etc. Modern web pages are in fact quite complex assemblies of information. So let us help our program by using the facilities built into a modern Content Management System (CMS) such as Wordpress used by FCM. One of these is the Rich Site Summary (RSS) feed.** | What we are basically looking for is a list of recent publications of Full Circle Magazine. This is quite a simple task for a human reader: simply navigate to the web page, and read the articles choosing those with “Full Circle Magazine” in the title (as opposed to “Weekly News”) or something else. However, it is more difficult for a computer program than for a human to do this, since it must somehow learn to distinguish between articles and background elements, images, etc. Modern web pages are in fact quite complex assemblies of information. So let us help our program by using the facilities built into a modern Content Management System (CMS) such as Wordpress used by FCM. One of these is the Rich Site Summary (RSS) feed.** | ||
+ | |||
+ | Cependant, la plupart des installations Ubuntu contiennent la bibliothèque libcurl, mais pas les fichiers d' | ||
+ | |||
+ | apt install libcurl4-gnutls-dev | ||
+ | |||
+ | Comme dernière note, on peut dire que CURL est rapide - habituellement plus rapide que d' | ||
+ | |||
+ | Qu' | ||
+ | |||
+ | Au départ, nous sommes à la recherche d'une liste des publications récentes du Full Circle Magazine. C'est une tâche très simple pour un lecteur humain : naviguer simplement jusqu' | ||
**An RSS feed is a way of querying the site’s database of articles. Most CMS allow us to build a query along several different lines. In the case of FCM, we can use the following query to get a list of all articles: | **An RSS feed is a way of querying the site’s database of articles. Most CMS allow us to build a query along several different lines. In the case of FCM, we can use the following query to get a list of all articles: | ||
Ligne 45: | Ligne 59: | ||
$ curl http:// | $ curl http:// | ||
+ | |||
+ | Un flux RSS est une façon d' | ||
+ | http:// | ||
+ | |||
+ | Si nous l' | ||
+ | |||
+ | Si ça intéresse quelqu' | ||
+ | |||
+ | $ curl http:// | ||
**Other queries can be handled in a similar fashion. For example, to locate all articles that are tagged with “podcast”, | **Other queries can be handled in a similar fashion. For example, to locate all articles that are tagged with “podcast”, | ||
Ligne 62: | Ligne 85: | ||
This is what we will be using to help our search for new issues.** | This is what we will be using to help our search for new issues.** | ||
+ | |||
+ | D' | ||
+ | http:// | ||
+ | |||
+ | http:// | ||
+ | |||
+ | http:// | ||
+ | |||
+ | En fait, Ronnie inclut une version simplifiée du flux pour le podcast Weekly News : | ||
+ | |||
+ | http:// | ||
+ | |||
+ | Ce mécanisme peut être utilisé pour spécifier les articles publiés par un auteur en particulier. Comme les articles annonçant un nouveau numéro du FCM sont invariablement de Ronnie, nous pouvons utiliser cette URL pour les filtrer : | ||
+ | |||
+ | http:// | ||
+ | |||
+ | C'est ce que nous allons faire pour nous aider dans notre recherche des nouveaux numéros. | ||
**Combining the pieces | **Combining the pieces | ||
Ligne 77: | Ligne 117: | ||
The first variable is simply the URL we will be passing on to libcurl, though in PChar format. Pascal strings were from its origin arrays of 256 bytes. Bytes in positions 1 to 255 held the ASCII codes for each character, while position 0 held the total number of characters in the string. Needless to say, this scheme has several limitations, | The first variable is simply the URL we will be passing on to libcurl, though in PChar format. Pascal strings were from its origin arrays of 256 bytes. Bytes in positions 1 to 255 held the ASCII codes for each character, while position 0 held the total number of characters in the string. Needless to say, this scheme has several limitations, | ||
+ | |||
+ | Combiner des éléments | ||
+ | |||
+ | À ce stade, nous avons un moyen de nous connecter au Web pour télécharger une page unique, à partir de son URL. En revanche, nous avons une URL spécifique qui nous permet d' | ||
+ | |||
+ | uses | ||
+ | LibCurl; | ||
+ | |||
+ | Var | ||
+ | URL : Pchar = ' | ||
+ | | ||
+ | hCurl : pCurl; | ||
+ | | ||
+ | La première variable est simplement l'URL que nous voulons passer à libcurl, dans le format PChar. Depuis l' | ||
**The second variable is a pointer to the CURL routine that will handle our connection. The following code has been directly copied from the unit’s example: | **The second variable is a pointer to the CURL routine that will handle our connection. The following code has been directly copied from the unit’s example: | ||
Ligne 93: | Ligne 147: | ||
The complete code of this program is available here: http:// | The complete code of this program is available here: http:// | ||
+ | |||
+ | La seconde variable est un pointeur vers la routine CURL qui gère notre connexion. Le code suivant a été copié directement de l' | ||
+ | |||
+ | hCurl:= curl_easy_init; | ||
+ | if Assigned(hCurl) then | ||
+ | begin | ||
+ | curl_easy_setopt(hCurl, | ||
+ | curl_easy_setopt(hCurl, | ||
+ | |||
+ | curl_easy_perform(hCurl); | ||
+ | curl_easy_cleanup(hCurl); | ||
+ | end; | ||
+ | | ||
+ | Un processus de gestion est paramétré avec curl_easy_init. Si cela fonctionne (nous avons accès à une bibliothèque dynamique et assez de mémoire libre), nous pouvons alors paramétrer deux options (une sortie verbeuse à l' | ||
+ | |||
+ | Le code complet de ce programme est disponible ici : http:// | ||
**If we compile and run the above program, we simply get the XML code returned by the FCM server echoed on screen. To do something useful with this data, we could for example write it to a file. To be more precise, we will need to get the CURL handler to do this for us. The process will take place in two steps. | **If we compile and run the above program, we simply get the XML code returned by the FCM server echoed on screen. To do something useful with this data, we could for example write it to a file. To be more precise, we will need to get the CURL handler to do this for us. The process will take place in two steps. | ||
Ligne 111: | Ligne 181: | ||
begin | begin | ||
f: | f: | ||
+ | | ||
+ | Si nous compilons et lançons le programme ci-dessus, nous obtenons simplement l' | ||
+ | |||
+ | Dans la première étape, nous allons passer au gestionnaire une fonction de rappel pour transformer la structure de ses données internes en flux de mots. Cette fonction a besoin d' | ||
+ | |||
+ | Function DoWrite(Ptr : Pointer; Size : size_t; nmemb: size_t; Data : Pointer) : size_t; cdecl; | ||
+ | |||
+ | begin | ||
+ | DoWrite := TStream(Data).Write(Ptr^, | ||
+ | end; | ||
+ | |||
+ | Dans la seconde étape, nous créerons un nouveau flux de fichier et indiquerons au gestionnaire d' | ||
+ | |||
+ | Var | ||
+ | f : TFileStream; | ||
+ | |||
+ | begin | ||
+ | f: | ||
**Continue by setting up hCurl options as before, but add: | **Continue by setting up hCurl options as before, but add: | ||
Ligne 123: | Ligne 211: | ||
In this part of our series on Free Pascal, we deviated a bit from Free Vision and went into the technical details on how to use the CURL library from Pascal to connect to an RSS feed on FCM’s content management system. At this stage, we know how to connect to the server and download an XML file containing a list of recent articles published on the web page. In the next part of our series, we will see how to parse the XML code to retrieve the information we are aiming for: issue numbers and download URLs.** | In this part of our series on Free Pascal, we deviated a bit from Free Vision and went into the technical details on how to use the CURL library from Pascal to connect to an RSS feed on FCM’s content management system. At this stage, we know how to connect to the server and download an XML file containing a list of recent articles published on the web page. In the next part of our series, we will see how to parse the XML code to retrieve the information we are aiming for: issue numbers and download URLs.** | ||
+ | |||
+ | Continuez en paramétrant les options de hCurl comme précédemment et ajoutez : | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | Les niveaux curl_easy_perform and curl_easy_cleanup prennent la même place que précédemment. N' | ||
+ | |||
+ | f.Free; | ||
+ | | ||
+ | Le code complet de ce programme est disponible par ce lien : http:// | ||
+ | |||
+ | Dans cette partie de notre série sur Free Pascal, nous nous sommes un peu éloignés de Free Vision et sommes rentrés dans des détails techniques sur comment utiliser la bibliothèque CURL à partir de Pascal pour se connecter au flux RSS du CMS du FCM. À ce stade, nous savons comment nous connecter au serveur et télécharger un fichier XML contenant une liste des articles récents publiés sur la page Web. Dans la prochaine partie de notre série, nous regarderons comment analyser le code XML pour récupérer l' |
issue114/tuto2.1479453632.txt.gz · Dernière modification : 2016/11/18 08:20 de d52fr