It's been my experience that the best way to learn a technology is to try to create something, to work on building a project using that technology.
You might not always succeed with the project as envisioned, you might fail to get even close, but it definitely always helps you learn because it puts the learning into context.
Another way I have found to help me, especially when it comes to programming, is to imagine I need to teach someone else what I am learning.
That seems to make connections in my grey matter that wouldn't ordinarily be made.
Unfortunately, it seems Einstein did not say the famous quote attributed to him, but I still believe it holds a lot of truth:
"if you can't explain it simply you don't understand it well enough"
So what is the project here?
I've mentioned to a few people that I would like to make an RPG game that runs on the Hive blockchain.
Not because I have any chance of making something as popular as Splinterlands (heck, I don't even understand Splinterlands) but because I think creating something that is fun for me and possibly others will allow me to learn the blockchain technology better than just reading documentation and white papers.
The foundation of this project will require reading content from the blockchain and writing to the blockchain, so I will start there.
Reading Your Blog
This brings me to the title of the post, how to read your most recent article from your own (or anyone else's) blog.
It starts with the Python library, Beem
.
You can install that by name, and hopefully its requirements, with pip
.
Then you include it or specify individual parts that you need with something like from beem import Hive
The reason we need to specify Hive is Beem was and is also for Steem.
Blog Entries
Using Account
we can access an individual blog's entries like so:
from beem.account import Account
acc = Account("makerhacks")
for h in acc.get_blog_entries():
print(h)
This will give you the basic information such as the ID, the permalink and so on.
Get an Individual Article
Confusingly, we get our blog content from Comments or Discussions. I don't know about you but I expected something called "Content" or "Blog Posts" or "Articles", and Discussions to be comments or something. Weird.
from beem.comment import Comment
If we use Comment
we can access the detailed information about our post using the author + permalink.
h=acc.get_blog_entries()[0]
c=Comment('@'+h['author']+'/'+h['permlink'])
print(c.json())
Here we get the first entry in the blog and use the author+perm to retrieve the details then we output it using the JSON.
Another approach, using Discussions
, is to query with only one result returned (limit=1
), obviously you can increase that if you want to get more results.
Python Code
import pprint
# initialize Hive class
from beem import Hive
from beem.discussions import Query, Discussions_by_blog as Discussions
from beem.comment import Comment
h = Hive()
q = Query(limit=1, tag="makerhacks")
#post list for selected query
posts = Discussions(q)
#posts list
for post in posts:
details = Comment(post)
# print post body for selected post
pprint.pprint('Depth: ' + str(details.depth))
pprint.pprint('Author: ' + details.author)
pprint.pprint('Category: ' + details.category)
pprint.pprint('Body: ' + details.body)
What Did We Learn?
Well other than the confusion around why blog posts are considered discussions (perhaps it was envisioned as a forum instead of a blog?) we did manage to list an account's blog posts and get the blog content.
Problem
There is a problem with this, however, in that people could get very annoyed if I use blog content to populate a game (especially as the generally given rule is to only post once per day) so it will be most wise I think to use replies to a dummy parent post with no payouts, so next time I will look at that.
I think originally, the whole thing was set up to be more like a Reddit experience.
But here we are...
Ahh ok
Hi -
I have written a small python application where my goal is to add up "total_payout_value" and "pending_payout_value" for a specific account's comments to any posts on Hive. It works BUT only up to limit=100. Is that an issue with the API being set up to limit only to 100 unless I sign in within the application? Or ... ? If so, is there a way to use my hive keys in my app to allow me to increase limit to infinite (all posts)? Thanks! Happy to share the code I am using if you need/want to see it.
Thanks!
Scott
@kennyskitchen
It could be that you need to page through results over 100 items, so the second page of results would be 101 onwards?
With much respect, I doubt that, since our for loop does not have any "range" or limits. It goes through the whole set... right?
Do you have any recommendations where I can look to find out?
The API documentation doesn't really tell me what I want... at least I found no detailed explanation of "limit=".