instagram_embed_facade.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from hogumathi_app import content_system as h_cs
  2. import hogumathi_app.view_model as h_vm
  3. def register_content_sources ():
  4. h_cs.register_content_source('instagram:post:', get_post, id_pattern='([a-zA-Z0-9\_\-]+)')
  5. def get_post (post_id, is_embed=True):
  6. html = embed_template(post_id)
  7. post = h_vm.FeedItem(
  8. id = post_id,
  9. created_at = 'some time',
  10. display_name = 'IG User',
  11. handle = 'iguser',
  12. html = html
  13. )
  14. return post
  15. def embed_template (post_id):
  16. """
  17. Pretty sure I made a little demo that fetches oembed to get detailed info.
  18. May be mistaken. The endpoint requires an access token.
  19. Perhaps I fetched the iframe embed and scraped?
  20. https://developers.facebook.com/docs/instagram/oembed/
  21. Maybe a token is not required:
  22. https://developers.facebook.com/docs/instagram/oembed-legacy
  23. Gives 404.
  24. I am starting to remember using the iframe embed and messing with params in browser; no scraping yet.
  25. // for scraping
  26. var postInfo = {
  27. caption: document.querySelector(".Caption").innerText.split("\n\n")[1],
  28. tags: Array.from(document.querySelectorAll(".Caption a")).map(a => a.innerText).slice(1),
  29. follower_count: document.querySelector(".FollowerCountText").innerText,
  30. image_url: document.querySelector(".EmbeddedMediaImage").src,
  31. avatar_src: document.querySelector(".Avatar img").src,
  32. username: document.querySelector(".UsernameText").innerText,
  33. source_url: document.querySelector(".EmbeddedMedia").href,
  34. author_source_url: document.querySelector(".ViewProfileButton").href
  35. }
  36. """
  37. return f'<iframe class="feed_item" width="100%" height="800" src="http://instagram.com/p/{post_id}/embed/captioned" frameborder="0"></iframe>'