Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F77294
youtube.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
youtube.ex
View Options
defmodule
Nola.Plugins.Link.YouTube
do
@behaviour
Nola.Plugins.Link
@moduledoc
"""
#
YouTube link preview
needs an API key:
```
config :nola, :youtube,
api_key: "xxxxxxxxxxxxx"
```
options:
* `invidious`: Add a link to invidious.
"""
@impl
true
def
match
(
uri
=
%
URI
{
host
:
yt
,
path
:
"/watch"
,
query
:
"v="
<>
video_id
},
_opts
)
when
yt
in
[
"youtube.com"
,
"www.youtube.com"
]
do
{
true
,
%{
video_id
:
video_id
}}
end
def
match
(%
URI
{
host
:
"youtu.be"
,
path
:
"/"
<>
video_id
},
_opts
)
do
{
true
,
%{
video_id
:
video_id
}}
end
def
match
(
_
,
_
),
do
:
false
@impl
true
def
post_match
(
_
,
_
,
_
,
_
),
do
:
false
@impl
true
def
expand
(
uri
,
%{
video_id
:
video_id
},
opts
)
do
key
=
Application
.
get_env
(
:nola
,
:youtube
)[
:api_key
]
params
=
%{
"part"
=>
"snippet,contentDetails,statistics"
,
"id"
=>
video_id
,
"key"
=>
key
}
headers
=
[]
options
=
[
params
:
params
]
case
HTTPoison
.
get
(
"https://www.googleapis.com/youtube/v3/videos"
,
[],
options
)
do
{
:ok
,
%
HTTPoison.Response
{
status_code
:
200
,
body
:
body
}}
->
case
Jason
.
decode
(
body
)
do
{
:ok
,
json
}
->
item
=
List
.
first
(
json
[
"items"
])
if
item
do
snippet
=
item
[
"snippet"
]
duration
=
item
[
"contentDetails"
][
"duration"
]
|>
String
.
replace
(
"PT"
,
""
)
|>
String
.
downcase
date
=
snippet
[
"publishedAt"
]
|>
DateTime
.
from_iso8601
()
|>
elem
(
1
)
|>
Timex
.
format
(
"{relative}"
,
:relative
)
|>
elem
(
1
)
line
=
if
host
=
Keyword
.
get
(
opts
,
:invidious
)
do
[
"-> https://
#{
host
}
/watch?v=
#{
video_id
}
"
]
else
[]
end
{
:ok
,
line
++
[
"
#{
snippet
[
"title"
]
}
"
,
"—
#{
duration
}
— uploaded by
#{
snippet
[
"channelTitle"
]
}
—
#{
date
}
"
<>
" —
#{
item
[
"statistics"
][
"viewCount"
]
}
views,
#{
item
[
"statistics"
][
"likeCount"
]
}
likes"
]}
else
:error
end
_
->
:error
end
end
end
end
File Metadata
Details
Attached
Mime Type
text/x-ruby
Expires
Sun, Jul 6, 12:44 PM (18 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
49827
Default Alt Text
youtube.ex (2 KB)
Attached To
rNOLA Nola
Event Timeline
Log In to Comment