Skip to content

Commit

Permalink
Fix errors in bsky scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Nov 15, 2024
1 parent febcdfa commit 282b8b7
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions lib/philomena_proxy/scrapers/bluesky.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,25 @@ defmodule PhilomenaProxy.Scrapers.Bluesky do
def scrape(_uri, url) do
[handle, id] = Regex.run(@url_regex, url, capture: :all_but_first)

did =
if String.starts_with?(handle, "did:") do
handle
else
api_url_resolve_handle =
"https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle?handle=#{handle}"

PhilomenaProxy.Http.get(api_url_resolve_handle) |> json!() |> Map.fetch!(:did)
end
did = fetch_did(handle)

api_url_get_posts =
"https://public.api.bsky.app/xrpc/app.bsky.feed.getPosts?uris=at://#{did}/app.bsky.feed.post/#{id}"

post_json = PhilomenaProxy.Http.get(api_url_get_posts) |> json!() |> Map.fetch!(:posts) |> hd
post_json =
api_url_get_posts
|> PhilomenaProxy.Http.get()
|> json!()
|> Map.fetch!("posts")
|> hd()

%{
source_url: url,
author_name: post_json["author"]["handle"],
author_name: domain_first_component(post_json["author"]["handle"]),
description: post_json["record"]["text"],
images:
post_json["embed"]["images"]
|> Enum.map(
Enum.map(
post_json["embed"]["images"],
&%{
url: String.replace(&1["fullsize"], @fullsize_image_regex, @blob_image_url_pattern),
camo_url: PhilomenaProxy.Camo.image_url(&1["thumb"])
Expand All @@ -49,5 +46,31 @@ defmodule PhilomenaProxy.Scrapers.Bluesky do
}
end

defp fetch_did(handle) do
case handle do
<<"did:", _rest::binary>> ->
handle

_ ->
api_url_resolve_handle =
"https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle?handle=#{handle}"

api_url_resolve_handle
|> PhilomenaProxy.Http.get()
|> json!()
|> Map.fetch!("did")
end
end

defp domain_first_component(domain) do
case String.split(domain, ".") do
[name | _] ->
name

_ ->
domain
end
end

defp json!({:ok, %{body: body, status: 200}}), do: Jason.decode!(body)
end

0 comments on commit 282b8b7

Please sign in to comment.