-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjazzhuset_start.erl
79 lines (64 loc) · 2.14 KB
/
jazzhuset_start.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
%% Author: Aliaksandr Karasiou
%% Email: [email protected]
%% Personal number: 0736796331
%% www.jazzhuset.se (part 1) parser
-module(jazzhuset_start).
-export([get_info/0]).
%%% Internal functions, Tomasz
-export([makeref/0, loop/0]).
%% starts the module, runs inets libraries for http requests, creates a
%% template for an Event, get the source code of a page (Body) and passes to
%% another function.
get_info() ->
inets:start(),
{ok, {{_Version, 200, _ReasonPhrase}, _Headers, Body}} =
httpc:request("http://www.jazzhuset.se/"),
Name = [],
Description = [],
Time = [],
Date = [],
Picture = [],
Event = [Name, Description, Time, Date, Picture],
set_start(Body, Event),
loop().
%% Creates the unique reference for the parser, only used by the server, Tomasz
makeref() ->
make_ref().
%% Function searches for a place where it will start to look for links of
%% events.
set_start([$", $k, $o, $m, $m, $a, $n, $d, $e, $e, $v, $e, $n, $t, $"|T],
Event) ->
get_link(T, Event);
set_start([_H|T], Event) -> set_start(T, Event);
set_start([], _Event) ->
srv ! {done, self()}.
%% Functions finds a place in source where link is starting by a particular
%% tag.
get_link([$h, $r, $e, $f, $=, $"|T], Event) ->
take_link(T,[], Event);
get_link([_H|T], Event) -> get_link(T, Event);
get_link([], _Event) -> ok.
%% Function extracts a link of an event, get a source code of this page (Body1)
%% and passes it to the module which parses it.
take_link([$"|T], List, Event) ->
NewLink = lists:reverse(List),
{ok, {{_Version1, 200, _ReasonPhrase1}, _Headers1, Body1}} =
httpc:request(NewLink),
jazzhuset:set_start(Body1, Event),
set_start(T, Event);
take_link([H|T], List, Event) ->
take_link(T, [H|List], Event).
%% @author Tomasz Rakalski
%% The function for the process part, waits for messages from the server and
%% runs the process again after 6 hours (after timeout).
loop() ->
receive
{ok, _Pid} ->
io:format("confirmation received~n"),
loop();
stop ->
ok
after 2160000 ->
get_info(),
loop()
end.