Skip to content

Commit

Permalink
Recording timestamp from github api to calculate how often live data …
Browse files Browse the repository at this point in the history
…needs to be polled
  • Loading branch information
alex committed May 9, 2012
1 parent 8e91f99 commit 383d1d5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions github.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ start() ->
crypto:start(),
inets:start(),
ssl:start(),
sqlite3:open(ct, [{file, "/home/alex/Downloads/erlanghub/erlang-sqlite3/hello.sqlite"}]),
loop("1").

loop(PageNumber) ->
%timer:sleep(1000),
timer:sleep(1000),
InitialUrl = "https://api.github.com/events?page=",
NextUrl = string:concat(InitialUrl, PageNumber),
{ok, {Status1, Headers1, Body1}} = httpc:request(get, {NextUrl, []}, [], []),
{ok, {Status1, Headers1, Body1}} = httpc:request(NextUrl),
% process the content of JSON data
ok = file:write_file("output.txt", Body1, [write, append]),
Obj = mochijson2:decode(Body1),
Events = proplists:get_all_values(struct, Obj),
lists:foreach(fun(Event) -> insert_event(Event) end, Events),
% ok = file:write_file("output.txt", Body1, [write, append]),
Link = proplists:get_value("link", Headers1),
case string:str(Link, "next") of
0 -> io:format("Start from page 1~n"),
Expand All @@ -27,3 +31,8 @@ loop(PageNumber) ->
loop(NextPageNumber)
end.

insert_event(Event) ->
io:format("~p~n", [proplists:get_value(<<"id">>, Event)]),
io:format("~p~n", [proplists:get_value(<<"created_at">>, Event)]),
sqlite3:sql_exec(ct, "INSERT INTO github (id, github_time, pull_time) VALUES (?, ?, ?)",
[proplists:get_value(<<"id">>, Event), proplists:get_value(<<"created_at">>, Event), httpd_util:rfc1123_date(erlang:localtime())]).

0 comments on commit 383d1d5

Please sign in to comment.