- 投稿日:2020-03-28T21:45:16+09:00
「福井県オープンデータ ごみ収集日一覧CSVをJSONに変換するツール」README
※この記事は、私がGitHubに公開しているnode.js製ツール「福井県オープンデータ ごみ収集日一覧CSVをJSONに変換するツール」のREADMEを転載したものです。
解説は「恋に落ちるコード.js」の絵子と樹里です。
福井県オープンデータ ごみ収集日一覧CSVをJSONに変換するツール
「福井県オープンデータライブラリ」で公開されている、県内17市町の「ごみ収集日一覧」データの形式を、CSV(Shift-JIS)からJSONに変換します。
Description
樹里「というわけで、福井県内自治体のごみ収集日一覧JSONデータを作成するアプリを作ってみた」
絵子「ほう。なんでまた突然」
樹里「それがな、久しぶりに福井県オープンデータに公開されてるデータを使って、アプリでも作ってみようとしたんだがな」
絵子「福井県は昔からオープンデータの公開に熱心だよね」
樹里「で、ごみ収集日一覧のデータを見てみたんだが、なんとShift-JISのCSVなんだよ。この令和の時代に」
絵子「うーん。確かにそのままだと扱いにくいよね」
樹里「Shift-JISのCSVなんだよ。この令和の時代に」
絵子「2回言わなくていいよ。あと太字にしなくてもいいよ」
樹里「これではどうしようもないので、まずはちゃんとしたUTF-8のJSONに変換するツールから作ってみたわけだ」
絵子「なるほど。経緯はよくわかった」Usage
樹里「使い方は簡単。Node.js製のアプリなので……」
git clone https://github.com/8amjp/fukui-opendata-gomisyusyubi-json.git cd fukui-opendata-gomisyusyubi-json npm install樹里「上記のコマンドでインストールして、」
node index.js樹里「と実行すれば、
dist
ディレクトリにJSONデータが出力される」
絵子「あら本当に簡単」
樹里「じゃあ、アプリがどういう動きをするのか解説しよう」index.js
樹里「まずは、メインとなるコードだ」
index.js/* 「福井県オープンデータライブラリ」の「ごみ収集日一覧」ページで公開されている 収集日のCSVデータ(Shift-JIS)をJSONに変換します。 */ const scraper = require('./lib/scraper'); const generator = require('./lib/generator'); const page = 'https://www.pref.fukui.lg.jp/doc/toukei-jouhou/opendata/list_ct_gomisyusyubi.html'; // 「ごみ収集日一覧」ページのURL (async () => { // ページ内の17市町のCSVデータのURLを取得 const resources = await scraper.scrape(page) // すべてのCSVを取得してJSONに変換して出力 await Promise.all(resources.map(resource => generator.generate(resource))) console.log('できたよ!') })();絵子「
scraper
とgenerator
っていうのが、樹里が作ったモジュール?」
樹里「そう。scraper
で「ごみ収集日一覧」ページからCSVへのリンクを取得している。で、generator
でCSVを取得してJSONに変換している」
絵子「なるほど」
樹里「では、各モジュールの動きをみてみよう」lib/scraper.js
樹里「次に、ページをスクレイピングして、CSVへのリンクを取得する
scraper
モジュールだ」lib/scraper.js/* 指定されたページ内の、CSVへのリンクをを取得します。 */ const fetch = require('node-fetch'); const cheerio = require('cheerio'); const url = require('url'); module.exports.scrape = async (page) => { // 指定されたページのHTMLを取得する const response = await fetch(page) const body = await response.text() // cheerioでページをスクレイピング const $ = await cheerio.load(body) // 末尾が'.csv'のリンクをすべて取得 const relativePaths = await $('a[href$=".csv"]').map((i, el) => $(el).attr('href')).get() // 絶対パスに変換 const absolutePaths = await relativePaths.map(path => url.resolve(page, path)) return absolutePaths }樹里「スクレイピングにはcheerioというライブラリを使用している」
絵子「知ってる。jQueryっぽく操作できるやつだよね」
樹里「そう。そのcheerioで、属性セレクターを使って、href
属性 が ".csv" で終わるa
要素、すなわちCSVへのリンクを取得してだな、その配列を返している」
絵子「なるほど」lib/generator.js
樹里「最後に、データの変換を行う
generator
モジュールだ」lib/generator.js/* 指定されたURLのCSVを取得し、文字コードをShift-JISからUTF-8にに変換して出力します。 */ const path = require('path'); const fs = require('fs-extra'); const fetch = require('node-fetch'); const parse = require('csv-parse/lib/sync'); module.exports.generate = async (resource) => { // ファイル名を生成 const file = path.basename(resource, '.csv') + '.json' // 指定されたURLのCSVを取得 const response = await fetch(resource) const buffer = await response.arrayBuffer() // CSVの文字コードをShift-JISからUTF-8にに変換 const decoder = new TextDecoder("Shift_JIS") const csv = decoder.decode(buffer) // CSVをJSONに変換 const json = await parse(csv, { columns: true, trim: true }) // JSONを出力 const result = await fs.outputJson(path.join('dist', file), json, { spaces: 4 }) return result };樹里「まず、node-fetchでCSVを取得して、Shift-JISからUTF-8に変換する」
絵子「へー、TextDecoderっていうので文字コードを変換できるんだね」
樹里「で、csvというライブラリのcsv-parse
という機能を使って、CSVをJSONに変換しているわけだ」
絵子「便利なライブラリだねー」樹里「さて、処理の結果、このようなJSONが出力される」
[ { "行": "あ", "音": "あ", "町名": "在田町", "読み": "あいだ", "燃える": "火・金", "燃えない": "2・4木", "プラスチック製容器包装": "月", "カン": "1・3水", "ビン": "4水", "ペットボトル": "2水", "ダンボール": "3水", "蛍光灯": "4木", "キーワード": "清水", "備考": "清水南" }, // 以下略絵子「これ、キーが日本語になってるけど問題ないの?」
樹里「ああ、仕様に則った正しいJSONだぞ」
絵子「へー、そうなんだ」
樹里「なにより、Shift-JISのCSVよりははるかに扱いやすい」
絵子「よっぽどキライなんだね」
樹里「……さて、無事にShift-JISのCSVをJSONに変換できた」
絵子「めでたしめでたし、だね」
樹里「いやいや、データの形式を変換しただけで、何も出来上がってないぞ。大事なのは、このデータを使ってどんなアプリを作るかだ」
絵子「そりゃそうだ。さ、次はアプリ制作に挑戦だ!」Author
- 投稿日:2020-03-28T18:32:46+09:00
npmって?
npmって?
node package manager
Node.jsで使うパッケージを管理するために使う
Nodeをインストールすると一緒にインストールされるnpmのインストール
僕はMacOSにnodebrewでインストールした
nodebrewはNode.jsのバージョン管理システム
Homebrewでインストールする$ brew install nodebrew$ echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> ~/.bash_profile$ source ~/.bash_profile$ which nodebrewこれでnodebrewのパスが表示されればOK
この後,nodeをインストールして,その後npmをインストールする
npmコマンド
インストールディレクトリを確認
npm root
- 投稿日:2020-03-28T18:29:22+09:00
Rails6でアプリ起動時にWebpacker configuration file not foundエラーが発生した時の対処方法
執筆背景
scaffoldを使って簡単なアプリを作ろうと以下のコマンドを打ち込むと、Webpackerがインストールされていないというエラーが発生したため、記述しておこうと思う。
$ rails new app_name $ cd app $ rails g scaffold post content:text $ rails db:migrate $ rails s (中略) Webpacker configuration file not found /Users/ユーザ名/app_name/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /Users/ユーザ名/app_name/config/webpacker.yml (RuntimeError)ちなみに、このときのrubyのバージョンは2.7.0、railsのバージョンは、6.0.2.2です。
結論
このエラーは、webpackerがインストールされていない場合に起こるエラーみたいです(rails6からwebpackerが標準になったため)。
webpackerのインストールには、Node.jsとYarnのインストールが必要で、これらをインストールした上でwebpackerをインストールすると解決します。
作業内容
Gemfileを確認し、gem 'webpacker'の記述を確認。
Gemfilegem 'webpacker', '~> 4.0'bundle installしたのち、webpackerをインストールしようと試みるもNode.jsがインストールされていないと怒られる。
$ bundle install $ rails webpacker:install sh: node: command not found sh: nodejs: command not found Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/Node.jsをインストールしたのち、再度webpackerのインストールを試みると、Yarnをインストールしろと怒られる。
$ brew install node (中略) ==> node Bash completion has been installed to: /usr/local/etc/bash_completion.d $ rails webpacker:install Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/Yarnをインストールしたのち、再度webpackerのインストールを試みたところインストールすることができた。
$ sudo npm install -g yarn added 1 package in 3.509s $ yarn -v 1.22.4 $ yarn install success Saved lockfile. ✨ Done in 3.49s. $ rails webpacker:install ✨ Done in 6.34s. Webpacker successfully installed ? ?アプリを再起動。
$ rails s Use Ctrl-C to stop Started GET "/posts/index" for ::1 at 2020-03-28 18:06:09 +0900
- 投稿日:2020-03-28T18:22:21+09:00
Why should you hire a dedicated node.js developer?
Every business needs software to run their business in a world that is rapidly digitizing. More people prefer online monetary transactions nowadays than ever before, so business organizations also have to adapt accordingly. The technology used to build software usually keeps on evolving to make the systems more efficient. One such technology that has had a strong impact on software development is Node. Due to the several advantages it offers, many organizations are constantly investing in developing software with this technology and are keen to hire node js developers. Many of the world’s leading organizations such as PayPal, LinkedIn, Walmart, NASA, Netflix, Twitter, YouTube, Yahoo, and others currently have systems that were built using Node.
Since Node is rather new when compared to other technologies, all these major organizations had to rewrite their code in Node.js to transition to this technology. This was to achieve high performing applications and improve the overall productivity of their business.
In such a scenario, it is important for any business to consider making a transition to a node. Let’s take a deeper look into this technology and understand why it could be beneficial to hire dedicated node.js developers for your organization’s software development needs.
Why Node is being preferred?
Many application programmers use JavaScript to create software. JavaScript is loved by many programmers all around the world, because of it’s near-perfect application as a cross-platform and front-end development tool. Node.Js is essentially a runtime environment built on Google’s V8 engine and it’s a single-threaded environment that runs faster. It has a unique I/O model that makes it speedy, lightweight and efficient. This I/O model greatly increases a developer’s output.
Node has all the features essential for running a program written in JavaScript efficiently. It has a very commendable ability to use JavaScript on both the back-end and front-end parts of any application.
Due to the many advantages that node.js has over, it’s counterparts, there has been a significant rise in the demand for node.js developers. Any node.js development company would greatly benefit from hiring talented developers that specialize in Node.
Any programmer can work with node if required, however, there are several benefits of hiring dedicated node.js developers.
1. Highly Experienced: Programmers usually only have a few specific languages that they work with although it is easy to switch from one language to another. However, hiring dedicated node.js developers can make a major difference because these professionals have years of experience of dealing with various types of problems specific to Node. This can help them a lot while working on projects for high-end clients. They can come up with solutions to problems that would otherwise be very difficult for a developer who is inexperienced with Programming in Node and JavaScript.
2. Customized Applications: Developers who have years of experience with Node have a relatively much easier time customizing applications for certain projects. Web applications need to be updated and changed frequently as per the requirements. Their projects are also time-bound, so it is important that the developers working at any node js development company, deliver these projects in the expected time frame. Programmers who have spent years working with nodes usually can make the necessary customizations and updates in a short period of time, with ease.
3. Post-Development Support: After an application is developed, it sometimes needs to go through several changes to make sure that it is running smoothly. When business organizations hire a node js web development company, they can be sure of getting prompt post-development support. They can resolve any queries and even get application support from the same people who developed the application. This eliminates the possibility of any confusion or inefficiency, which might lead to product failure or waste of valuable resources.
4. Error-free applications: Hiring dedicated developers can ensure that the applications developed for your business are flawless and bug-free. Developing applications that don’t have any security flaws, or redundant code or other such elements that make the application inefficient can go a long way. Poorly developed applications usually are very uncomfortable to use and that also affects the customers. So, if the applications are developed by hiring dedicated Node.js developers, it will be faster and error-free, which will boost the overall productivity in return.
One of the best places where people can find highly qualified node.js developers is Logistic Infotech. Any business in need of a node js development company USA can hire their services and get fast robust software developed for all of their business needs. They have a team of skilled node.js developers who have a proven track record of developing flawless applications for varying needs of their customers. They provide the best in class services at very affordable rates and prioritize customer satisfaction over everything else.
- 投稿日:2020-03-28T00:10:28+09:00
Mac Node.jsのバージョン有効化する手順でvX.X.X is not installedと出力された話
目的
- Node.jsをHomebrewを使用してインストール後にバージョンを有効化する際に「vX.X.X is not installed」と出力され、それを解決した話をまとめる。
実施環境
- ハードウェア環境
項目 情報 備考 OS macOS Catalina(10.15.3) ハードウェア MacBook Air (11-inch ,2012) プロセッサ 1.7 GHz デュアルコアIntel Core i5 メモリ 8 GB 1600 MHz DDR3 グラフィックス Intel HD Graphics 4000 1536 MB エラー内容
インストール後に下記コマンドを実行してバージョンの有効化を行った。(インストールは正常に完了、X.X.Xはバージョン名)
$ nodebrew install-binary stable $ nodebrew use vX.X.X >vX.X.X is not installed解決方法
下記コマンドを実行する。
$ nodebrew --versionコマンドの出力の下部にある「# install」に記載されているコマンド
$ nodebrew install vX.X.X
を実行する。$ nodebrew install vX.X.X再度、バージョンを有効化するコマンドを実行する。
$ nodebrew use vX.X.X下記コマンドを実行して有効化したバージョンが正しい物になっている事を確認する。
$ nodebrew ls ・ ・ ・ >current: vX.X.X