Fetching Steemmonsters' game data into the MongoDB
steemmonsters·@emrebeyler·
0.000 HBDFetching Steemmonsters' game data into the MongoDB
I use a similar approach at [Steem Monster Explorer](http://monsters.steeminator.com/) where I fetch custom JSON operations broadcasted by @steemmonsters constantly. (small sleep cycles) MongoDB works well for such small cases where you need to store and query dictionary (document) based data in a quick way.

#### Workflow
***
1. Fetch account_history and filter custom_json transactions
2. Ignore the follow plugin on custom jsons. (follow, unfollow, mute, reblog)
3. Insert the json data into the local mongodb instance
#### Script
***
```python
import json
import pymongo
from steem.account import Account
import time
def import_sm_actions(mongo_collection, op_count=None):
counter = 0
print("Fetching @steemmonsters' history")
account = Account('steemmonsters')
for custom_json in account.history_reverse(filter_by=["custom_json"]):
# stop at the max. limit
if op_count and counter == op_count:
break
if custom_json["id"] == "follow":
# do not sync follow, unfollow, mute, reblog ops.
continue
action_data = json.loads(custom_json["json"])
action_data.update({
"action_id": custom_json["id"],
"timestamp": custom_json["timestamp"],
"trx_id": custom_json["trx_id"],
})
mongo_collection.update(
{'trx_id': custom_json["trx_id"]},
{'$set': action_data},
upsert=True
)
counter += 1
print(f"Total of {counter} operations indexed.")
def main():
mongo_collection = pymongo.MongoClient()["steem_monsters_db"]["actions"]
while True:
import_sm_actions(mongo_collection, op_count=100)
time.sleep(3)
if __name__ == '__main__':
main()
```
#### Notes
***
- Depending on your query behaviors you might need to add some indexes. (Otherwise full collection scan might hurt query times.)
- This code is POC. I have an enhanced version at [Steem Monster Explorer](http://monsters.steeminator.com/). However, that's a good start if you're into playing with these kind of data.
👍 emrebeyler, sereze, mcanimation, saluton, omersurer, bobdos, evilest-fiend, elchin, tolgahanuzun, zcool, bigboy9, boontjie, acun, teamhumble, berkerpeksag, hahajin, firatozbek, tugbabeauty, feronio, lastozgur, tipitip, tinowhale, khaliloff, votebott, siriusgaia, toninux, jacobkaled, rightscomet, nudgent, enjoyy, thinkermyles, lukecreed, fromhell2sky, carment, andreas-winkler, rechellomataro, kryptorero, gydronium, basav, ewq, canburaksimsek, loosely-lucid, anthonyadavisii, randohealer, trump3t, flagawhale, neutralizer, dailythoughts, nullahnung, qustodian, ruel.cedeno, googletr, atalante, isnochys, decebal2dac, senseofhumor, drkcyd, sebastiaaanb, uzerebru, the-naked-geek, hyroniz, uzercanan, oyundelisi, malay11, tentalavera, iwanderela, veleje, oguzcan, flugschwein, parakazan, damla, oendertuerk, witnessbot, punky, forkonti, ahmetmertugrul, ahmeterbay, muratkbesiroglu, jumbot, hellowhale, steemerturk, turkolog, ataturk, hayirhah, ucmuharfli, embesilikat, gulumserunver, theturk, emirfirlar, gcamkerten, cashthekush, giftbox, agememnon, rainbowlord, kemalyokus, trsteemer, firster78, canku, alisari, batuhoca, junostar, bboyabluka, vaire, trsteemian, varolleng, teaminfo, bircan, airdroptr1, gemplaymaker, semihbalkanli, mesutkrgln, emotionalsea, omitaylor, beesteem, dasha, sunshine, proteus, brains, alnedkov, zymer, steemvoter, luna33, ilyastarar, greengreen, pointeblue, miloudett, sreepyeldarb, rockz, masummim50, hdmed, mariostucablog, eloyibarra, sniffbiff, bigdizzle91, badol1994, fmalafaya, rynergy, thi-js, gbd, lucentbritex, getboost, arhitekt, mrsadman, dalethewhale, mumin007, ccsbot, exp500p, theresistance, masonmiler, amreshchandra, taxguy, camillesteemer, trollshunter, ahmetchef, sambillingham, privacybydesign,