<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[cod3 - Medium]]></title>
        <description><![CDATA[Stuff I wish had been on top of Google results when I was stuck, mostly about web development. Also anything I feel like sharing. - Medium]]></description>
        <link>https://medium.com/cod3?source=rss----c75caa50c9fe---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>cod3 - Medium</title>
            <link>https://medium.com/cod3?source=rss----c75caa50c9fe---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 02:28:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/cod3" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create an Amazon wish-list from the Facebook pages you “like” (with Zero server)]]></title>
            <link>https://medium.com/cod3/create-an-amazon-wish-list-from-the-facebook-pages-you-like-with-zero-server-61d021c5981?source=rss----c75caa50c9fe---4</link>
            <guid isPermaLink="false">https://medium.com/p/61d021c5981</guid>
            <category><![CDATA[bulma]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[zero-server]]></category>
            <dc:creator><![CDATA[Julien]]></dc:creator>
            <pubDate>Mon, 11 Mar 2019 15:13:35 GMT</pubDate>
            <atom:updated>2019-03-10T11:40:44.412Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4A383_QeFImzh6e7tocOpg.png" /><figcaption>Here’s a capture of the result to get you excited (with pudding)</figcaption></figure><p>When my friends ask me what I want for my birthday I usually draw a blank.</p><p>Then I start to think, it lasts about three months, and when I’m done thinking, my birthday is over, I have no friends left, and I use my own money to buy self-help books on Amazon Prime.</p><p>But there is one place where I’ve spent literally hours of my life creating a catalog of the things I like: <strong>Facebook</strong>. When I really like an artist, a movie, a product or anything really, my first reaction is to go to Facebook and “<em>like</em>” his/her/its page immediately.</p><p><strong><em>If only </em>there was a place where I could display a list of my Facebook “<em>liked</em>” pages, and search for Amazon products related to that page!</strong></p><p>That’s how came the idea for this app, which consists of four parts:</p><ul><li>Fetch your “<em>liked</em>” pages from Facebook</li><li>Fetch products related to a keyword from Amazon</li><li>Create the API</li><li>Build the UI</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FzaezT79s3Ng7C%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fliz-lemon-lets-do-this-zaezT79s3Ng7C&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2FzaezT79s3Ng7C%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/84da20b7ffddaf133e60ffacb1dd7096/href">https://medium.com/media/84da20b7ffddaf133e60ffacb1dd7096/href</a></iframe><h4>Zero server</h4><p><a href="https://github.com/remoteinterview/zero">remoteinterview/zero</a></p><p>I stumbled upon <strong>Zero server</strong>’s Github page and the idea sounded too good to be true, and it was a perfect match for my project, so I decided to take it for a test drive.</p><p>To spoil you a little, some of its features are really clever and work perfectly, but my personal experience with it has been riddled with bugs, memory crashes, very long starting times, and to be honest I don’t think I will use it again before it matures a little.</p><h4>Are you local?</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FYa2o92Smq6Ila%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fcomedy-Ya2o92Smq6Ila&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYa2o92Smq6Ila%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="208" frameborder="0" scrolling="no"><a href="https://medium.com/media/a233a8fc2293bb0d9c620a2920d31714/href">https://medium.com/media/a233a8fc2293bb0d9c620a2920d31714/href</a></iframe><p>An important lesson that I learned from building websites with <a href="https://www.gatsbyjs.org/">Gatsby</a>, is that building static websites / apps makes life 100% more tolerable.</p><p>With this project, the only end user is <strong>you</strong>. You want to access your own personal data, from your own locally-launched server, from your own command line. You don’t need to worry about deployment, security, bugs, best practices or anything.</p><p>If your application sucks, you’re the only person that will suffer.</p><h3>Fetching your liked pages from Facebook</h3><p>To fetch your liked pages from Facebook, you need a Facebook account and an application.</p><h4>Create a Facebook application</h4><p>We need an application to be able to query the <strong>Facebook Graph API</strong>.<br>The Facebook Graph API is the endpoint where you can access any public information that Facebook has on you or anyone, depending on your app’s privacy settings.</p><p>If you don’t already have a Facebook application, you can use the following tutorial to create a new one:</p><p><a href="https://www.nukesuite.com/support/social-applications/creating-application-from-facebook-developers/">Creating application from Facebook Developers</a></p><h4>Simulate your query</h4><p>Before starting to code, we can use the <strong>Graph API Explorer</strong>, which is a tool developed by Facebook to query their API.</p><p><a href="https://developers.facebook.com/tools/explorer">Graph API Explorer - Facebook for Developers</a></p><p>We’re going to create a script that we will run just once, to fetch our own “<em>likes</em>” and save them to a local database, so we don’t have to manage access tokens from our script, we can just generate one from the Graph API Explorer and hard-code it into our script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0d82eRDtP-l8K-U6qXZyVQ.png" /><figcaption>Just click on the big blue “Get Access Token” button</figcaption></figure><p>On the “<em>address</em>” bar, you can see “<em>me?fields=id,name</em>”. <br>This is the place where you define the kind of properties that you want to retrieve for the user — “me” in this case, which means you, “me” will be replaced with you Facebook user id internally.</p><p>For this project, we need to fetch the name of the pages you liked, so we have to consult the <strong>Graph API Reference</strong> to find the name of this property.</p><p><a href="https://developers.facebook.com/docs/graph-api/reference">Graph API Reference - Documentation - Facebook for Developers</a></p><p>A quick search for “<em>likes</em>” brought me to this particular property:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nFH2AjAmiTD5A3mXiUofZg.png" /></figure><p>Sounds about right. <br>First we have to update the <strong>Permissions</strong> that we will use for this request:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*EbIPKadTjZtJfU9OYsyLCA.png" /><figcaption>“user_likes” auto-completed when I typed “like”</figcaption></figure><p>Then we have to update the “<em>address</em>” bar with the “<em>likes</em>” property.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rd77eR2_VV8mwGfiRxRcHw.png" /></figure><p>And trigger the <strong>Submit</strong> button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q-75KCqa6kupf_X2RJt9QQ.png" /></figure><p>Now we have our access token, and we know the parameters that we must send to the API to retrieve our user likes.</p><p>We have all the information we need to start to code!</p><h4>Query the API from your script</h4><p>We will create a small script that does the same query, but programmatically:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ffc5b058d593fee5e1fd18218afe7bd/href">https://medium.com/media/5ffc5b058d593fee5e1fd18218afe7bd/href</a></iframe><p>This is what the final script looks like. We’re using the “<em>fb</em>” npm package to query the “<em>me/likes</em>” path, which is the equivalent of the “<em>me?fields=likes</em>” query that we saw earlier.</p><p>Facebook’s pagination uses <strong>cursors</strong>, as long as you haven’t hit the last page of results, it sends an “<em>after</em>” value in “<em>paging.cursors.after</em>”, which is a token that you must send with your query to display the next page.</p><p>So in a <em>while</em> loop, we <em>concat</em> all the elements returned by the “<em>me/likes</em>” query until the query returns an empty array.</p><h3>Fetch products related to a keyword from Amazon</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3d01NemOfuEKiMzbLU%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fsnl-saturday-night-live-season-44-3d01NemOfuEKiMzbLU&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3d01NemOfuEKiMzbLU%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="242" frameborder="0" scrolling="no"><a href="https://medium.com/media/c62c9df27fbe8ed586d212cebf4d3f57/href">https://medium.com/media/c62c9df27fbe8ed586d212cebf4d3f57/href</a></iframe><h4>Accessing the API</h4><p>It takes a couple of minutes or less to get access to Facebook’s Graph API.</p><p>To get an official access for Amazon’s “<em>Product Advertising API</em>”, you must send an application, that will be reviewed by a real Amazon employee made of flesh and blood, with answers to “<em>How many millions of unique visitors does your website get daily?</em>”, and “<em>Are you blood-related to Jeff Bezos?</em>”.</p><p>After being rejected twice, I decided that I would abandon the idea and find a better way to spend my free time.</p><p>Just kidding.</p><h4>Simulating the query</h4><p>The first thing we will do is to go to amazon.com and to search for …erm … something.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G7x__A5u3vBdmD_jvxzhpg.png" /><figcaption>Don’t judge if you have never played.</figcaption></figure><p>We can remove the “<em>&amp;ref=…</em>” bit and the search still works. So we know that we can search for anything by prefacing our query with “<em>https://www.amazon.com/s?k=</em>”.</p><p>We need the details about each result, so we use a super-secret tool called the inspector (right-click / Inspect in Chrome).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4t6AH1CDweCtKHk9LQp4YQ.png" /></figure><p>There’s lots of information but if you’ve been watching the Matrix movies many times in a row like I have, you can find out how to access the interesting data pretty easily, with CSS selectors.</p><h4>Walking on the wild side</h4><p>Since Amazon won’t let us use its API, we have no choice but to “<em>scrape</em>” the search results.</p><p>According to Google, “<em>scraping</em>” means “<em>spreading (butter or margarine) thinly over bread</em>”, but in this case I think the right definition is “<em>copy (data) from a website using a computer program</em>”.</p><p>There is an npm package that does this easily: <strong>scrape-it</strong>.</p><p><a href="https://github.com/IonicaBizau/scrape-it#readme">IonicaBizau/scrape-it</a></p><p>So using the search URL and the CSS selectors that we have found in the previous chapter, we can build the following script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c72dd5f37ec68614325ab7da294f716/href">https://medium.com/media/1c72dd5f37ec68614325ab7da294f716/href</a></iframe><p>It’s so simple it speaks for itself: we name the fields we want to retrieve image, title, price, link and stars, and for each of them we specify the CSS selector and the attribute of the matching node.</p><p>The result looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BSSz9tG0hpx67qz-WESnmA.png" /></figure><h3>Creating the API</h3><p>Now that we have our two libraries (for fetching Facebook likes and Amazon search results), we can start build our own API endpoints, that will be consumed by our user interface.</p><p>With Zero server, creating a node.js server script is incredibly easy, if you create /mypath/myscript.js for example, you will be able to run it from <a href="http://localhost:3000/mypath/myscript.">http://localhost:3000/mypath/myscript.</a></p><p>So we will create two API endpoints that will use the two library scripts we just created. For the record, here’s what my project folder looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*h7q32iFBCUUrf8e554xh8w.png" /></figure><p>My low-level library files are placed in /lib/, my API scripts are placed in /api/ — Neat and clean, easy to understand. <br>Ignore the other files and folders for now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67322a4ab1ae4e91a8d5d7f8a03731c0/href">https://medium.com/media/67322a4ab1ae4e91a8d5d7f8a03731c0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f21e8995a862553fdf5817f84559e78/href">https://medium.com/media/8f21e8995a862553fdf5817f84559e78/href</a></iframe><p>As you can see, both files have a pretty similar behaviour.</p><p>We have a get/load function that loads a value from the database if it exists, calls the library if it doesn’t, saves the value into the database and returns the result. The goal of the database is to reduce the calls to the source website as much as possible.</p><p>Facebook, for example, has a pretty strict rate limit so if you didn’t use a database to cache the results, you would get errors after only a few calls.</p><p>The Amazon script handles the query in the URL as well.</p><p>For the database I used <strong>keyv</strong>, which is a key/value storage library that I really recommend, since it’s very easy to use, has a single purpose and does it extremely well. For this project I used an SQLite database but it supports a lot of other storage strategies.</p><p><a href="https://github.com/lukechilds/keyv">lukechilds/keyv</a></p><h3>Building the UI</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F1BH7Xygg3hgWxm71HG%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fhistoryuk-al-murray-hatetheenglish-on-a-train-1BH7Xygg3hgWxm71HG&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F1BH7Xygg3hgWxm71HG%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdadcdd08ed30a909fe4549094a25444/href">https://medium.com/media/cdadcdd08ed30a909fe4549094a25444/href</a></iframe><h4>All aboard the Rant Express</h4><p>Again, some things are just magic with Zero server, for example, if you want to create a React front-end for your application, you just have to create a .jsx file. Then re-launch the server. Then wait for it to re-build. Then to wait for it to fetch the dependencies. Then to manually add the dependencies because it crashed. Then to launch it ag… Oh, it’s working.</p><p>…Then you launch the URL in your browser. Then it fetches the dependencies again ohgodwhy. Ok, it says it’s ready. Oh, it says it again. Let’s move the mouse on the wind… It’s rebuilding.</p><p>I’m exaggerating a bit, but not so much, when it’s ready it could be a real game-changer because it simplifies some of the processes involved in web development so much, but it’s just too instable for me to recommend it this early.</p><h4>Choice of weapons</h4><p>Rant over, for the UI I chose Bulma which is a pretty popular UI kit, and more specifically the <strong>react-bulma-components</strong> package by <em>couds</em>:</p><p><a href="https://github.com/couds/react-bulma-components">couds/react-bulma-components</a></p><p><strong>Side note</strong>: I spent too much time trying to figure out how to implement FontAwesome in React the way FontAwesome recommends.</p><p>The real solution here is <strong>react-icons</strong>, which just works the way you would expect.</p><p><a href="https://github.com/react-icons/react-icons">GitHub - react-icons/react-icons: svg react icons of popular icon packs</a></p><p>To consume the API, all I had to do was to use the native node <strong>fetch</strong> function, like so:</p><pre>const resp = await fetch(&#39;/api/facebook-likes&#39;);<br>const likes = await resp.json();</pre><p>I believe there isn’t much more I could comment on, you will have to try the result by yourself! The source code is available on Github:</p><p><a href="https://github.com/bouiboui/facebook-likes-to-amazon-wishlist">https://github.com/bouiboui/facebook-likes-to-amazon-wishlist</a></p><p>Thank you for reading, feel free to comment if you have any question!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61d021c5981" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cod3/create-an-amazon-wish-list-from-the-facebook-pages-you-like-with-zero-server-61d021c5981">Create an Amazon wish-list from the Facebook pages you “like” (with Zero server)</a> was originally published in <a href="https://medium.com/cod3">cod3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to export your Facebook saved links to Pocket]]></title>
            <link>https://medium.com/cod3/how-to-export-your-facebook-saved-links-to-pocket-c34828dd5f8e?source=rss----c75caa50c9fe---4</link>
            <guid isPermaLink="false">https://medium.com/p/c34828dd5f8e</guid>
            <category><![CDATA[freedom]]></category>
            <category><![CDATA[pocket]]></category>
            <category><![CDATA[facebook]]></category>
            <dc:creator><![CDATA[Julien]]></dc:creator>
            <pubDate>Thu, 05 Apr 2018 11:28:23 GMT</pubDate>
            <atom:updated>2020-09-06T08:37:21.751Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<p>This is a quick and dirty fix, but it helped me export 1300+ links to Pocket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/0*bnfO2SqPDif1GRlR.jpg" /></figure><p>I find a lot of useful content on Facebook and their <strong>Save</strong> button is very convenient for reading links later, but I’m also a huge fan of Pocket, especially for reading articles on my mobile phone, so I wanted to find a solution to get the best of both worlds.</p><p><strong><em>Edit (2020–09–06): </em></strong><em>I open-sourced the Google Chrome Extension on Github:</em></p><p><a href="https://github.com/bouiboui/facebook-saved">bouiboui/facebook-saved</a></p><p><strong><em>Edit (2019–07–12):</em></strong><em> I took the time to convert all the scripts in this page to a Google Chrome extension, which should be more convenient for most people. Check out the link below!</em></p><p><a href="https://chrome.google.com/webstore/detail/ecpknffljbjgdccapjaflmpfoclakjcg">Export Facebook saved links</a></p><h3>What you will need</h3><ul><li>Any Pocket account</li></ul><p><a href="https://getpocket.com/">Pocket</a></p><ul><li>The Batch Save Pocket extension</li></ul><p><a href="https://chrome.google.com/webstore/detail/batch-save-pocket/ihhiomekhplpdojbcaniaglcfopckaef">Batch Save Pocket</a></p><h3>Instructions</h3><ol><li>Go to <a href="https://www.facebook.com/saved">your “Saved” page</a></li><li>Scroll all the way down until all links are loaded<br>Keep your “PageDown” key pressed, or paste this in your adress bar:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99b07cd49b71e9e0961477f61b8e4a69/href">https://medium.com/media/99b07cd49b71e9e0961477f61b8e4a69/href</a></iframe><p>3. Open your console, paste this and hit Enter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b54df9c4dd42f76258c37fc467a7f103/href">https://medium.com/media/b54df9c4dd42f76258c37fc467a7f103/href</a></iframe><p>This will output all the external links, without duplicates, separated by space characters, similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4JXEvV62bKFP-sxA_uZnQ.png" /></figure><p>4. Copy the output, Google Chrome provides a handy <strong>Copy </strong>button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/207/1*zKilwP-zMv5pWF2dIA3DEA.png" /></figure><p>5. Paste the output in Batch Save Pocket’s <strong>Paste Links</strong> tab</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*ELSGwQAXoOtKK3vqE_rmLw.png" /></figure><p>6. Click on <strong>Parse</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*_rqiQKZmVml3rEhIE_IODQ.png" /></figure><p>7. Click on <strong>Submit to Pocket</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*XBgngXtM10kS-qQzwGjZcA.png" /></figure><p>Depending on the number of links and your computer speed, you might have to sit tight for a few minutes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*M-NN4ni1dx7JXCP5vY8rbw.png" /></figure><p>And voilà!</p><p>Your links are now free to enjoy from anywhere.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*V0-mpLKVoZDKmZNoDMzysg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c34828dd5f8e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cod3/how-to-export-your-facebook-saved-links-to-pocket-c34828dd5f8e">How to export your Facebook saved links to Pocket</a> was originally published in <a href="https://medium.com/cod3">cod3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a better search engine for PHP dependencies]]></title>
            <link>https://medium.com/cod3/building-a-better-search-engine-for-php-dependencies-47501787b62?source=rss----c75caa50c9fe---4</link>
            <guid isPermaLink="false">https://medium.com/p/47501787b62</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[composer]]></category>
            <category><![CDATA[packagist]]></category>
            <dc:creator><![CDATA[Julien]]></dc:creator>
            <pubDate>Sat, 31 Mar 2018 12:08:33 GMT</pubDate>
            <atom:updated>2018-04-04T06:34:48.021Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<blockquote><strong>Library</strong> (<em>computer science</em>) A collection of software subprograms that provides functionality, to be incorporated into or used by a computer program.<br><a href="https://en.wiktionary.org/wiki/library">https://en.wiktionary.org/wiki/library</a></blockquote><p><strong>TL;DR</strong>: Try <a href="https://phpkg.com/">https://phpkg.com</a>, a new search engine for PHP dependencies.</p><p><a href="https://phpkg.com/">PHPkg: PHP packages search engine</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IkYORR686aKlJ8FJ.jpg" /></figure><h4>The age of composer</h4><p>Long gone is the time when you add to copy and paste thousands of lines from the internet, or download and unzip a library in your project, making updates impossible, your code base size unmanageable and your code vulnerable.</p><p>Today you just <em>composer require</em> any piece of code and it becomes easy to implement, version, update, and even replace if a better alternative comes along.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/0*-Rg940vKn2pzvDwo.jpg" /></figure><h4>Why would I trust some code that I didn’t write myself?</h4><p>When you add a feature to your project, you can start from scratch — also known as “reinvent the wheel”, or you can use a piece of code that has been time-tested, peer-reviewed, that has already solved all the problems you’re going to face and that already covers use cases that you may need in the future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*M_pAGHQ7D1fKQBWX1N7UzA.gif" /></figure><p>It’s a huge gain of time, and by looking at code that was developed by other people, you might learn a lot about programming, best practices, design patterns and so on.</p><p>You might also find a way to make the code better, and suggest your modification to the author, so that everyone can benefit from your talent.</p><h4>What do you look for when you add a dependency to your project?</h4><p>Here is a non-exhaustive list of questions that I ask myself each time I have to pick a dependency, in no particular order:</p><ul><li><strong>Is the documentation clear?</strong></li></ul><p>This is surprisingly the most recurring error that authors make when they share their librairies on Github, they put a lot of effort into making amazing code, but they spend very little time making sure that other programmers can easily install and use their code.</p><ul><li><strong>Will it be easy to integrate in my project?</strong></li></ul><p>If I have to re-compile PHP, use a specific version of Linux, spend 15 minutes waiting for the code to build, just to support one specific feature, I will reconsider.</p><p>This also applies to code that is tightly coupled to its dependencies, making features exponentially heavy.</p><blockquote>You wanted a banana but what you got was a gorilla holding the banana and the entire jungle — <em>Joe Armstrong, creator of Erlang</em></blockquote><ul><li><strong>Do I like the code style?</strong></li></ul><p>To each his own taste, but if I have to support and maybe debug a library in the future, I’d rather use one where I understand what the author meant, and find some familiarity in the decisions he made.</p><p>If all my code uses camelCase and is object-oriented, and the author writes PHP $like = we_used_to_in_the_nineties(), I’m going to pass.</p><ul><li><strong>Does it have tests?</strong></li></ul><p>Tests are not infaillible, but at the very minimum they’re a clue that the author intended to do things the right way. If I use a library and I find a bug, I know I can write a test that reproduces the behavior and make it easier to fix.</p><p>Sometimes it’s also a good way to understand how you are meant to use the library the way the author intended to do a specific action.</p><blockquote>Those are very important, but they’re hard to spot automatically with accuracy. In contrast, the following questions are easy to answer, and they’re available at one glance in the search engine I’m presenting to you in this post.</blockquote><ul><li><strong>Has it been around for a long time?</strong></li></ul><p>Why would you trust <em>any</em> product that’s been around for a longer time than another?</p><p>This is about the survival of the fittest, a product that just came out in the market hasn’t yet faced all the obstacles that a product with a longer life has, so you can’t predict whether it will be able to solve them in the future.</p><p>You also don’t know if the author will support it in the near future, or abandon it and leave you with an incomplete piece of code that can potentially become incompatible or vulnerable.</p><ul><li><strong>Has it been updated recently?</strong></li></ul><p>Computer programming evolves fast, PHP has made tremendous changes in the last years, and code quickly becomes obsolete. Code that takes advantage of the newest features and optimizations that PHP offers gets bonus points. And don’t get me started on PHP7 Typing!</p><p>Vulnerabilities are also a point of concern, the longer a piece of code has been around, the longer a malicious person has had the time to find a security hole.</p><ul><li><strong>Does the author acknowledge the issues?</strong></li></ul><p>The bigger a library gets, the more users adopt it on their own projects, the more feedback it gets. Whether the author acknowledges the users’ feature requests, suggestions and bug reports or ignores them should make a big impact on your decision.</p><p>Remember, you may be one of the hundreds of users complaining about the same bug in the future.</p><ul><li><strong>Is it popular?</strong></li></ul><p>This is controversial and I kept it last in full conscience, but to me, again considering the survival of the fittest, the popularity of a project often comes as a token of viability.</p><p>Of course this is unfair, because projects with more exposure will automatically get more stars, someone working in a big company with thousands of developers will get an unfair advantage just by asking his/her colleagues to vote for him/her, while an independent package author will start with 0 stars and strive to get every new star.</p><p>I am fully aware of this, being a victim of this myself, but at the same time, it doesn’t mean that stars are meaningless and that popular projects don’t deserve their amount of stars. For example, I will happily star projects with less than ten stars, and almost never star huge projects who don’t need it, and I’m thinking I’m not the only one, which counter-balances the problem.</p><h3>Introducing PHPkg</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ighUV1S86P8UaWDDudW2Wg.png" /><figcaption><a href="https://phpkg.com/#oauth2">https://phpkg.com/#oauth2</a></figcaption></figure><p>Before you read more, I suggest you to <a href="https://phpkg.com/">try it by yourself</a>. If you’ve ever used Packagist or Github to search for packages in the past — which I think you do since you’ve read this far — you should instantly understand the benefits.</p><ul><li>Google-like <strong>instant search</strong> functionality</li><li>Built with <strong>speed</strong> in mind</li><li><strong>Dedicated </strong>to PHP dependencies</li><li>Displays <strong>date since creation</strong>, <strong>last update </strong>and <strong>number of issues</strong></li><li>Provides the <em>composer require</em> command in one click</li></ul><h4>Why don’t you use Packagist?</h4><p>PHPkg is not trying to replace Packagist, its only focus is speed and convenience when searching for a package.</p><p>Also, I find it very frustrating that I can’t sort Packagist results by Downloads or Stars. PHPkg’s sorting system is straight to the point. And its logo is a bit sexier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*q0canSXASwkQnsUML9hlXA.png" /><figcaption>That may just be my own taste though</figcaption></figure><h4>Why don’t you use Github? or X? or Y?</h4><p>Because PHPkg is exactly the search engine I was looking for, it does one thing, and it does it the best and the fastest it can.</p><p>There’s nothing wrong with the alternatives, but I believe PHPkg could make the life of PHP developers easier.</p><p>Thank you for reading, and don’t hesitate to add your comments and suggestions to make it better.</p><h4>Ready to give it a go?</h4><p><a href="https://phpkg.com/">PHPkg: PHP packages search engine</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47501787b62" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cod3/building-a-better-search-engine-for-php-dependencies-47501787b62">Building a better search engine for PHP dependencies</a> was originally published in <a href="https://medium.com/cod3">cod3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install Ghost on Debian + Apache]]></title>
            <link>https://medium.com/cod3/install-ghost-on-debian-apache-cf6c49478714?source=rss----c75caa50c9fe---4</link>
            <guid isPermaLink="false">https://medium.com/p/cf6c49478714</guid>
            <category><![CDATA[ghost-blog]]></category>
            <category><![CDATA[debian]]></category>
            <category><![CDATA[apache]]></category>
            <dc:creator><![CDATA[Julien]]></dc:creator>
            <pubDate>Wed, 24 Jan 2018 20:05:43 GMT</pubDate>
            <atom:updated>2019-11-25T07:46:13.318Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<blockquote><a href="https://ghost.org/"><em>Ghost</em></a><em> is a simple, powerful publishing platform that allows you to share your stories with the world.</em></blockquote><p>Despite the amount of information available online, I spent hours trying to setup my previous blog. I’ll try to make it easier for you by telling you exactly how I finally succeeded.</p><p>This is not a perfect guide, I’m not an expert, I’m just sharing what worked for me. If you find something that could be perfected, <strong><em>please</em></strong> let me know.</p><p><em>Edit (2015–08–03):</em> Totally forgot to talk about <em>forever</em>. <a href="http://ghost.cod3.net/install-ghost-on-debian-and-apache/#what-to-do-next">This is now fixed</a>. Thanks to <em>BadAtParties</em> on <a href="https://www.reddit.com/r/webdev/comments/3fj1p5/oc_install_ghost_on_debian_apache/">the Reddit thread</a>.</p><p><strong>Versions</strong></p><p>At the time of writing, <strong>Debian</strong>’s latest stable version is <strong>8.1</strong> « jessie », <strong>Apache</strong>’s current version is <strong>2.4.10</strong> and <strong>Ghost</strong>’s latest github release is <strong>0.6.4</strong>. If you’re using anything else I can’t predict the results.</p><p><strong>Warning: I won’t write <em>sudo</em> all the time, I assume you have the correct permissions. Also, Don’t forget to replace <em>youruser</em>, <em>yourserver</em> and so on.</strong></p><p>The very first thing you should do is to <strong>create a DNS entry</strong> for your blog, to make your domain or subdomain point to your server’s IP. This is usually done via your domain registrar’s interface, and depending on your registrar it could take from few minutes to several days (which is a good sign to change for a better one).</p><p>Once it’s done, fire up your favourite terminal and copypaste the following:</p><pre># Login to your server via SSH<br>ssh youruser@yourserver.tld</pre><pre># Download the latest updates<br>apt-get update</pre><pre># Install/update node (and npm)<br>apt-get install nodejs</pre><h3>Apache setup</h3><p>I use VirtualHosts, I assume you do too. <br>If you don’t there are <a href="https://debian-handbook.info/browse/stable/sect.http-web-server.html">countless very good tutorials</a> available.</p><pre># Go to Apache VirtualHosts&#39; config folder<br>cd /etc/apache2/sites-available</pre><pre># Create a .conf file for your Ghost blog<br>nano ghost.yourserver.tld.conf</pre><p>Yes, I’m using <em>nano</em>. It’s awesome. Write this:</p><pre>&lt;VirtualHost *:80&gt;  <br>    ServerName ghost.yourserver.tld<br>    ProxyPreserveHost On<br>    ProxyPass / http://127.0.0.1:2368/<br>&lt;/VirtualHost&gt;</pre><p>Ctrl + O to save, Enter to confirm, Ctrl + X to quit. <br>And finally, enable your new VHost:</p><pre>a2ensite ghost.yourserver.tld.conf  <br>service apache2 reload</pre><p><strong><em>NB</em></strong><em>: Your Apache setup may require “a2enmod proxy” before restarting Apache. (thanks </em><a href="https://medium.com/@dan.hetherington_11386/your-apache-setup-may-require-a2enmod-proxy-before-restarting-apache-feafb4c94e96"><em>Dan Hetherington</em></a><em>)</em></p><p>The following is <strong>optional</strong>. <br>I tried a lot of things when I couldn’t get it to work, including this, so I don’t know if it did anything or not. If you’re stuck, try it, but avoid it if you can.</p><pre># Open the default port used by Ghost (2368)<br>iptables -I INPUT 1 -p tcp --dport 2368 -j ACCEPT  <br>iptables save</pre><h3>Ghost setup</h3><p>Go to the directory where you want to install Ghost, create it if need be.</p><pre>mkdir /your/path &amp;&amp; cd $_</pre><p>Now is the time to find out if you’re lucky. There are 3 options available, if you’re lucky all you will need is a one-liner.</p><p><strong>1. First, try this:</strong></p><pre>npm install ghost --production</pre><p>Edit Ghost’s config with your blog’s URL</p><pre>nano config.js</pre><p>Hit Ctrl + W to prompt the Search function, type my-ghost-blog and hit Enter.</p><p><em>nano</em> will jump to the relevant position in the file, find and replace my-ghost-blog.com with ghost.yourserver.tld (and https with http if relevant).</p><p>Let’s try to start Ghost:</p><pre>npm start --production</pre><p>Open a web browser, cross your fingers and go to ghost.yourserver.tld. <br>If you see Ghost&#39;s welcome page <a href="http://ghost.cod3.net/install-ghost-on-debian-and-apache/#what-to-do-next">you&#39;re good to go</a>.</p><p><strong>2. Second chance</strong></p><p>If it did not work, rm -Rf everything in the directory and try the following:</p><pre># Download Ghost&#39;s latest version from ghost.org/download<br>wget https://ghost.org/zip/ghost-0.6.4.zip</pre><pre># Unzip it in your directory<br>unzip ghost-0.6.4.zip</pre><pre># Remove the zip file<br>rm ghost-0.6.4.zip</pre><pre># Install Ghost<br>npm install --production</pre><p>And then try again to start Ghost (see above).</p><p><strong>3. Third time’s the charm</strong></p><p>We’ve tried the shortcuts, but sometimes it just won’t work. Fortunately this last option was the solution for me. We’ll be doing what the documentation refers to as <em>Developer install</em>. It’s not lean, it’s slow, but at this point I bet you’re willing to resort to anything.</p><p><strong>Install/update dependencies</strong></p><p>You will get Ghost’s latest version from Github with <em>git</em> and build some stuff with <em>make</em>, which is part of Debian’s <em>build-essentials</em> package. Grunt is used as well, and must be installed globally.</p><pre>apt-get install git build-essentials  <br>npm install -g grunt-cli</pre><p>Once again, start by rm -Rfing the folder, and then build and install Ghost:</p><pre># Clone Ghost&#39;s latest version from Github<br>git clone git://github.com/tryghost/ghost.git</pre><pre># Go to the directory and checkout the stable version<br>cd ghost &amp;&amp; git checkout stable</pre><pre># Proceed to the installation<br>npm install</pre><pre># Build with grunt, takes a while<br>grunt init</pre><pre># Minify things for production<br>grunt prod</pre><p>And finally, <a href="http://ghost.cod3.net/install-ghost-on-debian-and-apache/#start-ghost">try one last time</a>.</p><h3>Nothing worked</h3><p>If at this point it doesn’t work, I’m afraid there’s nothing else I can do for you. Sometimes things suck. Try reloading apache, check if your port is open from antoher Terminal, check your DNS, ping your (sub)domain. Hit <a href="http://stackoverflow.com/questions/tagged/ghost">StackOverflow</a> maybe. <a href="https://www.youtube.com/watch?v=9u0EL_u4nvw">Sorry</a>.</p><h3>One last step</h3><blockquote><em>Nothing is forever, then what makes </em>Ghost <em>the exception?</em></blockquote><p>We finally have our Ghost blog up and running, but as soon as we hit Ctrl + C to go back to the shell, the blog is offline. We have to use some tool to keep it running all the time, there are <a href="http://support.ghost.org/deploying-ghost/#making-ghost-run-forever">many options</a>, my favourite is <em>forever</em>.</p><pre># Install forever <br>npm install forever -g</pre><pre># Start Ghost using forever from the Ghost installation directory<br>NODE_ENV=production forever start index.js</pre><p>To check if Ghost is currently running type forever list, to stop Ghost type forever stop index.js.</p><p><strong>Side note:</strong> I’ve been using forever for a long time, and there’s a feature I love, which is the --watch, aka -w option. It tells <em>forever</em> to watch Ghost&#39;s files all the time and restart automatically if it detects any change. This is perfect for updates, or on a development environment, but I don&#39;t know if it would be a good thing on a production server. If you&#39;ve tried, please advise.</p><p>You may wish to start <em>forever</em> at boot, I recommend <a href="http://stackoverflow.com/a/13388741">this method</a>.</p><h3>What to do next</h3><p>If you reached this point Ghost is installed and the fun begins. <br>Go to ghost.yourserver.tld/ghost and follow the instructions.</p><p>This is where I leave you. <br>Hope you liked my first post, hopefully there will be more to come.</p><p><strong>Footnotes</strong></p><ol><li><a href="http://wiltonsoftware.com/posts/view/finding-your-debian-and-ubuntu-install-version">“Finding your Debian […] install version”</a> — Wilson Software <a href="http://ghost.cod3.net/install-ghost-on-debian-and-apache/#fnref:1">↩</a></li><li><a href="http://www.cyberciti.biz/faq/find-out-apache-version/">“Find out Apache version”</a> — nixCraft <a href="http://ghost.cod3.net/install-ghost-on-debian-and-apache/#fnref:2">↩</a></li><li>I use <a href="https://msysgit.github.io/">Git Bash</a> on Windows, Terminal w/ <a href="http://ohmyz.sh/">Oh My Zsh</a> on MacOs, <a href="https://www.youtube.com/watch?v=Sagg08DrO5U">Debian’s bash</a> on Debian <a href="http://ghost.cod3.net/install-ghost-on-debian-and-apache/#fnref:3">↩</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf6c49478714" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cod3/install-ghost-on-debian-apache-cf6c49478714">Install Ghost on Debian + Apache</a> was originally published in <a href="https://medium.com/cod3">cod3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[This operation requires an interactive window station]]></title>
            <link>https://medium.com/cod3/this-operation-requires-an-interactive-window-station-7509b8889888?source=rss----c75caa50c9fe---4</link>
            <guid isPermaLink="false">https://medium.com/p/7509b8889888</guid>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[android-studio]]></category>
            <dc:creator><![CDATA[Julien]]></dc:creator>
            <pubDate>Wed, 24 Jan 2018 19:59:25 GMT</pubDate>
            <atom:updated>2018-01-24T19:59:24.512Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<p><strong>TL;DR:</strong> Leave your device plugged to your PC and “Refresh your PC”</p><p>As an Android developer I’ve been trying to install the USB driver for my Nexus 5 for ADB/debugging purposes. Worked perfectly under Windows 7, works perfectly at home… Fails miserably at my parents’ for some reason.</p><p>After going through the <a href="https://en.wikipedia.org/wiki/K%C3%BCbler-Ross_model#Stages">5 stages of grief</a> and finally accepting that I would never be able to develop the ultimate Flappy bird clone while on vacation, I stumbled upon several forums that pointed me to the right direction. But first, let’s point out the problem.</p><h3>The problem</h3><ul><li>You plug in your USB device</li><li>It appears unknown in the Device Manager</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/0*gvK7ejjPuX7vyQuq.png" /></figure><ul><li>You right-click and try to Update the Driver Software</li><li>You pick the driver… and then BAM!</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/0*z8fnC13BoPMUXA4f.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/0*S8WPdQQz8r4GxXkM.gif" /></figure><p>Ok, so you’ve got the right driver, the right version, you’re an Administrator… it’s just Windows being a little bitch.</p><h3>The solution</h3><p>Well, kinda. A real solution would be for Microsoft to publish a fix, or to move some files around or something, but after trying many “solutions” that didn’t work (changing the permissions to the drivers and Driverstore folders, enabling the administrator account and installing the driver from there, Rebooting the computer and authorizing unsigned drivers, …) I found a solution that worked for me.</p><p><strong>Disclaimer / Caveat</strong> You won’t lose your personal files (love letters written in notepad, pictures of kitten), but you <strong>will</strong> have to re-install all your software. All of it. Browser, media player, IDE, cracked Adobe suite… Everything. Now, saving everything in the cloud doesn’t seem like such a bad idea, right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/0*CkTNmPcj8mAT9hPV.gif" /></figure><p>This is unconvenient but it’s a step above the previous best solution that was to reformat your hard drive and install Windows again.</p><p><strong>Step 0: Keep your device plugged in the USB port (or plug it right now)</strong></p><p>And then I’ll just Google and copy-paste “Refresh Windows 10”:</p><blockquote><em>Step 1: Open the Start menu, and then select Settings &gt; Update &amp; security &gt; Recovery.</em></blockquote><blockquote><em>Step 2: Under Reset this PC, select Get started.</em></blockquote><blockquote><em>Step 3: Select Keep my files.</em></blockquote><p>Your computer is going to restart and repair your Windows installation. Mine got stuck a long time on 8%, and then went through anyway, so be patient.</p><p>When it restarts, you’ll still have your users, your desktop background, everything but your applications. Go back to the device manager, right click on the Unknown device, update it… And this time it works like a charm.</p><p>Takes an hour or so, but now you can start a new life, like nothing happened and eat your spaghetti like everything’s right in the world.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7509b8889888" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cod3/this-operation-requires-an-interactive-window-station-7509b8889888">This operation requires an interactive window station</a> was originally published in <a href="https://medium.com/cod3">cod3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convert speech from an audio file to text using Google Speech API]]></title>
            <link>https://medium.com/cod3/convert-speech-from-an-audio-file-to-text-using-google-speech-api-b951f4032a64?source=rss----c75caa50c9fe---4</link>
            <guid isPermaLink="false">https://medium.com/p/b951f4032a64</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Julien]]></dc:creator>
            <pubDate>Wed, 24 Jan 2018 19:54:29 GMT</pubDate>
            <atom:updated>2019-09-18T13:23:53.093Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h3>The backstory</h3><p>I had to transcribe messages recorded from an iPhone in the m4a format, with a duration of 30 seconds to a couple of minutes, to text. After trying several APIs, I found the one from Google, without much surprise, to be the most accurate.</p><p>If you own an Android phone, you can expect about the same accuracy as when you use the “OK Google” application to send an SMS message. So something like 80% hit, 20% gibberish. Which you know could be worse, if you have ever used the Youtube auto-caption feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*8cgKBQvkGhDlCxSo.jpg" /></figure><p>This created the following issues for the project:</p><ul><li>The expected input format for the Google Speech API is LINEAR16 PCM (.wav), not m4a.</li><li>Audio files that last more than 1 minute must be uploaded to Google Storage, you can’t send them to the Google Speech API directly.</li></ul><h3>The tasks</h3><p>Let’s split the problem into simple tasks:</p><ul><li>Create a Google Cloud account</li><li>Convert the audio file to LINEAR16</li><li>Upload the converted file to Google Storage</li><li>Send the uploaded file to the Speech API</li><li>Display the transcription into the console</li></ul><p><strong>Create a Google Cloud account</strong></p><p>I won’t really go into details for this, just go to <a href="https://cloud.google.com/">https://cloud.google.com/</a> and follow the steps for signing up. You will have to create a service account for your application, so follow the instructions at <a href="https://cloud.google.com/speech/docs/common/auth#set_up_a_service_account">Creating a Service Account</a>.</p><p>Like instructed, save the JSON file generated with your credentials locally, and create a copy at the root of your project (as credentials.json for example).</p><p><strong>Convert the audio file to LINEAR16</strong></p><p>The Google Speech API documentation specifies that the expected input format is LINEAR16, but never really explains concretely how to convert an existing file to this format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/0*W1tkFoFK4DzggVid.jpg" /></figure><p>My knowledge about audio formats is really limited, but after a little while I found <a href="https://groups.google.com/forum/#!topic/cloud-speech-discuss/tbQHoaTTNH8">this conversation</a> and more specifically, this comment by <em>droidha…@gmail.com</em>:</p><blockquote><em>And here is an example for those that might be using ffmpeg to get audio from video sources or just using ffmpeg for conversion</em></blockquote><blockquote><em>ffmpeg foo.mp4 -f s16le -acodec pcm_s16le -vn -ac 1 -ar 16k foo.raw</em></blockquote><blockquote><em>the point to note is that its is s16le (little endian (intel) byte ordering) and this also down mixes to mono with -ac 1 and the -ar (audio rate) is 16k</em></blockquote><p><em>Edit (2019–09–18): It seems that the ffmpeg command is a bit off, see the answer below if you want to try it from the command line:</em></p><p><a href="https://medium.com/@jcrben/i-believe-your-ffmpeg-command-is-a-bit-off-as-it-is-missing-the-i-and-should-be-7915e94398ab">I believe your ffmpeg command is a bit off as it is missing the -i and should be:</a></p><p>Though I’m still not sure if the last line really means something or was auto-generated by a Twitter bot, I downloaded the FFmpeg binary for my platform, tried the command and it worked!</p><p>So I had to code a node.js version of this command. I found a node module that wraps FFmpeg (fluent-ffmpeg). It’s pretty well done but requires <a href="https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#prerequisites">a specific installation of FFmpeg</a>.</p><p>After copying and tweaking bits from similar projects, I came up with the following code:</p><pre>&#39;use strict&#39;;</pre><pre>const ffmpeg = require(&#39;fluent-ffmpeg&#39;);  <br>const mime = require(&#39;mime&#39;);  <br>const fs = require(&#39;fs&#39;);</pre><pre>module.exports = (filePathIn, filePathOut) =&gt; new Promise((resolve, reject) =&gt; {  <br>    if (!filePathIn || !filePathOut) {<br>        throw new Error(&#39;You must specify a path for both input and output files.&#39;);<br>    }<br>    if (!fs.existsSync(filePathIn)) {<br>        throw new Error(&#39;Input file must exist.&#39;);<br>    }<br>    if (mime.lookup(filePathIn).indexOf(&#39;audio&#39;) &gt; -1) {<br>        try {<br>            ffmpeg()<br>                .input(filePathIn)<br>                .outputOptions([<br>                    &#39;-f s16le&#39;,<br>                    &#39;-acodec pcm_s16le&#39;,<br>                    &#39;-vn&#39;,<br>                    &#39;-ac 1&#39;,<br>                    &#39;-ar 16k&#39;,<br>                    &#39;-map_metadata -1&#39;<br>                ])<br>                .save(filePathOut)<br>                .on(&#39;end&#39;, () =&gt; resolve(filePathOut));</pre><pre>        } catch (e) {<br>            reject(e);<br>        }<br>    } else {<br>        throw new Error(&#39;File must have audio mime.&#39;);<br>    }<br>});</pre><p>Since I crave geek-cred, I published <a href="https://github.com/bouiboui/linear16">the source on Github</a> and <a href="https://www.npmjs.com/package/linear16">a package on npm</a>:</p><pre>npm i --save linear16</pre><p><strong>Upload the converted file to Google Storage</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/0*9QOBTBsiwXu1Meaw.png" /></figure><p>First, you will have to create a “bucket”, a place where your files will be uploaded. You can do so through the SDK, I did it manually. It’s very intuitive but <a href="https://cloud.google.com/storage/docs/creating-buckets">here’s a tutorial</a> if you need some help.</p><p>The Google node SDK is pretty easy to use, just authenticate using your project id and your credentials file, select the bucket where you want to upload your files and specify the path of the file to upload:</p><pre>const gcs = require(&#39;@google-cloud/storage&#39;)({  <br>    projectId: &#39;your-projectid-12345&#39;,<br>    keyFilename: &#39;./credentials.json&#39;<br>});</pre><pre>const bucket = gcs.bucket(&#39;your-bucket-name&#39;);</pre><pre>module.exports = filePath =&gt; new Promise((resolve, reject) =&gt;  <br>    bucket.upload(filePath, function (err, file) {<br>        if (err) {<br>            reject(err);<br>        } else {<br>            resolve(file);<br>        }<br>    })<br>);</pre><p><em>Don’t forget to replace “your-project-id” and “your-bucket-name” in this script.</em></p><p>Once uploaded, the files are accessible throughout the Google API ecosystem via a special path, following this pattern:</p><pre>gs://your-bucket-name/your-file-name.ext</pre><p><strong>Send the uploaded file to the Google Speech API</strong></p><p>The Speech API is also pretty easy to use, we have to specify the input format and use the <em>startRecognition</em> method. A pretty interesting feature of the Speech API is that it recognizes <a href="https://cloud.google.com/speech/docs/languages">a wide range of languages</a>.</p><pre>const speechClient = require(&#39;@google-cloud/speech&#39;)({  <br>    projectId: &#39;your-projectid-12345&#39;,<br>    keyFilename: &#39;./credentials.json&#39;<br>});</pre><pre>const options = {  <br>    &#39;languageCode&#39;: &#39;en-US&#39;,<br>    &#39;sampleRate&#39;: 16600,<br>    &#39;encoding&#39;: &#39;LINEAR16&#39;<br>};</pre><pre>module.exports = fileName =&gt;  <br>    new Promise((resolve, reject) =&gt; {<br>            speechClient.startRecognition(fileName, options, function (err, operation) {<br>                if (err) {<br>                    return reject(err)<br>                }<br>                operation<br>                    .on(&#39;error&#39;, function (err) {<br>                        return reject(err);<br>                    })<br>                    .on(&#39;complete&#39;, function (results) {<br>                        return resolve(results);<br>                    });<br>            });<br>        }<br>    );</pre><p><em>Don’t forget to replace “your-project-id” and “your-bucket-name” in this script.</em></p><p>Since we’re dealing with an asynchronous request, the use of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promises</a> is pretty fitting. Once the transcription is ready, the Promise will resolve with the resulting text.</p><p><strong>Display the transcription into the console</strong></p><p>At first I just displayed the text in the console, but since the transcription takes a little long (up to 1 minute for a minute-long file), I wanted a visual indicator that everything works correctly.</p><p>The simplest node module I found for this is the Spinner from <a href="https://github.com/nathanpeck/clui">clui</a>, which is an awesome pack of command-line UI components.</p><p>The result looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/0*5C5r9zuFSZY8aPHm.gif" /></figure><p>I also used <a href="https://www.npmjs.com/package/chalk">chalk</a> to add some colours to the output.</p><p><strong>Putting it all together</strong></p><p>I saved each component as a module:</p><ul><li>I published the LINEAR16 converter as the linear16 package on npm, so I just have to call require(&#39;linear16&#39;) to use it.</li><li>I saved the Storage uploader as libs/cloud-storage.js and the Speech transcriber as libs/cloud-speech.js)</li><li>And I created a index.js main file to orchestrate all the operations.</li></ul><p>I think the result is pretty simple to follow, and it works!</p><pre>&#39;use strict&#39;;</pre><pre>const linear16 = require(&#39;linear16&#39;);  <br>const Spinner = require(&#39;clui&#39;).Spinner;</pre><pre>const cloudStore = require(&#39;./libs/cloud-storage&#39;);  <br>const cloudSpeech = require(&#39;./libs/cloud-speech&#39;);</pre><pre>const path = require(&#39;path&#39;);  <br>const chalk = require(&#39;chalk&#39;);</pre><pre>try {</pre><pre>    const countdown = new Spinner(`Starting...`);<br>    countdown.start();</pre><pre>    const params = {<br>        input: &#39;./input/input.m4a&#39;,<br>        output: &#39;./output/output.wav&#39;<br>    };</pre><pre>    Promise.resolve(params)<br>        .then(paths =&gt; {<br>            countdown.message(`Converting ${path.basename(paths.input)} to ${path.basename(paths.output)}...`);<br>            return linear16(paths.input, paths.output);<br>        })<br>        .then(wavFile =&gt; {<br>            countdown.message(`Storing ${path.basename(wavFile)}...`);<br>            return cloudStore(wavFile);<br>        })<br>        .then(storageFile =&gt; {<br>            countdown.message(`Transcribing ${storageFile.name}...`);<br>            return cloudSpeech(&#39;gs://messages-audio/&#39; + storageFile.name);<br>        })<br>        .then(transcription =&gt; {<br>            countdown.stop();<br>            console.log(chalk.green(transcription));<br>        })<br>        .catch(err =&gt; console.error(err));<br></pre><pre>} catch (err) {<br>    console.log(chalk.red(err.message));<br>    console.error(err);<br>}</pre><p>I hope you enjoyed this post, don’t hesitate to <a href="https://twitter.com/j_____________n">contact me on Twitter</a> if you have any question or comment!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b951f4032a64" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cod3/convert-speech-from-an-audio-file-to-text-using-google-speech-api-b951f4032a64">Convert speech from an audio file to text using Google Speech API</a> was originally published in <a href="https://medium.com/cod3">cod3</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>