Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tiktok and twitter providers #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions fixture/vcr_cassettes/tiktok_invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": {
"follow_redirect": "true",
"ssl_options": {
"versions": {
"tlsv1.2": true
}
}
},
"request_body": "",
"url": "https://www.tiktok.com/@scout2015/video/invalid_url"
},
"response": {
"binary": false,
"message": "Something went wrong",
"status_code": 400,
"type": "ok"
}
}
]
50 changes: 50 additions & 0 deletions fixture/vcr_cassettes/tiktok_valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": {
"follow_redirect": "true",
"ssl_options": {
"versions": {
"tlsv1.2": true
}
}
},
"request_body": "",
"url": "https://www.tiktok.com/oembed?url=https%3A%2F%2Fwww.tiktok.com%2F%40scout2015%2Fvideo%2F6718335390845095173"
},
"response": {
"binary": false,
"body": "{\"version\":\"1.0\",\"type\":\"video\",\"title\":\"Scramble up ur name & I’ll try to guess it😍❤️ #foryoupage #petsoftiktok #aesthetic\",\"author_url\":\"https://www.tiktok.com/@scout2015\",\"author_name\":\"Scout, Suki & Stella\",\"width\":\"100%\",\"height\":\"100%\",\"html\":\"<blockquote class=\\\"tiktok-embed\\\" cite=\\\"https://www.tiktok.com/@scout2015/video/6718335390845095173\\\" data-video-id=\\\"6718335390845095173\\\" data-embed-from=\\\"oembed\\\" style=\\\"max-width: 605px;min-width: 325px;\\\" > <section> <a target=\\\"_blank\\\" title=\\\"@scout2015\\\" href=\\\"https://www.tiktok.com/@scout2015?refer=embed\\\">@scout2015</a> <p>Scramble up ur name & I’ll try to guess it😍❤️ <a title=\\\"foryoupage\\\" target=\\\"_blank\\\" href=\\\"https://www.tiktok.com/tag/foryoupage?refer=embed\\\">#foryoupage</a> <a title=\\\"petsoftiktok\\\" target=\\\"_blank\\\" href=\\\"https://www.tiktok.com/tag/petsoftiktok?refer=embed\\\">#petsoftiktok</a> <a title=\\\"aesthetic\\\" target=\\\"_blank\\\" href=\\\"https://www.tiktok.com/tag/aesthetic?refer=embed\\\">#aesthetic</a></p> <a target=\\\"_blank\\\" title=\\\"♬ original sound - tiff\\\" href=\\\"https://www.tiktok.com/music/original-sound-6689804660171082501?refer=embed\\\">♬ original sound - tiff</a> </section> </blockquote> <script async src=\\\"https://www.tiktok.com/embed.js\\\"></script>\",\"thumbnail_width\":720,\"thumbnail_height\":1280,\"thumbnail_url\":\"https://p16-sign-va.tiktokcdn.com/obj/tos-maliva-p-0068/2367c7d45cf54a1397abd0e72bf22eac?x-expires=1665003600&x-signature=Fpv8iDJCk%2Bt8PO%2F8%2FjE1MNLY4Lc%3D\",\"provider_url\":\"https://www.tiktok.com\",\"provider_name\":\"TikTok\"}",
"headers": {
"Server": "nginx",
"Content-Type": "application/json; charset=utf-8",
"X-Tt-Logid": "20221005154816F14388128A319830E5F4",
"X-Xss-Protection": "1; mode=block",
"X-Frame-Options": "SAMEORIGIN",
"X-Content-Type-Options": "nosniff",
"X-Download-Options": "noopen",
"x-tt-trace-host": "0140a3cbc36dbd2901a952d68819c5a5cbe21382bcb1cf4df83480642d2901718f581ee66fc93afe022de4bdec10ba453b9895718999ec854d41ac681115f19628fda5204bc135e5cd882aae960fc06ba6eb7736d92dcf9c9abb3c3f30b7634ecb",
"Access-Control-Allow-Origin": "*",
"X-Origin-Response-Time": "64,23.45.233.7",
"X-Akamai-Request-ID": "1b69d3c8.7a5f8eb",
"Expires": "Wed, 05 Oct 2022 15:48:16 GMT",
"Cache-Control": "max-age=0, no-cache, no-store",
"Pragma": "no-cache",
"Date": "Wed, 05 Oct 2022 15:48:16 GMT",
"Content-Length": "1563",
"X-Cache": "TCP_MISS from a23-45-12-173.deploy.akamaitechnologies.com (AkamaiGHost/10.9.5-44379351) (-)",
"Connection": "keep-alive",
"X-Cache-Remote": "TCP_MISS from a23-45-233-7.deploy.akamaitechnologies.com (AkamaiGHost/10.9.5-44379351) (-)",
"x-tt-trace-tag": "id=16;cdn-cache=miss;type=dyn",
"Server-Timing": "cdn-cache; desc=MISS, edge; dur=97, origin; dur=64",
"X-Parent-Response-Time": "161,23.45.12.173",
"Set-Cookie": "_abck=BD0C68FA825306B5EA8113BA8184691B~-1~YAAQrQwtF94iPp+DAQAAylLVqAj8iBkIcUKELOZskzFbnO2pr8sYgc7UJCSSj+NWtQrYR49yiwQKnodkVSFDk7QckimIYrRRaFb9hxSciev1yfK9+CG+TncxsggVkloUI7OoLKr0718kCZjX0AncP8n87LiO/UGbqPE/Bi6P2L5oJGT6bgXQec5XsokrJxHCc3DXGYtjztay1OS3TEZBBogksGKtw/ilawqe2TEiemyt0wT1hHECaOXEaZSaKwm44HGggaFUju3J+mK4igrWYW/Zij1nQkW5+GWu3C/7HCjyY22yYmMKwJMzFgYUp068IBbtzBgq5LXA2anRiizh2P/40KS1Qottmq0hFeB9Sm67E4c8tw8aubg=~-1~-1~-1; Domain=.tiktok.com; Path=/; Expires=Thu, 05 Oct 2023 15:48:16 GMT; Max-Age=31536000; Secure"
},
"status_code": 200,
"type": "ok"
}
}
]
45 changes: 45 additions & 0 deletions fixture/vcr_cassettes/twitter_status_invalid.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions fixture/vcr_cassettes/twitter_status_valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": {
"follow_redirect": "true",
"ssl_options": {
"versions": {
"tlsv1.2": true
}
}
},
"request_body": "",
"url": "https://publish.twitter.com/oembed?url=https%3A%2F%2Ftwitter.com%2Fjack%2Fstatus%2F20"
},
"response": {
"binary": false,
"body": "{\"url\":\"https:\\/\\/twitter.com\\/jack\\/status\\/20\",\"author_name\":\"jack\",\"author_url\":\"https:\\/\\/twitter.com\\/jack\",\"html\":\"\\u003Cblockquote class=\\\"twitter-tweet\\\"\\u003E\\u003Cp lang=\\\"en\\\" dir=\\\"ltr\\\"\\u003Ejust setting up my twttr\\u003C\\/p\\u003E&mdash; jack (@jack) \\u003Ca href=\\\"https:\\/\\/twitter.com\\/jack\\/status\\/20?ref_src=twsrc%5Etfw\\\"\\u003EMarch 21, 2006\\u003C\\/a\\u003E\\u003C\\/blockquote\\u003E\\n\\u003Cscript async src=\\\"https:\\/\\/platform.twitter.com\\/widgets.js\\\" charset=\\\"utf-8\\\"\\u003E\\u003C\\/script\\u003E\\n\",\"width\":550,\"height\":null,\"type\":\"rich\",\"cache_age\":\"3153600000\",\"provider_name\":\"Twitter\",\"provider_url\":\"https:\\/\\/twitter.com\",\"version\":\"1.0\"}",
"headers": {
"date": "Fri, 09 Jul 2021 02:36:28 GMT",
"server": "tsa_a",
"expires": "Sun, 15 Jun 2121 02:36:29 GMT",
"set-cookie": "personalization_id=\"v1_1VCHb8EcTSSour3kTA1gmg==\"; Max-Age=63072000; Expires=Sun, 09 Jul 2023 02:36:29 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None",
"content-type": "application/json; charset=utf-8",
"cache-control": "must-revalidate, max-age=3153600000",
"last-modified": "Fri, 09 Jul 2021 02:36:29 GMT",
"content-length": "664",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "0",
"content-disposition": "attachment; filename=json.json",
"x-content-type-options": "nosniff",
"strict-transport-security": "max-age=631138519",
"x-connection-hash": "9a68eee7d5f5b51f5057f60e2c65b1d36fcec3d6f702658b8fa7d58c29000dc9"
},
"status_code": 200,
"type": "ok"
}
}
]
4 changes: 3 additions & 1 deletion lib/oembed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ defmodule OEmbed do
OEmbed.InstagramProvider,
OEmbed.PinterestProvider,
OEmbed.VimeoProvider,
OEmbed.YoutubeProvider
OEmbed.YoutubeProvider,
OEmbed.TwitterProvider,
OEmbed.TikTokProvider
]

