Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F77284
imgur.ex
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
imgur.ex
View Options
defmodule
Nola.Plugins.Link.Imgur
do
@behaviour
Nola.Plugins.Link
@moduledoc
"""
#
Imgur link preview
No options.
Needs to have a Imgur API key configured:
```
config :nola, :imgur,
client_id: "xxxxxxxx",
client_secret: "xxxxxxxxxxxxxxxxxxxx"
```
"""
@impl
true
def
match
(
uri
=
%
URI
{
host
:
"imgur.io"
},
arg
)
do
match
(%
URI
{
uri
|
host
:
"imgur.com"
},
arg
)
end
def
match
(
uri
=
%
URI
{
host
:
"i.imgur.io"
},
arg
)
do
match
(%
URI
{
uri
|
host
:
"i.imgur.com"
},
arg
)
end
def
match
(
uri
=
%
URI
{
host
:
"imgur.com"
,
path
:
"/a/"
<>
album_id
},
_
)
do
{
true
,
%{
album_id
:
album_id
}}
end
def
match
(
uri
=
%
URI
{
host
:
"imgur.com"
,
path
:
"/gallery/"
<>
album_id
},
_
)
do
{
true
,
%{
album_id
:
album_id
}}
end
def
match
(
uri
=
%
URI
{
host
:
"i.imgur.com"
,
path
:
"/"
<>
image
},
_
)
do
[
hash
,
_
]
=
String
.
split
(
image
,
"."
,
parts
:
2
)
{
true
,
%{
image_id
:
hash
}}
end
def
match
(
_
,
_
),
do
:
false
@impl
true
def
post_match
(
_
,
_
,
_
,
_
),
do
:
false
def
expand
(
_uri
,
%{
album_id
:
album_id
},
opts
)
do
expand_imgur_album
(
album_id
,
opts
)
end
def
expand
(
_uri
,
%{
image_id
:
image_id
},
opts
)
do
expand_imgur_image
(
image_id
,
opts
)
end
def
expand_imgur_image
(
image_id
,
opts
)
do
client_id
=
Keyword
.
get
(
Application
.
get_env
(
:nola
,
:imgur
,
[]),
:client_id
,
"42"
)
headers
=
[{
"Authorization"
,
"Client-ID
#{
client_id
}
"
}]
options
=
[]
case
HTTPoison
.
get
(
"https://api.imgur.com/3/image/
#{
image_id
}
"
,
headers
,
options
)
do
{
:ok
,
%
HTTPoison.Response
{
status_code
:
200
,
body
:
body
}}
->
{
:ok
,
json
}
=
Jason
.
decode
(
body
)
data
=
json
[
"data"
]
title
=
String
.
slice
(
data
[
"title"
]
||
data
[
"description"
],
0
,
180
)
nsfw
=
if
data
[
"nsfw"
],
do
:
"(NSFW) - "
,
else
:
" "
height
=
Map
.
get
(
data
,
"height"
)
width
=
Map
.
get
(
data
,
"width"
)
size
=
Map
.
get
(
data
,
"size"
)
{
:ok
,
"image,
#{
width
}
x
#{
height
}
,
#{
size
}
bytes
#{
nsfw
}#{
title
}
"
}
other
->
:error
end
end
def
expand_imgur_album
(
album_id
,
opts
)
do
client_id
=
Keyword
.
get
(
Application
.
get_env
(
:nola
,
:imgur
,
[]),
:client_id
,
"42"
)
headers
=
[{
"Authorization"
,
"Client-ID
#{
client_id
}
"
}]
options
=
[]
case
HTTPoison
.
get
(
"https://api.imgur.com/3/album/
#{
album_id
}
"
,
headers
,
options
)
do
{
:ok
,
%
HTTPoison.Response
{
status_code
:
200
,
body
:
body
}}
->
{
:ok
,
json
}
=
Jason
.
decode
(
body
)
data
=
json
[
"data"
]
title
=
data
[
"title"
]
nsfw
=
data
[
"nsfw"
]
nsfw
=
if
nsfw
,
do
:
"(NSFW) - "
,
else
:
""
if
data
[
"images_count"
]
==
1
do
[
image
]
=
data
[
"images"
]
title
=
if
title
||
data
[
"title"
]
do
title
=
[
title
,
data
[
"title"
]]
|>
Enum
.
filter
(
fn
(
x
)
->
x
end
)
|>
Enum
.
uniq
()
|>
Enum
.
join
(
" — "
)
"
#{
title
}
— "
else
""
end
{
:ok
,
"
#{
nsfw
}#{
title
}#{
image
[
"link"
]
}
"
}
else
title
=
if
title
,
do
:
title
,
else
:
"Untitled album"
{
:ok
,
"
#{
nsfw
}#{
title
}
-
#{
data
[
"images_count"
]
}
images"
}
end
other
->
:error
end
end
end
File Metadata
Details
Attached
Mime Type
text/x-ruby
Expires
Sun, Jul 6, 9:46 AM (15 h, 57 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
49819
Default Alt Text
imgur.ex (2 KB)
Attached To
rNOLA Nola
Event Timeline
Log In to Comment