<?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>DEV Community: Roman</title>
    <description>The latest articles on DEV Community by Roman (@kield01).</description>
    <link>https://dev.to/kield01</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%2F35009%2F40ba281b-987e-4cea-ad1a-bec400be8cb7.jpeg</url>
      <title>DEV Community: Roman</title>
      <link>https://dev.to/kield01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kield01"/>
    <language>en</language>
    <item>
      <title>Time Optimization</title>
      <dc:creator>Roman</dc:creator>
      <pubDate>Thu, 29 Aug 2019 10:16:05 +0000</pubDate>
      <link>https://dev.to/kield01/time-optimization-1b03</link>
      <guid>https://dev.to/kield01/time-optimization-1b03</guid>
      <description>&lt;h2&gt;
  
  
  Why for?
&lt;/h2&gt;

&lt;p&gt;From time to time I used to use some web projects, which helps me to find something out within the game I play (Ingress Prime).&lt;/p&gt;

&lt;p&gt;So, recently I have started collection banners, images, with the missions.&lt;br&gt;
To see the banners, I use &lt;a href="https://ingressmosaik.com"&gt;Ingress Mosaic&lt;/a&gt; website.&lt;/p&gt;

&lt;p&gt;To reach the SpecOps Onyx medal, I have to complete 500 missions, but to get in top - at least 1400. So, to count the missions and length I am using Laravel 5.8 with the CLI.&lt;/p&gt;
&lt;h2&gt;
  
  
  What I did?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Made an artisan command with the &lt;code&gt;php artisan make:command&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Installed dependency of &lt;code&gt;fabpot/goutte&lt;/code&gt; to make an HTTPS Requests and work with the content efficiently&lt;/li&gt;
&lt;li&gt;Learned the structure of the pages I need to work with&lt;/li&gt;
&lt;li&gt;Go Code!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firstly, I had to make an alias for the name of the command to be executed from the CLI. I've came up with the next shorthand: &lt;code&gt;profile:parser {nickname}&lt;/code&gt;.&lt;br&gt;
30 minutes - and I've came with the next code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        /** Fetching profile page **/
        $this-&amp;gt;http = new Client([
            'base_uri' =&amp;gt; 'https://ingressmosaik.com'
        ]);

        /** Transforming the response to the DOM **/
        $dom = new Crawler($this-&amp;gt;http-&amp;gt;get("/profil/{$this-&amp;gt;argument('nickname')}")-&amp;gt;getBody()-&amp;gt;getContents());
        $todoItems = $dom-&amp;gt;filter('div#this_todo &amp;gt; div');

        $mosaics = [];

        /** Fetching no. of the mosaics **/
        $todoItems-&amp;gt;each(function (Crawler $node) use (&amp;amp;$mosaics) {
            $link = $node-&amp;gt;filter('h5 &amp;gt; a')-&amp;gt;attr('href');
            list(, , $no) = explode('/', $link);
            $mosaics[] = intval($no);
        });

        $targets = [
            'cities' =&amp;gt; [],
            'missions' =&amp;gt; 0,
            'distance' =&amp;gt; 0.00
        ];

        collect($mosaics)
            -&amp;gt;each(function (int $id) use (&amp;amp;$targets) {
                /** Fetching mission page **/
                $missionPage = new Crawler($this-&amp;gt;http-&amp;gt;get("/mosaic/{$id}")-&amp;gt;getBody()-&amp;gt;getContents());
                $title = $missionPage-&amp;gt;filter('div.panel-heading-1 &amp;gt; h4');
                $block = $missionPage-&amp;gt;filter('div#mo_img &amp;gt; div.panel-body-1');

                list($title,) = explode(' - ', $title-&amp;gt;text());

                $city = $block-&amp;gt;filter('div &amp;gt; span')-&amp;gt;eq(1)-&amp;gt;text();
                $mc = $block-&amp;gt;filter('div &amp;gt; span')-&amp;gt;eq(6)-&amp;gt;text();
                $lt = $block-&amp;gt;filter('div &amp;gt; span')-&amp;gt;eq(7)-&amp;gt;text();
                $startPortal = $missionPage-&amp;gt;filter('div#portals &amp;gt; table')-&amp;gt;eq(0)
                    -&amp;gt;filter('a')
                    -&amp;gt;eq(1);

                $title = trim($title);

                $this-&amp;gt;output-&amp;gt;text("Parsing '{$title}'");

                $targets['cities'][$city][] = [
                    'mosaic' =&amp;gt; trim($title),
                    'first_portal' =&amp;gt; $startPortal-&amp;gt;attr('href'),
                    'missions' =&amp;gt; intval($mc),
                    'distance' =&amp;gt; floatval($lt)
                ];

                $targets['missions'] += intval($mc);
                $targets['distance'] += floatval($lt);
            });

        $targets['distance'] = (float)number_format($targets['distance'], 2);

        foreach ($targets['cities'] as $city =&amp;gt; $mosaicsList) {
            $targets['cities'][$city] = collect($mosaicsList)
                -&amp;gt;sortBy('missions', SORT_DESC, true);
        }

        file_put_contents(public_path('missions.json'), json_encode($targets, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

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

&lt;/div&gt;



&lt;p&gt;Yes, this code is ugly, as all of it is in the &lt;code&gt;handle()&lt;/code&gt; method. But it's a quick solution to calculate all to-do banners and save all data into the JSON file.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>cli</category>
      <category>web</category>
    </item>
  </channel>
</rss>
