Skip to content

Commit

Permalink
mod_oembed: add oembed support for spotify. (#3403)
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed May 8, 2023
1 parent 002b47a commit 4fe0bfd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
30 changes: 11 additions & 19 deletions modules/mod_oembed/support/oembed_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,17 @@ find_providers(Url, [Provider=#oembed_provider{}|Rest], Acc) ->
end.

oembed_request(RequestUrl) ->
HttpOptions = [
{autoredirect, true},
{timeout, ?HTTP_GET_TIMEOUT},
{relaxed, true}
],
case httpc:request(get, {RequestUrl, []}, HttpOptions, []) of
{ok, {{_, Code, _}, Headers, Body}} ->
case Code of
200 ->
{ok, z_convert:convert_json(mochijson2:decode(Body))};
404 ->
{error, {http, 404, <<>>}};
NoAccess when NoAccess =:= 401; NoAccess =:= 403 ->
lager:warning("OEmbed HTTP Request returned ~p for '~p' (~p ~p)", [Code, RequestUrl, Headers, Body]),
{error, {http, Code, Body}};
_Other ->
lager:info("OEmbed HTTP Request returned ~p for '~p' (~p ~p)", [Code, RequestUrl, Headers, Body]),
{error, {http, Code, Body}}
end;
case z_url_fetch:fetch(RequestUrl, []) of
{ok, {_FinalUrl, _Hs, _Size, Body}} ->
{ok, z_convert:convert_json(mochijson2:decode(Body))};
{error, {404, _FinalUrl, _Hs, _Sz, _Body}} ->
{error, {http, 404, <<>>}};
{error, {NoAccess, _FinalUrl, Hs, _Sz, Body}} when NoAccess =:= 401; NoAccess =:= 403 ->
lager:warning("OEmbed HTTP Request returned ~p for '~p' (~p ~p)", [NoAccess, RequestUrl, Hs, Body]),
{error, {http, NoAccess, Body}};
{error, {Code, _FinalUrl, Hs, _Sz, Body}} ->
lager:info("OEmbed HTTP Request returned ~p for '~p' (~p ~p)", [Code, RequestUrl, Hs, Body]),
{error, {http, Code, Body}};
{error, _} = Error ->
Error
end.
Expand Down
6 changes: 6 additions & 0 deletions modules/mod_oembed/support/oembed_providers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,11 @@ list() ->
url_re="^https?://(www\\.)?soundcloud\\.com/.+/.+",
endpoint_url="https://soundcloud.com/oembed",
title="Soundcloud"
},

#oembed_provider{
url_re="^https?://open\\.spotify\\.com/.+/.+",
endpoint_url="https://open.spotify.com/oembed",
title="Spotify"
}
].
1 change: 1 addition & 0 deletions src/support/z_sanitize.erl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ wl(<<"player.vimeo.com/", _/binary>> = Url) -> {ok, Url};
wl(<<"vimeo.com/", _/binary>> = Url) -> {ok, Url};
wl(<<"www.slideshare.net/", _/binary>> = Url) -> {ok, Url};
wl(<<"embed.spotify.com/", _/binary>> = Url) -> {ok, Url};
wl(<<"open.spotify.com/embed/", _/binary>> = Url) -> {ok, Url};
wl(<<"api.soundcloud.com/", _/binary>> = Url) -> {ok, Url};
wl(<<"w.soundcloud.com/", _/binary>> = Url) -> {ok, Url};
wl(<<"cdn.knightlab.com/", _/binary>> = Url) -> {ok, Url};
Expand Down

0 comments on commit 4fe0bfd

Please sign in to comment.