-
Notifications
You must be signed in to change notification settings - Fork 4
/
data.py
327 lines (287 loc) · 7.24 KB
/
data.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import datetime
import json
import pymssql
import sys
import config
data = {}
cur_categories = [
["life"],
["photography"],
["steemit"],
["introduceyourself"],
["esteem"],
["bitcoin"],
["food"],
["cryptocurrency"],
["art"],
["travel"],
["dlive"],
["spanish"],
["busy"],
["nature"],
["health"],
["news"],
["music"],
["blockchain"],
["crypto"],
["love"],
["blog"],
["steem"],
["poetry"],
["funny"],
["kr"],
["story"],
["ulog"],
["blocktradesworldcup"],
["writing"],
["dmania"],
["photo"],
["colorchallenge"],
["ico"],
["indonesia"],
["technology"],
["sports"],
["money"],
["gaming"],
["animal"],
["dtube"],
["motivation"],
["meme"],
["flower"],
["animalphotography"],
["smartphonephotography"],
["steemhunt"],
["science"],
["cervantes"],
["tasteem"],
["history"],
["football"],
["aceh"],
["contest"],
["mgsc"],
["dsound"],
["eos"],
["macrophotography"],
["foodphotography"],
["education"],
["landscapephotography"],
["introducemyself"],
["video"],
["politics"],
["sport"],
["animals"],
["goldenhourphotography"],
["streetphotography"],
["flowers"],
["steepshot"],
["new"],
["airdrop"],
["poem"],
["deutsch"],
["utopian-io"],
["game"],
["family"],
["steemchurch"],
["colourfulphotography"],
["cn"],
["artzone"],
["ethereum"],
["fun"],
["zappl"],
["movie"],
["architecturalphotography"],
["business"],
["steempress"],
["promo-steem"],
["inspiration"],
["vehiclephotography"],
["tr"],
["openmic"],
["nsfw"],
["venezuela"],
["introduction"],
["fruit"],
["polish"],
["cat"],
["quarkchain"],
["worldcup"]
]
def initData():
global data
data = {
'nodes': [],
'links': [],
'snapshot': str(datetime.date.today()),
'count': 0,
'max': 0
}
def connect():
return pymssql.connect(
config.server, config.user, config.password, config.database)
def selectCategoryAccounts(category):
print('category accounts #' + category)
connection = connect()
# category
SQLCommand = ('''
SELECT distinct TOP 100
name,
FLOOR((CAST(REPLACE(vesting_shares, ' VESTS','') AS float)
+ CAST(REPLACE(received_vesting_shares, ' VESTS','') AS float)
- CAST(REPLACE(delegated_vesting_shares, ' VESTS','') AS float))
/ 2036) as 'STEEM POWER',
COUNT(Comments.created) as 'COUNT'
FROM Accounts (NOLOCK)
LEFT JOIN Comments (NOLOCK) ON Accounts.name = Comments.author
WHERE category = \'''' + category + '''\'
AND FLOOR((CAST(REPLACE(vesting_shares, ' VESTS','') AS float)
+ CAST(REPLACE(received_vesting_shares, ' VESTS','') AS float)
- CAST(REPLACE(delegated_vesting_shares, ' VESTS','') AS float))
/ 2036) > 25
AND Comments.created >= DATEADD(MONTH, -3, GETDATE())
AND depth = 0
GROUP BY name, vesting_shares, received_vesting_shares, delegated_vesting_shares
ORDER BY 'STEEM POWER' DESC
''')
cursor = connection.cursor()
cursor.execute(SQLCommand)
for row in cursor:
duplicate = False
for node in data['nodes']:
if node['id'] == row[0]:
for link in data['links']:
if link['source'] == row[0]:
if row[2] > link['strength']:
node['category'] = '#' + category
link['target'] = '#' + category
link['strength'] = 100 if row[2] > 100 else row[2]
# set to end of list for grouping purposes
data['nodes'].append(data['nodes'].pop(data['nodes'].index(node)))
break
duplicate = True
break
if not duplicate:
data['nodes'].append({ 'id': row[0], 'sp': int(row[1]), 'category': '#' + category })
data['links'].append({ 'source': row[0], 'target': '#' + category, 'strength': 100 if row[2] > 100 else row[2] })
# data['count'] += 1
if data['max'] < row[2]:
data['max'] = row[2]
connection.close()
def selectTopCategories():
connection = connect()
SQLCommand = ('''
SELECT TOP 100
category,
COUNT(DISTINCT author) as 'count'
FROM comments (NOLOCK)
WHERE created >= DATEADD(MONTH, -3, GETDATE())
AND depth = 0
GROUP BY category
ORDER BY 'count' DESC
''')
cursor = connection.cursor()
cursor.execute(SQLCommand)
cur = cursor.fetchall()
for row in cur:
data['nodes'].append({ 'id': '#' + row[0], 'count': row[1], 'type': 'tag' })
if row[0] == 'steem':
data['nodes'][-1]['fx'] = 0
data['nodes'][-1]['fy'] = 0
data['nodes'][-1]['fz'] = 0
sumSecondaryTags(cur)
print(json.dumps(data))
with open('categories.json', 'w') as file:
file.write(json.dumps(data))
connection.close()
return cur
def sumSecondaryTags(cur):
connection = connect()
for category in cur:
print('#' + category[0] + ', ' + str(category[1]))
tempTags = {}
for tag in cur:
if category[0] == tag[0]:
continue
print(str(datetime.datetime.now()) + ' ' + str(tag))
SQLCommand = ('''
SELECT
COUNT(DISTINCT author)
FROM comments (NOLOCK)
WHERE created >= DATEADD(MONTH, -3, GETDATE())
AND category = \'''' + category[0] + '''\'
AND depth = 0
AND ISJSON(json_metadata) > 0
AND CONTAINS(json_metadata, \'''' + tag[0] + '''\')
AND \'''' + tag[0] + '''\' IN (SELECT value FROM OPENJSON(json_metadata, '$.tags'))
''')
# --AND CONTAINS(JSON_VALUE(json_metadata, '$.tags), \'''' + tag[0] + '''\')
# --AND JSON_VALUE(json_metadata, '$.tags[''' + str(i) + ''']') IS NOT NULL
# --AND JSON_VALUE(json_metadata, '$.tags[''' + str(i) + ''']') = \'''' + tag[0] + '''\'
# ''')
cursor = connection.cursor()
cursor.execute(SQLCommand)
for row in cursor:
if row[0] > int(category[1] / 5):
tempTags[tag[0]] = row[0]
data['links'].append({ 'source': '#' + category[0], 'target': '#' + tag[0], 'strength': round(row[0] / category[1], 2) })
print(tempTags)
connection.close()
# unused
def selectCategoryTags(category = 'polish'):
connection = connect()
sum = 0
tags = {}
# category
SQLCommand = ('''
SELECT
COUNT(*)
FROM comments (NOLOCK)
WHERE category = \'''' + category + '''\'
AND created >= DATEADD(MONTH, -1, GETDATE())
AND depth = 0
''')
cursor = connection.cursor()
cursor.execute(SQLCommand)
for row in cursor:
sum += row[0]
if category not in tags:
tags[category] = 0
tags[category] += row[0]
# tags
for i in range(1, 5):
SQLCommand = ('''
SELECT TOP 100
JSON_VALUE(json_metadata, '$.tags[''' + str(i) + ''']'),
COUNT(JSON_VALUE(json_metadata, '$.tags[''' + str(i) + ''']')) as 'count'
FROM comments (NOLOCK)
WHERE category = \'''' + category + '''\'
AND created >= DATEADD(MONTH, -1, GETDATE())
AND depth = 0
AND ISJSON(json_metadata) > 0
AND JSON_VALUE(json_metadata, '$.tags[''' + str(i) + ''']') IS NOT NULL
GROUP BY JSON_VALUE(json_metadata, '$.tags[''' + str(i) + ''']')
ORDER BY 'count' DESC
''')
cursor = connection.cursor()
cursor.execute(SQLCommand)
cur = cursor.fetchall()
for row in cur:
print('count: ' + str(row[0]))
if row[0] not in tags:
tags[row[0]] = 0
tags[row[0]] += row[1]
for key, value in tags.items():
data['nodes'].append({ 'id': '#' + key, 'count': value })
if key == category:
data['nodes'][-1]['fx'] = 0
data['nodes'][-1]['fy'] = 0
data['nodes'][-1]['fz'] = 0
# sumSecondaryTags(cur)
print(json.dumps(data))
connection.close()
initData()
cur_categories = selectTopCategories()
initData()
for category in cur_categories:
selectCategoryAccounts(category[0])
with open('accounts.json', 'w') as file:
file.write(json.dumps(data))