This repository was archived by the owner on Nov 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
137 lines (95 loc) · 3.2 KB
/
parser.py
File metadata and controls
137 lines (95 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import json
import argparse
from funcy.flow import silent
from steem.post import Post
from mongoengine import connect
from steem.blockchain import Blockchain
from main import DB_HOST, DB_NAME
from posts.models import PostModel
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Command line tool to interact with the DTrip Backend")
parser.add_argument(
'--resync',
nargs='?',
const=True,
help='Resync database'
)
args = parser.parse_args()
APP_TAG = os.getenv('APP_TAG', 'dtrip')
mongo = connect(DB_NAME, host=DB_HOST)
if args.resync:
mongo.drop_database(DB_NAME)
class Settings:
def __init__(self, mongo):
self._settings = mongo[DB_NAME]['settings']
if not self._settings.find_one():
self._settings.insert_one({
"last_block": None,
})
def last_block(self):
return self._settings.find_one().get('last_block', 1)
def update_last_block(self, block_num):
return self._settings.update_one({}, {"$set": {'last_block': block_num}})
settings = Settings(mongo)
blockchain = Blockchain()
BLOCK_NUM = settings.last_block() or blockchain.get_current_block_num()
print(BLOCK_NUM)
# BLOCK_NUM = 26052441
# print(BLOCK_NUM) # POST 26052437
def fix_legacy_json(json):
# Coordinates
if 'location' in json and isinstance(json['location'], dict):
if json['location'].keys() >= {'name', 'lat', 'lng'}:
json['location'] = {
'geometry': {
'type': 'Point',
'coordinates': [
json['location']['lng'],
json['location']['lat']
]
},
'properties': {
'name': json['location']['name']
}
}
return json
def handle_post(post):
meta = silent(json.loads)(post['json_metadata']) or {}
if not isinstance(meta, dict):
return
meta.setdefault('tags', post['parent_permlink'])
if APP_TAG in meta['tags']:
post = Post(post).export()
if post['depth'] > 0:
return
del post['id']
post['tags'] = list(post['tags'])
post['json_metadata'] = fix_legacy_json(meta)
PostModel.objects(url=post['url']).update(upsert=True, **post)
print('saved', post['url'])
def get_posts(query):
return blockchain.steem.get_discussions_by_created(query)
def parse():
if args.resync:
query = {'tag': APP_TAG, 'limit': 100}
posts = get_posts(query)
while True:
for post in posts:
handle_post(post)
query['start_author'] = post['author']
query['start_permlink'] = post['permlink']
posts = get_posts(query)
if len(posts) == 1 and posts[0]['url'] == post['url']:
break
print('Posts Synced')
for op in blockchain.stream(filter_by=['comment'], start_block=BLOCK_NUM):
handle_post(op)
settings.update_last_block(op['block_num'])
print(settings.last_block())
if __name__ == '__main__':
try:
parse()
except KeyboardInterrupt:
print('Exit...')