-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpacker_rules.erl
143 lines (134 loc) · 4.79 KB
/
unpacker_rules.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%%%-------------------------------------------------------------------
%%% @author Stefan Hagdahl
%%% @copyright (C) 2017, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 01. Mar 2017 13:55
%%%-------------------------------------------------------------------
-module(unpacker_rules).
-author("Stefan Hagdahl").
-include("unpacker.hrl").
%% API
-export([rules/1,
match/2]).
rules(Config) ->
case find_rules(Config) of
{ok, Rules} ->
lager:info("Rules:~p~n", [Rules]),
case verify_rules(Rules) of
{error, Reason} ->
lager:error("Failed to verify Rules:~p~nConfig"
":~p~nReason:~p~n",
[Rules, Config, Reason]),
erlang:halt(?EVerifyRules);
MapRules ->
MapRules
end;
{error, Reason} ->
lager:error("Failed to parse rules in config"
":~p~nError:~p~n", [Config, Reason]),
erlang:halt(?EParseRules)
end.
find_rules([]) ->
{error, "No Rules found"};
find_rules([{"rules", Rules}|_Rest]) ->
{ok, Rules};
find_rules([_|Rest]) ->
find_rules(Rest).
verify_rules([]) ->
[];
verify_rules([[{Rule, RuleOptions}]|Rest]) ->
RuleOptionsMap = maps:from_list(RuleOptions),
case verify_rule_options(RuleOptionsMap) of
false ->
{error, "Missing mandatory fields in rule: " ++ Rule};
true ->
[#{rule => Rule, rule_opts => RuleOptionsMap}] ++
verify_rules(Rest)
end.
verify_rule_options(#{"type" := _, "extraction_location" := _}) ->
true;
verify_rule_options(_RuleOptions) ->
false.
match(_, []) ->
{error, "No rule matching Guessit information"};
match(#{?GuessitType := ?GuessitMovie}=Guessit,
[#{rule := Rule, rule_opts :=
#{"type" := ?RuleMovie,
"extraction_location" := ExtractionLocation}=RuleOptions}|Rest]) ->
lager:info("Processing Rule:~p~nRule Options:~p~n", [Rule, RuleOptions]),
case match_rule_options(maps:to_list(RuleOptions), Guessit) of
true ->
{match, Rule, ExtractionLocation};
false ->
match(Guessit, Rest)
end;
match(#{?GuessitType := ?GuessitTv}=Guessit,
[#{rule := Rule, rule_opts :=
#{"type" := ?RuleTv,
"extraction_location" := ExtractionLocation}=RuleOptions}|Rest]) ->
lager:info("Processing Rule:~p~nRule Options:~p~n", [Rule, RuleOptions]),
case match_rule_options(maps:to_list(RuleOptions), Guessit) of
true ->
{match, Rule, ExtractionLocation};
false ->
match(Guessit, Rest)
end;
match(Guessit, [_Rule|Rest]) ->
match(Guessit, Rest).
match_rule_options([], _Guessit) ->
true;
match_rule_options([{"screen_size", RuleScreenSize}|Rest],
#{?GuessitScreenSize := GuessitScreenSize}=Guessit) ->
{Range, RuleScreenSizeInt} = split_screen_size(RuleScreenSize),
{_, GuessitScreenSizeInt} = split_screen_size(bitstring_to_list(GuessitScreenSize)),
lager:info("Range:~p GuessitScreenSize:~p RuleScreenSize:~p",
[Range, GuessitScreenSizeInt, RuleScreenSizeInt]),
case Range of
"-" ->
if
GuessitScreenSizeInt =< RuleScreenSizeInt ->
match_rule_options(Rest, Guessit);
true ->
false
end;
"+" ->
if
GuessitScreenSizeInt >= RuleScreenSizeInt ->
match_rule_options(Rest, Guessit);
true ->
false
end;
no_range ->
if
GuessitScreenSizeInt == RuleScreenSizeInt ->
match_rule_options(Rest, Guessit);
true ->
false
end
end;
match_rule_options([{"regex", Regex}|Rest], #{?GuessitName := Name}=Guessit) ->
NameString = bitstring_to_list(Name),
case re:run(NameString, Regex) of
{match, _} ->
match_rule_options(Rest, Guessit);
nomatch ->
false
end;
match_rule_options([_RuleOption|Rest], Guessit) ->
match_rule_options(Rest, Guessit).
split_screen_size("4K") ->
split_screen_size("2160p");
split_screen_size(ScreenSize) ->
case string:substr(ScreenSize, string:len(ScreenSize), 1) of
"-" ->
{"-", list_to_integer(string:substr(ScreenSize, 1,
string:len(ScreenSize)-2))};
"+" ->
{"+", list_to_integer(string:substr(ScreenSize, 1,
string:len(ScreenSize)-2))};
"p" ->
{no_range, list_to_integer(string:substr(ScreenSize, 1,
string:len(ScreenSize)-1))}
end.