Last active
August 29, 2015 17:27
-
-
Save shibacow/91b6e9a93621af8472f0 to your computer and use it in GitHub Desktop.
http://qiita.com/HirofumiTamori/items/7936ed9c6f73f4c9b475 を真似て書いた。ubuntu 14.04 Elixir (1.0.5)で動作確認。 iex -S mixで動く。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Vercheckex.Mixfile do | |
use Mix.Project | |
def project do | |
[app: :vercheckex, | |
version: "0.0.1", | |
elixir: "~> 1.0", | |
build_embedded: Mix.env == :prod, | |
start_permanent: Mix.env == :prod, | |
deps: deps] | |
end | |
# Configuration for the OTP application | |
# | |
# Type `mix help compile.app` for more information | |
def application do | |
[applications: [:logger,:httpoison,:floki,:timex]] | |
end | |
# Dependencies can be Hex packages: | |
# | |
# {:mydep, "~> 0.3.0"} | |
# | |
# Or git/path repositories: | |
# | |
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} | |
# | |
# Type `mix help deps` for more examples and options | |
defp deps do | |
[{:httpoison, "~> 0.7.2"}, {:floki, "~> 0.3"},{:timex, "~> 0.19.2"}] | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule VercheckEx do | |
# requireで使用するライブラリを指定 | |
require HTTPoison | |
require Floki | |
require Timex | |
use Timex | |
def fetch_content() do | |
IO.puts("fetch_content") | |
receive do | |
{caller,url,type,i} -> | |
IO.puts("i=#{i}") | |
ret = HTTPoison.get!( url ) # urlで指定されるページのデータを取得 | |
%HTTPoison.Response{status_code: 200, body: body} = ret | |
# HTML bodyを取得する | |
# HTMLパーザー Flokiで処理 | |
# 名前、リリース日時を取得 | |
{_,_,n} = Floki.find(body, "[itemprop=title]") |> List.first | |
{_, date} = Floki.find(body, "time") |> Floki.attribute("datetime") | |
|> List.first | |
|> Timex.DateFormat.parse("{ISOz}") | |
if(type == :type1) do # バージョン番号を取得 | |
{_,_,x} = Floki.find(body, ".tag-name span") |> List.first | |
else | |
{_,_,x} = Floki.find(body, ".css-truncate-target span") |> List.first | |
end | |
#UTC時刻をJSTに変更 | |
date |> DateConvert.to_erlang_datetime | |
|> Timex.Date.from "Asia/Tokyo" | |
send caller ,{:ok,{hd(n),hd(x),date,i}} # 戻り値はタプル | |
end | |
end | |
def put_a_formatted_line(val) do # 1行出力 | |
{title, ver, date,_} = val | |
l = title | |
if String.length(title) < 8 do | |
l = l <> "\t" | |
end | |
l = l <> "\t" <> ver | |
if String.length(ver) < 8 do | |
l = l <> "\t" | |
end | |
l = l <> "\t" <> Timex.DateFormat.format!(date, "%Y.%m.%d", :strftime) | |
now = Timex.Date.now("JST") | |
diff = Timex.Date.diff( date, now, :days) # リリースから今日までの日数 | |
if diff < 14 do # 14日以内なら警告する。以前の仕事が2週間スプリントだった名残り。 | |
l = l <> "\t<<<<< updated at " <> Integer.to_string(diff) <> " day(s) ago." | |
end | |
IO.puts(l) | |
end | |
def receiver(result_list,n) do | |
IO.puts("receiver") | |
if(length(result_list) < n) do | |
receive do | |
{:ok,res} -> | |
IO.puts("n=#{length(result_list)}") | |
receiver(result_list++[res],n) | |
end | |
else | |
Enum.sort(result_list,fn(a,b)-> | |
{_,_,_,i1} = a | |
{_,_,_,i2} = b | |
i1 < i2 end | |
)|>Enum.each( | |
fn(x) -> put_a_formatted_line x end | |
) | |
end | |
end | |
end | |
urls = [ | |
{"https://github.com/jquery/jquery/releases", :type1}, | |
{"https://github.com/angular/angular/releases", :type1}, | |
{"https://github.com/facebook/react/releases", :type2}, | |
{"https://github.com/PuerkitoBio/goquery/releases", :type1}, | |
{"https://github.com/revel/revel/releases", :type2}, | |
{"https://github.com/lhorie/mithril.js/releases", :type1}, | |
{"https://github.com/riot/riot/releases", :type1}, | |
{"https://github.com/atom/atom/releases", :type2}, | |
{"https://github.com/Microsoft/TypeScript/releases", :type2}, | |
{"https://github.com/docker/docker/releases", :type1}, | |
{"https://github.com/JuliaLang/julia/releases", :type2}, | |
{"https://github.com/nim-lang/Nim/releases", :type1}, | |
{"https://github.com/elixir-lang/elixir/releases", :type2}, | |
{"https://github.com/philss/floki/releases", :type1}, | |
{"https://github.com/takscape/elixir-array/releases", :type2}, | |
] | |
HTTPoison.start | |
IO.puts("Start") | |
fetchers = for _ <- 0..length(urls) , do: spawn_link fn -> VercheckEx.fetch_content() end | |
IO.puts("fetchers") | |
Enum.each( Enum.with_index(urls),fn(x)-> | |
{{u,t},i} = x | |
IO.puts("pre send enum i = #{i}") | |
send Enum.at(fetchers,i),{self,u,t,i} | |
end) | |
result_list = [] | |
VercheckEx.receiver(result_list,length(urls)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment