paint-brush
Bluesky's API Bilaashka ah waxay iga caawisaa inaan hagaajiyo qoraaladayda ugu sarreeya si aan waligeed horay loo aragby@noahm
Taariikh cusub

Bluesky's API Bilaashka ah waxay iga caawisaa inaan hagaajiyo qoraaladayda ugu sarreeya si aan waligeed horay loo arag

by Noah5m2024/12/17
Read on Terminal Reader

Aad u dheer; In la akhriyo

Baro sida barnaamij ahaan loogu soo saaro oo u dhexgeliyo boostadaada Bluesky ee aad jeceshahay adiga oo isticmaalaya xaqiijin, wicitaanada API, iyo oEmbed dhamaadka.
featured image - Bluesky's API Bilaashka ah waxay iga caawisaa inaan hagaajiyo qoraaladayda ugu sarreeya si aan waligeed horay loo arag
Noah HackerNoon profile picture

Baro sida barnaamij ahaan loogu soo saaro oo u dhexgeliyo qoraaladaada Bluesky ee aad jeceshahay adiga oo isticmaalaya xaqiijin, wicitaanada API, iyo oEmbed dhamaadka.


Waxaan dhawaan u wareegay Bluesky . Waxaan horeyba u xaqiijin karaa inay jirto bulsho tignoolajiyadeed firfircoon oo halkaas ka kooban waxyaabo xiiso leh, faa'iido leh, iyo dhiirigelin leh. Waxaan ahay isticmaale cusub oo faraxsan! Natiijo ahaan, waxaan doonayay inaan ku dhejiyo fariimahayga ugu sarreeya ee Bluesky-ga ee aan jeclahay qoraalladayda warsidaha billaha ah ee "Dev roundup". Ujeedadaydu waa in aan bixiyo liis la soo koobay oo ah qoraalada Bluesky kuwaas oo si gaar ah loogu talagalay Soosaarayaasha Software-ka.


Nasiib wanaag, Bluesky's API gabi ahaanba waa xor in la isticmaalo, taas oo u oggolaanaysa gelitaanka barnaamijka dhammaan waxyaabaha ku jira. Casharradani waxay ku socon doontaa habka aad dib ugu soo celin lahayd oo aad ugu dhejin lahayd qoraallada Bluesky ee la jecel yahay iyaga oo isticmaalaya API-gooda, oo ku habboon blogyada gaarka ah, faylalka, ama mashaariicda isku-darka nuxurka.

Fahamka socodka shaqadayda Bluesky API

Waxaan dhisay qoraal ii ogolaanaya inaan si toos ah ugu dhejiyo qoraaladeyda Bluesky boostada blog-ka calaamadaynta. Waxaan u maleynayaa in mid ama dhammaan tillaabooyinka lagu isticmaalo qoraalkan ay qiimo u leeyihiin kiisas badan oo la isticmaalo.


Si aan u soo koobo socodka shaqadayda ee ku dhejinta qoraalada la jecel yahay, waxaanu raacnaa talaabooyinkan muhiimka ah:

  1. Samee fadhi la xaqiijiyay
  2. URI-yada la jeclaaday u soo celi "jilaa"
  3. Isticmaal URI-yadan si aad u soo qaadato oEmbed HTML
  4. Nadiifi oo qaabee koodka guntanka ah


Hirgelinta Dhamaystiran

Aynu kala saarno hawl kasta iyo ujeeddadeeda:

1. Abuuritaanka Kalfadhi Buluuga ah

 export const createSession = async (): Promise<string | null> => { try { const response = await fetch( "https://bsky.social/xrpc/com.atproto.server.createSession", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ identifier: "your-handle", password: "your-password", }), } ); const responseJson = await response.json(); return responseJson.accessJwt; } catch (e) { console.error("Failed to create session: ", e); return null; } };


Aragtida Muhiimka ah:

  • Shaqadani waxay xaqiijinaysaa akoonkaaga Bluesky.
    • Fiiro gaar ah: Tusaalahani wuxuu xadhkaha ku dhejiyaa aqoonsiga, laakiin tani waa in laga fogaadaa kiisaska isticmaalka wax soo saarka.
  • Waxay soo celisaa accessJwt JWT (JSON Web Token) wicida xiga ee API
  • Wuxuu isticmaalaa createSession barta dhamaadka kulanka Bluesky's ATP (Borotokoolka Wareejinta La Xaqiijiyay
  • Khaladaadka maaraynta waxay xaqiijisaa guuldarada quruxda badan haddii xaqiijintu fashilanto

