paint-brush
API ya ofele ya Bluesky Esalisaka Ngai Na Curer Ba Posts Na Ngai Ya Likolo Neti Jamais Avantpene@noahm
Lisolo ya sika

API ya ofele ya Bluesky Esalisaka Ngai Na Curer Ba Posts Na Ngai Ya Likolo Neti Jamais Avant

pene Noah5m2024/12/17
Read on Terminal Reader

Molai mingi; Mpo na kotánga

Yekola ndenge ya kozua na programme mpe ko intégrer ba posts na yo ya Bluesky oyo olingaka na nzela ya authentification, ba appels ya API, na ba points d’arrêt ya oEmbed.
featured image - API ya ofele ya Bluesky Esalisaka Ngai Na Curer Ba Posts Na Ngai Ya Likolo Neti Jamais Avant
Noah HackerNoon profile picture

Yekola ndenge ya kozua na programme mpe ko intégrer ba posts na yo ya Bluesky oyo olingaka na nzela ya authentification, ba appels ya API, na ba points d’arrêt ya oEmbed.


Nauti kosala déplacement likolo ya Bluesky . Nakoki déjà ko confirmer que ezali na communauté tech vibrante kuna na ba tonnes ya contenus intéressants, utile, et inspirants. Nazali mosaleli ya sika ya esengo! Na yango, nazalaki na mposa ya kokɔtisa ba posts na ngai ya Bluesky oyo nalingaka mingi na ba posts na ngai ya sanza na sanza ya zulunalo "Dev roundup". Objectif na ngai ezali ya kopesa liste curée ya ba posts ya Bluesky oyo ezali spécifiquement adapté na ba Développeurs ya Logiciel.


Heureusement, API ya Bluesky ezali entièrement libre ya kosalela, permettant accès programmatique na nionso ya contenus na kati. Mateya oyo ekotambola na yo na nzela ya kozwa mpe kokɔtisa ba posts ya Bluesky oyo olingaka na kosalelaka API na bango, oyo ebongi mpenza mpo na ba blogs personnels, ba portefeuilles, to ba projets ya agrégation ya contenus.

Kososola Mosala ya mosala na ngai ya API Bluesky

Natongaki script oyo epesi ngai nzela ya ko intégrer automatiquement ba posts na ngai ya Bluesky na poste ya blog ya markdown. Nabanzi ete moko to nyonso ya matambe oyo esalelami na script oyo ezali na motuya mpo na makambo mingi ya kosalela.


Mpo na koloba na mokuse ndenge na ngai ya mosala mpo na kokɔtisa ba posts oyo esepelisi, tolandaka makambo oyo ya ntina:

  1. Bosala session oyo e authentiqué
  2. Recuperer ba URI ya post oyo esepelisaki pona "acteur".
  3. Salelá ba URI oyo mpo na kozwa oEmbed embed HTML
  4. Bopeto pe bo formater code ya embed


Bosaleli ya mobimba

Tokabola fonction moko na moko na tina na yango:

1. Kosala Session ya Bluesky

 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; } };


Boyebi ya ntina:

  • Fonction oyo e authentiquer compte na yo ya Bluesky.
    • Liyebisi: ndakisa oyo ezo hardcodes ba chaînes pona ba credentiels, kasi esengeli ko éviter yango na ba cas ya usage ya production.
  • Ezongisaka accessJwt JWT (JSON Web Token) mpo na mabiangi ya API oyo elandi
  • Esalelaka esika ya suka createSession uta na ATP ya Bluesky (Protocole ya transfert authentiqué) .
  • Bosalisi ya mabunga esalaka ete bozangi ya grace soki bondimi elongi te

2. Kozwa ba URI ya Post oyo esepeli

 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; };


Boyebi ya ntina:

  • Esengaka jeton ya session oyo endimami
  • Esalelaka esika ya suka ya getActorLikes mpo na kozwa ba posts oyo esepelisi
  • Likambo ya ntina -- domaine ya esika ya suka esengeli kozala https://bsky.social, lokola oyo ezali bosengi oyo endimami.
  • Ebimisaka ba URI oyo ekeseni na mosusu mpo na post moko na moko oyo esepelisi
  • Limite na 40 posts (configurable) .

3. Kobongola ba URI na HTML Embeddable

 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; } };


Boyebi ya ntina:

  • Esalelaka esika ya suka ya oEmbed ya Bluesky na ba URI ya post mpo na kozua HTML oyo ekoki kokɔtisama ya poste
  • Optional: Esalelaka prettier mpo na ko formater HTML mbala na mbala
  • Na bolingi: Elongolaka ba balises <script> mpo na bokengi mpe botiami ya peto
    • Ntina ya likambo oyo ezali ete nakɔtisaka script moko ya Bluesky mpo na poste mokomoko oyo ezali na makambo ya Bluesky.
  • Bosalisi ya mabunga na ndenge ya kobongola

Kotia Yango Nyonso Esika: Ndakisa ya mobimba

 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); } }

Makambo oyo ekoki kobongisama

Solution oyo esalaka pona ngai po nionso oyo nasengaka ezali poste ya blog ya sanza na sanza oyo esalemi statique.


Mwa makambo oyo bakoki kobongisa ekoki kozala:

  • Bakisa soutien ya pagination pona kozua ba posts koleka 40 oyo ba aimé
  • Salelá caching mpo na kokitisa ba appels ya API oyo ezali na ntina te
  • Bosala mécanisme ya traitement ya erreur ya makasi koleka
  • Kosala mécanisme ya ko refresher accessJwt token soki esalelami na ba processus ya longue durée
  • Kobongola ba posts oyo ba aimé na popularité (like) .

Toli ya kosilisa mikakatano

  • Verifier ba credentiels na yo ya Bluesky ezali correct
  • Tala soki jeton ya Bearer ezali kotiama malamu na ba demandes na yo oyo endimami.
  • Verifier que ba domaines ya point final oyo ozali kosalela nionso ezali valide.

Maloba ya nsuka

Ko kotisa ba posts ya Bluesky epesaka moyen dynamique ya kolakisa ba interactions na yo na ba médias sociaux. Na kososolaka mosala ya API mpe kosalelaka bokambami ya mabunga ya makasi, okoki kosala bosangisi ya makambo oyo ekobenda likebi, oyo ebongisami mpo na moto ye moko, mpe oyo ebongisami.

Matambe oyo ekolanda

  1. Meká kosala code yango
  2. Personnaliser styling ya embed
  3. Tala ba points d'arrêt ya API ya Bluesky ya kobakisa


Sepela mpe esengo tinkering! 🚀