@fallback_providers [OEmbed.DiscoverableProvider]
Expand Down
25 changes: 25 additions & 0 deletions lib/oembed/providers/tiktok_provider.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule OEmbed.TikTokProvider do
@moduledoc """
oEmbed provider for Tiktok URLs.
"""
use OEmbed.Provider

@oembed_endpoint "https://www.tiktok.com/oembed?url="

@doc """
Check if this provider supports given URL.
"""
def provides?(url) do
Regex.match?(
~r/(^|[^'"])(https?:\/\/(www\.)?tiktok\.com\/\@?(\w+)\/video?\/(\d+))/i,
url
)
end

@doc """
Get oEmbed result for given URL.
"""
def get(url) do
get_oembed(@oembed_endpoint <> URI.encode(url, &URI.char_unreserved?/1))
end
end
25 changes: 25 additions & 0 deletions lib/oembed/providers/twitter_provider.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule OEmbed.TwitterProvider do
@moduledoc """
oEmbed provider for Twitter URLs.
"""
use OEmbed.Provider

@oembed_endpoint "https://publish.twitter.com/oembed?url="

@doc """
Check if this provider supports given URL.
"""
def provides?(url) do
Regex.match?(
~r/(^|[^'"])(https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+))/i,
url
)
end

@doc """
Get oEmbed result for given URL.
"""
def get(url) do
get_oembed(@oembed_endpoint <> URI.encode(url, &URI.char_unreserved?/1))
end
end
28 changes: 28 additions & 0 deletions test/oembed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ defmodule OEmbedTest do
end
end

test "gets rich oembed for valid twitter status url" do
use_cassette "twitter_status_valid" do
{:ok, %Rich{} = oembed} = OEmbed.for("https://twitter.com/jack/status/20")
assert oembed.html =~ "just setting up my twttr"
end
end

test "gets error response for invalid twitter url" do
use_cassette "twitter_status_invalid" do
{:error, _} = OEmbed.for("https://www.twitter.com/jack/status/invalid_url/")
end
end

test "gets rich oembed for valid soundcloud url" do
use_cassette "soundcloud_valid" do
{:ok, %Rich{} = oembed} = OEmbed.for("https://soundcloud.com/forss/flickermood")
Expand Down Expand Up @@ -129,4 +142,19 @@ defmodule OEmbedTest do
assert oembed.html =~ "vimeo"
end
end

test "gets video oembed for valid tiktok video url" do
use_cassette "tiktok_valid" do
{:ok, %Video{} = oembed} =
OEmbed.for("https://www.tiktok.com/@scout2015/video/6718335390845095173")

assert oembed.html =~ "Scramble up ur name"
end
end

test "gets error response for invalid tiktok url" do
use_cassette "tiktok_invalid" do
{:error, _} = OEmbed.for("https://www.tiktok.com/@scout2015/video/invalid_url")
end
end
end