2. Soo Celinta URI-yada Boostada La Jecelyahay

 export const getBlueskyLikeUris = async (actor: string, limit: number = 40) => { const token = await createSession(); if (!token) { console.error("Failed to get token"); return; } const response = await fetch( "https://bsky.social/xrpc/app.bsky.feed.getActorLikes?actor=${actor}&limit=${limit}", { method: "GET", headers: { Authorization: `Bearer ${token}`, }, } ); const responseJson = await response.json(); const uris = responseJson.feed.map((entry: any) => entry.post.uri); return uris; };


Aragtida Muhiimka ah:

  • Waxay u baahan tahay calaamad fadhi la xaqiijiyay
  • Wuxuu adeegsadaa barta ugu dambeysa getActorLikes si uu u soo saaro qoraallada la jecel yahay
  • Muhiim -- barta dhamaadka barta waa inay noqotaa https://bsky.social, maadaama kani yahay codsi la xaqiijiyay.
  • Soo saarta URI-yo gaar ah boosto kasta oo la jeclaado
  • Waxay ku xaddidan tahay 40 boosto (la habeyn karo)

3. URI-yada oo loo beddelo HTML-ga la dhex-dhigi karo

 export const getBlueskyPostEmbedMarkup = async (uri: string) => { try { const response = await fetch(`https://embed.bsky.app/oembed?url=${uri}`); const responseJson = await response.json(); const formattedHTML = prettier.format(responseJson.html, { parser: "html", plugins: [require("prettier/parser-html")], htmlWhitespaceSensitivity: "ignore", printWidth: 1000, }); return formattedHTML.replace(/<script[\s\S]*?<\/script>/g, ""); } catch (e) { console.error("Failed to get Bluesky post embed markup"); return null; } };


Aragtida Muhiimka ah:

  • Wuxuu adeegsadaa barta ugu dambeysa ee Bluesky's oEmbed oo leh URI-yada boostada si ay u galaan HTML-ga la dhejisan karo ee boostada
  • Ikhtiyaar: Wuxuu ka faa'iidaystaa prettier inuu u habeeyo HTML-ka si joogto ah
  • Ikhtiyaar: Ka saara <script> sumadaha amniga iyo ku-xidhka nadiifka ah
    • Sababta tani waa inaan ku dhejiyay hal qoraal oo bluesky ah qoraal kasta oo ka kooban nuxurka Bluesky.
  • Maareynta khaladka dabacsan

Isku soo wada duuboo: Tusaale Dhamaystiran

 async function embedLikedPosts() { try { // Get liked post URIs const likedPostUris = await getBlueskyLikeUris(); if (!likedPostUris) { console.error("No liked posts found"); return; } // Convert URIs to embed HTML const embedPromises = likedPostUris.map(getBlueskyPostEmbedMarkup); const embedHtmlArray = await Promise.all(embedPromises); // Filter out any failed embeds const validEmbeds = embedHtmlArray.filter(embed => embed !== null); // Return the markup for all liked posts return ` ## Some Fave Posts 🦋 ${validEmbeds.join(`\n\n`)} ` } catch (error) { console.error("Error embedding Bluesky posts:", error); } }

Kobcinta suurtagalka ah

Xalkani wuu ii shaqeeyaa sababtoo ah waxa kaliya ee aan u baahanahay waa boostada blog-ka ee bilaha ah.


Horumarrada qaarkood waxaa ka mid noqon kara:

  • Ku dar taageerada bogga si aad u soo qaadato in ka badan 40 qoraalo la jeclaaday
  • Hirgeli kaydinta si loo yareeyo wicitaanada API ee aan loo baahnayn
  • Samee hab lagu maamulo khaladka oo aad u adag
  • Abuuritaanka hab lagu cusboonaysiiyo accessJwt token haddii loo isticmaalo geeddi-socodyada dheer ee socda
  • Ku kala soocida fariimaha la jeclaaday ee caanka ah (jeceyl)

Talooyin xallinta dhibaatada

  • Xaqiiji in aqoonsigaaga Bluesky ay sax yihiin
  • Hubi in calaamadda sita si sax ah loogu dejiyay codsiyadaada la xaqiijiyay.
  • Hubi in barta dhamaadka barta aad isticmaalayso ay dhamaantood sax yihiin.

Gabagabo

Ku dhejinta qoraallada Bluesky waxay ku siinaysaa hab firfircoon oo lagu muujiyo isdhexgalkaaga warbaahinta bulshada. Markaad fahamto socodka shaqada API iyo hirgelinta khaladka adag ee maaraynta, waxaad abuuri kartaa ka-qaybgal, la gaaryeelay, iyo isku-dhafka nuxurka.

Talaabooyinka Xiga

  1. Ku tijaabi koodka
  2. Habbee qaabaynta guntanka
  3. Sahami dhibcood dheeri ah oo Bluesky API ah


Ku raaxayso oo tinkering faraxsan! 🚀