from hogumathi_app import content_system as h_cs
import hogumathi_app.view_model as h_vm

def register_content_sources ():
    h_cs.register_content_source('instagram:post:', get_post, id_pattern='([a-zA-Z0-9\_\-]+)')


def get_post (post_id, is_embed=True):
    html = embed_template(post_id)
    post = h_vm.FeedItem(
        id = post_id,
        created_at = 'some time',
        display_name = 'IG User',
        handle = 'iguser',
        
        html = html
    )

    return post

def embed_template (post_id):
    """
    Pretty sure I made a little demo that fetches oembed to get detailed info.
    
    May be mistaken. The endpoint requires an access token.
    
    Perhaps I fetched the iframe embed and scraped?
    
    https://developers.facebook.com/docs/instagram/oembed/
    
    Maybe a token is not required:
    
    https://developers.facebook.com/docs/instagram/oembed-legacy
    
    Gives 404.
    
    I am starting to remember using the iframe embed and messing with params in browser; no scraping yet.
    
// for scraping
var postInfo = {
    caption: document.querySelector(".Caption").innerText.split("\n\n")[1],
    tags: Array.from(document.querySelectorAll(".Caption a")).map(a => a.innerText).slice(1), 
    follower_count: document.querySelector(".FollowerCountText").innerText,
    image_url: document.querySelector(".EmbeddedMediaImage").src,
    avatar_src: document.querySelector(".Avatar img").src,
    username: document.querySelector(".UsernameText").innerText,
    source_url: document.querySelector(".EmbeddedMedia").href,
    author_source_url: document.querySelector(".ViewProfileButton").href
    }
    """
    return f'<iframe class="feed_item" width="100%" height="800" src="http://instagram.com/p/{post_id}/embed/captioned" frameborder="0"></iframe>'