<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Olayemi</title>
    <description>The latest articles on Forem by Michael Olayemi (@michaelin007).</description>
    <link>https://forem.com/michaelin007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F732018%2Fbc6392cf-ba24-4d44-afe0-7121be93b681.jpg</url>
      <title>Forem: Michael Olayemi</title>
      <link>https://forem.com/michaelin007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/michaelin007"/>
    <language>en</language>
    <item>
      <title>Secure Shell (SSH) Crates in Rust</title>
      <dc:creator>Michael Olayemi</dc:creator>
      <pubDate>Fri, 18 Oct 2024 02:47:11 +0000</pubDate>
      <link>https://forem.com/michaelin007/secure-shell-ssh-crates-in-rust-4ggh</link>
      <guid>https://forem.com/michaelin007/secure-shell-ssh-crates-in-rust-4ggh</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Secure Shell (SSH) allows us to communicate with a remote server on our local machine for various operations.&lt;br&gt;
One of the common operations is &lt;a href="https://crustc.com/reading-a-remote-file-rust/" rel="noopener noreferrer"&gt;reading a file remotely&lt;/a&gt;.&lt;br&gt;
Let's see some third-party SSH crates (dependencies) available in Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust SSH Crates
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://crates.io/crates/ssh2" rel="noopener noreferrer"&gt;ssh2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://crates.io/crates/ssh" rel="noopener noreferrer"&gt;ssh&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://crates.io/crates/fast-ssh" rel="noopener noreferrer"&gt;fast-ssh&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://crates.io/crates/ssh-rs" rel="noopener noreferrer"&gt;ssh-rs&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can use any of these crates in our future Rust project as an SSH client.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Multiple Page Web Scraper in Rust.</title>
      <dc:creator>Michael Olayemi</dc:creator>
      <pubDate>Fri, 24 Jun 2022 18:56:05 +0000</pubDate>
      <link>https://forem.com/michaelin007/multiple-page-web-scrapper-in-rust-3hnl</link>
      <guid>https://forem.com/michaelin007/multiple-page-web-scrapper-in-rust-3hnl</guid>
      <description>&lt;p&gt;Data is the new gold according to some of my folks. To some extent maybe they are right. Getting data especially massive data could be expensive. In this article, I will show you step-by-step how to build a multiple-page web scraper using rust programming language.&lt;/p&gt;

&lt;p&gt;Imagine you wanted to get the price of an iPhone x from an eCommerce website with multiple pages. You can achieve that manually but it will take a lot of time. Writing a code for the purpose will make it easy. In seconds you can scrape over 10 pages.&lt;/p&gt;

