javascript + hive api (bridge), to fetch ALL posts for a certain tag.

in #hive-dev3 months ago (edited)
<!DOCTYPE html>
<html>
<body>
    api url:
    <input id="api_url_field">
    <br>
    tag:
    <input id="tag_field">
    <br>
    <button id="go_button">GO</button>    
<script>

let api_url="https://api.hive.blog"
let tag = "mc2022"

let api_url_field = document.getElementById("api_url_field")
let tag_field = document.getElementById("tag_field")
let go_button = document.getElementById("go_button")

api_url_field.value = api_url
tag_field.value = tag

let get_posts = async(api_url, tag, start_author = "", start_permlink = "") => {
    let fetched = await fetch(
        api_url, {
            method: "POST",
            body: JSON.stringify({
                "id": 1,
                "jsonrpc": "2.0",
                "method": "bridge.get_ranked_posts",
                "params": {
                    "sort": "created",
                    "tag": tag,
                    "observer": "",
                    "limit": 100,
                    "start_author": start_author,
                    "start_permlink": start_permlink
                }
            })
        }
    )    
    let result = await fetched.json()    
    return result
}

let get_all_posts = async(api_url, tag) => {
    console.log("fetching...")
    let all_posts = []
    let reply = await get_posts(api_url, tag)
    let posts = reply.result

    all_posts = all_posts.concat(posts)   

    while(posts.length>99){   
        console.log("fetching more...")     
        let start_author = posts[99].author
        let start_permlink = posts[99].permlink
        reply = await get_posts(api_url, tag, start_author, start_permlink)
        posts = reply.result
        all_posts = all_posts.concat(posts)
    }

    return all_posts
}

let go = async() => {
    api_url = api_url_field.value 
    tag = tag_field.value 
    let all_posts = await get_all_posts(api_url, tag)
    console.log(all_posts)
}

go_button.onclick = go

</script>
</body>
</html>

Node operators will probably hate me for dumping this, but I don't really care anymore.

Save this as scrape_tag.html and open in browser

right-click -> 'inspect'
go to console:

image.png

(screenshot made in firefox browser)

This should work for any device that has a browser.

These are all posts with the tag #mc2022, sorted by date (latest first)
If you need to export those as CSV or whatever, I can also build that, but since gatekeeping is the thing here, I feel like I have given away enough for free. Learn to code, or contact me directly.

For someone actually trying to build something, this should be much more helpful than all examples provided under https://developers.hive.io/ combined.
No virtual environment needed, no libraries, no weird fluff.

You are welcome

Now follow me, vote this and start flaming.

Sort:  

very cool little tool!

Thanks!

It might be little, but it's strong :P

Warum sagt mir keiner, dass ich da nen bug hab :D

habs jedenfalls überarbeitet, geht jetz.

!WITZ

Was macht ein Pirat am Computer?
Er drückt die Enter-Taste.

Credit: werkseber
@felixxx, ich habe dir im Namen von @thehockeyfan-at einen $LOLZ Token gesendet
Verwende den Befehl !WITZ oder !LOOL, um einen Witz und ein $LOLZ zu teilen.

Delegate Hive Tokens to Farm $LOLZ and earn 110% Rewards. Learn more.
.(2/8)

Now I feel like learning coding😌

I should offer Hive API bootcamps for profit :D