&lt;p&gt;Create a new project using cargo new multipage. We are going to the need the following dependencies, add them to your cargo.toml under dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reqwest={version="0.11.9", features=["blocking"]}
select="0.5.0"
anyhow="1.0.56"`

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reqwest is an http client and it will be useful for making http calls to the web pages will intend to scrap some data from.&lt;/p&gt;

&lt;p&gt;Select is the heart of this program. It's a cool crate that has functions to scrap HTML web pages.&lt;/p&gt;

&lt;p&gt;Anyhow will majorly help in handling errors.&lt;/p&gt;

&lt;p&gt;let's dive in:&lt;br&gt;
Firstly, we are going to inspect the web page we wanted to scrap. If you are using Chrome, right-click and pick the inspect option. inspect the HTML codes until you get to the elements of the web page you intended to scrap. I used this web link as a practice: &lt;br&gt;
"&lt;a href="https://www.jumia.com.ng/catalog/?q=iphone&amp;amp;viewType=grid&amp;amp;page=1#catalog-listing"&gt;https://www.jumia.com.ng/catalog/?q=iphone&amp;amp;viewType=grid&amp;amp;page=1#catalog-listing&lt;/a&gt;"&lt;br&gt;
The website is an eCommerce website. After inspecting the page, I got the HTML code for the catalog-listing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a class="core" href="/castillo-castillo-de-liria-red-75cl-x1-81158848.html" data-id="CA254FF1EJDRYNAFAMZ" data-name="Castillo De Liria RED 75cl x1" data-price="3.85" data-brand="Castillo" data-category="Grocery/Beer, Wine &amp;amp;amp; Spirits/Wine/Red Wine" data-dimension23="" data-dimension26="18"
 data-dimension27="4.6" data-dimension28="1" data-dimension37="0" data-dimension43="CP_MT38|CP_MT72|CP_MT73|CP_MT76|CP_MT77|CP_MT78|CP_MT80|CP_MT81|CP_MT84|CP_MT92|Camp_45|Camp_70|Camp_72|FDY2020|FDYJE|Merch_148|Merch_150" data-dimension44="0" data-list=""
  data-position="1" data-track-onclick="eecProduct" data-track-onview="eecProduct" 
  data-track-onclick-bound="true"&amp;gt;&amp;lt;div class="img-c"&amp;gt;&amp;lt;img data-src="https://ng.jumia.is/unsafe/fit-in/300x300/filters:fill(white)/product/84/885118/1.jpg?0770" src="https://ng.jumia.is/unsafe/fit-in/300x300/filters:fill(white)/product/84/885118/1.jpg?0770"
   class="img" width="208" height="208" alt=""&amp;gt;
   &amp;lt;img data-src="https://ng.jumia.is/badges/fdyje/10/138x18.png?4886" src="https://ng.jumia.is/badges/fdyje/10/138x18.png?4886" class="_ni camp" alt="FDYJE"&amp;gt;&amp;lt;/div&amp;gt;

   &amp;lt;div class="info"&amp;gt;&amp;lt;h3 class="name"&amp;gt;Castillo Castillo De Liria RED 75cl x1&amp;lt;/h3&amp;gt;
   &amp;lt;div class="prc"&amp;gt;₦ 1,725&amp;lt;/div&amp;gt;&amp;lt;div class="s-prc-w"&amp;gt;&amp;lt;div class="old"&amp;gt;₦ 1,898&amp;lt;/div&amp;gt;
   &amp;lt;div class="tag _dsct _sm"&amp;gt;9%&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
   &amp;lt;div class="rev"&amp;gt;&amp;lt;div class="stars _s"&amp;gt;4.6 out of 5&amp;lt;div class="in" style="width:91.99999999999999%"&amp;gt;
   &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;(18)&amp;lt;/div&amp;gt;&amp;lt;svg aria-label="Express Shipping" viewBox="0 0 114 12" class="ic xprss" width="94" height="10"&amp;gt;
   &amp;lt;use xlink:href="https://www.jumia.com.ng/assets_he/images/i-shop-jumia.c8de1c55.svg#express"&amp;gt;&amp;lt;/use&amp;gt;
   &amp;lt;/svg&amp;gt;&amp;lt;p class="shipp"&amp;gt;Jumia Express items in your order will be delivered for free (Lagos &amp;amp;amp; Abuja only, excluding large items)&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following attributes were use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;class="info"&amp;gt; as the major node.
&amp;lt;class="name"&amp;gt; to get the name of the product
&amp;lt;class="prc"&amp;gt; to get the price of the product
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Calling the URL with Reqwest
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut page = 1;

    while page != 10 {
        let url = format!(
            "https://www.jumia.com.ng/catalog/?q=iphone&amp;amp;viewType=grid&amp;amp;page={:?}#catalog-listing",
            page
        );
        let res = reqwest::blocking::get(url).with_context(|| format!("opening url error"))?;
let document = Document::from_read(res).context("parsing response")?;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing to note is that this is a multi-page scrapper, we are going to loop through different pages until we are satisfied. From the code above, I intend to go through the iPhone listing page 10 times and get the name of available iPhones and their price. To achieve looping through the page I used the format! macro, easy way for me to concatenate I guess. The macro will accept the page params as the program keeps looping through different pages.&lt;/p&gt;

&lt;p&gt;I used reqwest to accept the URL and used the document method in select to parse it to a file that will be readable by the program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let writer = OpenOptions::new()
            .read(true)
            .append(true)
            .create(true)
            .open("box.txt")
            .with_context(|| format!("opening file "))?;

        let mut writer = LineWriter::new(writer);

        let jumia = document.find(Class("info"));
        let link = document
            .find(Class("core"))
            .next()
            .context("writing to the output file")?;

        for node in jumia {
            let name = node
                .find(Class("name"))
                .next()
                .context("writing to the output file")?;
            let price = node
                .find(Class("prc"))
                .next()
                .context("writing to the output file")?;


            writeln!(
                writer,
                "{:?}---{:?}---{:?}",
                name.text(),
                price.text(),
                link.attr("href")
            )
            .context("writing to the output file")?;
        }
        page = page + 1;
    }

    Ok(())

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first part of the code specifies what the scrapped data will be saved as. It will be saved as box.txt.&lt;/p&gt;

&lt;p&gt;The paged is being pulled down by the document variable and every node can easily be assessed and data can be scraped out of it.&lt;/p&gt;

&lt;p&gt;Thanks for reading, you can also check &lt;a href="https://www.nutrisoft.xyz/"&gt;nutrisoft&lt;/a&gt;, my nutrition web application.&lt;/p&gt;

&lt;p&gt;Code available on &lt;a href="https://github.com/Michaelin007/multipage"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>rust</category>
      <category>webscrapping</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rust Project for Beginner: Natural Language Detector</title>
      <dc:creator>Michael Olayemi</dc:creator>
      <pubDate>Sat, 19 Feb 2022 18:38:06 +0000</pubDate>
      <link>https://forem.com/michaelin007/rust-project-for-beginner-natural-language-detector-i9k</link>
      <guid>https://forem.com/michaelin007/rust-project-for-beginner-natural-language-detector-i9k</guid>
      <description>&lt;p&gt;Rust is a lovely language, the more you learn it the more you love it. In this tutorial we are going to build a simple application that have the capacity to detect over 80 languages.&lt;br&gt;
This tutorial showcase the following concept in rust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using external crate in our project&lt;/li&gt;
&lt;li&gt;Creating functions in rust&lt;/li&gt;
&lt;li&gt;Using of macros&lt;/li&gt;
&lt;li&gt;Calling functions in rust&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To get starter open your terminal and run the command:&lt;code&gt;cargo init langdector&lt;/code&gt; If you don't have rust installed on your system, please head over to the official &lt;a href="https://www.rust-lang.org/"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Open the generated project in your vscode or your favourite IDE. Edit the cargo.toml file by adding this dependency:&lt;code&gt;whatlang = "0.13.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After adding the external create, we can proceed to our main.rs and begin applying the logic.&lt;/p&gt;

&lt;p&gt;We are going to create a function called &lt;code&gt;what_language&lt;/code&gt;. With the following codes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//calling the external crate
use whatlang::{detect, Lang, Script};

//creating a function
fn what_language(mut x: String) {
    let text = &amp;amp;x;
//using .unwrap() for exception handling
    let info = detect(text).unwrap();
//println! is one of the major macros used in rust
    println!("Language type {}", info.lang());
    println!("Language Script {}", info.script());
    println!("System confidence {}", info.confidence());
    println!("System reliability {}", info.is_reliable());

}

fn main() {
//creating string alice
    let alice = String::from(
        "Ĉu vi ne volas eklerni Esperanton? Bonvolu! Estas unu de la plej bonaj aferoj!",
    );
//calling the function
    what_language(alice);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Language type Esperanto
Language Script Latin
System confidence 1
System reliability true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comments explained each logic._ &amp;amp;x_ obeys the concept of ownership and borrowing.&lt;/p&gt;

&lt;p&gt;You can try to run the code :)&lt;/p&gt;

&lt;p&gt;Head over to &lt;a href="https://github.com/Michaelin007/Rust-Projects"&gt;github&lt;/a&gt; for the code.&lt;br&gt;
You can follow me of &lt;a href="https://www.twitter.com/michaelin007"&gt;twitter&lt;/a&gt; for collaboration.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Creating a Web Page with Actix-Web (Rust )</title>
      <dc:creator>Michael Olayemi</dc:creator>
      <pubDate>Wed, 01 Dec 2021 13:10:15 +0000</pubDate>
      <link>https://forem.com/michaelin007/creating-a-web-page-with-actix-web-rust--2agd</link>
      <guid>https://forem.com/michaelin007/creating-a-web-page-with-actix-web-rust--2agd</guid>
      <description>&lt;p&gt;Overview&lt;br&gt;
In this tutorial, we are going to build a simple website using actix, a stable framework based on rust programming language.&lt;br&gt;
The simple website is called bookstore: it is a simple website for book index.&lt;/p&gt;

&lt;p&gt;Rust is a young language, coming from Java background was so awesome, when I pick up rust, I fell in love with the language and felt like building everything in rust.&lt;/p&gt;

&lt;p&gt;There are cool frameworks in rust to build a website but I choose actix because to me it doesn't have any con yet.&lt;/p&gt;

&lt;p&gt;Let dive in, create an empty project with cargo (cargo new bookstore). Open the cargo.toml and add the following dependencies :&lt;br&gt;
&lt;code&gt;actix-web="3"&lt;br&gt;
actix-files="0.4.0"&lt;br&gt;
serde_json = "1.0.53"&lt;br&gt;
handlebars = { version = "4.1.4", features = ["dir_source"] }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The actix-web will help us to access the actix web framework.&lt;br&gt;
actix-files will help us to add static files to our project.&lt;br&gt;
Handlebar is a template engine that will help us to create template for our web page.&lt;/p&gt;

&lt;p&gt;Using Handlebars&lt;br&gt;
In the main.rs, paste the code below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

use actix_files::{Files, NamedFile};
use actix_web::{web, App, HttpResponse, HttpServer, Result};
use handlebars::Handlebars;
use serde_json::json;

async fn index(hb: web::Data&amp;lt;Handlebars&amp;lt;'_&amp;gt;&amp;gt;) -&amp;gt; HttpResponse {
    let data = json!({
        "project_name": "Book Store",
        "books":[
            {
                "name":"Harry Potter",
                "author":"J K Rowlings",
                "image_path":"/static/image/download.jpeg"
            },
            {
                "name":"Lord of the ring",
                "author":"Tolken",
                "image_path": "/static/image/lord_of.jpeg"
            },
            {
                "name":"Americanah",
                "author":"Chimamada Adichie",
                "image_path":"/static/image/americanah.jpeg"
            },
            {
                "name":"Elon Musk",
                "author":"#####",
                "image_path":"/static/image/elon.jpeg"
            },
        ]


    });

    let body = hb.render("index", &amp;amp;data).unwrap();
    HttpResponse::Ok().body(body)


}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Handlebar helps load and compile the json before before using them. The data created with json will be used in our project.&lt;/p&gt;

&lt;p&gt;In the main function, we have to initialize the handlebar and register the templates_directory.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#[actix_web::main]
async fn main() -&amp;gt; std::io::Result&amp;lt;()&amp;gt; {
    let mut handlebars = Handlebars::new();
    handlebars
        .register_templates_directory(".html", "./static/")
        .unwrap();
    let handlebars_ref = web::Data::new(handlebars);

    println!("listening on port 8080");
    HttpServer::new(move || {
        App::new()
            .app_data(handlebars_ref.clone())
            .service(Files::new("/static", "static").show_files_listing())
            .route("/", web::get().to(index))
    })
    .bind("127.0.0.1:9090")?
    .run()
    .await
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a directory in the root folder called static and inside the static create two folders for images and css file. Inside the directory create index.html and paste the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;title&amp;gt;{{project_name}}&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="static/css/index.css" type="text/css"&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;{{project_name}}&amp;lt;/h1&amp;gt;
    &amp;lt;section class="books"&amp;gt;
      {{#each books}}
        &amp;lt;article class="book"&amp;gt;
          &amp;lt;h3&amp;gt;{{this.name}}&amp;lt;/h3&amp;gt;
          &amp;lt;img src="{{this.image_path}}" /&amp;gt;
            &amp;lt;h4&amp;gt;Author: {{this.author}}&amp;lt;/h4&amp;gt;
        &amp;lt;/article&amp;gt;
      {{/each}}
    &amp;lt;/section&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Handlebar help us to render the data created in json into the html template, &lt;code&gt;{{project_name}}&lt;/code&gt; will be the project name. &lt;code&gt;{{#each books}}&lt;/code&gt; helps loop through the json to create the book index without writing a separate tag for each index.&lt;/p&gt;

&lt;p&gt;When you are done, run cargo run in your terminal and access the page with 127.0.0.1:9090&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxlwvcd5kh3pvwdyjo3s9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxlwvcd5kh3pvwdyjo3s9.png" alt="bookindex"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Head over to &lt;a href="https://github.com/Michaelin007/BookIndex.git" rel="noopener noreferrer"&gt;github to download the full code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you find this article useful and will like to tip me with &lt;a href="//coindrop.to/michael-olayemi"&gt;$1 worth of cryptocurrency, you can do it here and I will really appreciate&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
