-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
208 lines (163 loc) · 6.97 KB
/
utils.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
#-*- coding: utf-8 -*-
import json
import logging
import os
import traceback
import config
def log(msg, level = logging.DEBUG):
logging.log(level, msg)
print('level:%s, msg:%s' % (level, msg))
if level == logging.WARNING or level == logging.ERROR:
for line in traceback.format_stack():
print(line.strip())
for line in traceback.format_stack():
logging.log(level, line.strip())
def make_dir(dir):
log('make dir:%s' % dir)
if not os.path.exists(dir):
os.makedirs(dir)
def format_json(data):
try:
return json.dumps(json.loads(data), indent = 4)
except Exception, e:
log('format_json exception msg:%s' % e, logging.WARNING)
return data
def create_table(sql, table_name):
commond = ("CREATE TABLE IF NOT EXISTS {} ("
"`id` INT(6) NOT NULL PRIMARY KEY UNIQUE,"
"`name` TEXT NOT NULL,"
"`asset_url` TEXT DEFAULT NULL,"
"`rating_count` INT(4) DEFAULT 0,"
"`rating_comments_count` INT(4) DEFAULT 0,"
"`rating_comments_ratio` FLOAT DEFAULT 0.0,"
"`rating_average` INT(4) DEFAULT 0,"
"`rating_five` INT(4) DEFAULT 0,"
"`rating_five_ratio` FLOAT(4) DEFAULT 0.0,"
"`rating_four` INT(4) DEFAULT 0,"
"`rating_three` INT(4) DEFAULT 0,"
"`rating_two` INT(4) DEFAULT 0,"
"`rating_one` INT(4) DEFAULT 0,"
"`pubdate` TEXT NOT NULL,"
"`category` TEXT NOT NULL,"
"`version` TEXT NOT NULL,"
"`price_USD` FLOAT DEFAULT NULL,"
"`price_JPY` FLOAT DEFAULT NULL,"
"`price_DKK` FLOAT DEFAULT NULL,"
"`price_EUR` FLOAT DEFAULT NULL,"
"`sizetext` TEXT DEFAULT NULL,"
"`publisher_name` TEXT DEFAULT NULL,"
"`publisher_url` TEXT DEFAULT NULL,"
"`publisher_support_url` TEXT DEFAULT NULL,"
"`publisher_email` TEXT DEFAULT NULL,"
"`first_published_at` TEXT DEFAULT NULL"
") ENGINE=InnoDB".format(table_name))
sql.create_table(commond)
def export_to_sql(sql, dir_all):
for i, file in enumerate(os.listdir(dir_all)):
file_name = '%s/%s' % (dir_all, file)
log('export_to_sql file_name:%s' % file_name)
names = file_name.split('_')
if names[len(names) - 1] == 'list.json' or names[len(names) - 1] == 'comments.json':
continue
if file_name.find('.json') == -1:
continue
name = file[:-5]
try:
name = int(name)
except:
pass
if type(name) is not int:
continue
try:
insert_to_sql(sql, file_name, config.assetstore_table_name)
except:
pass
def insert_to_sql(sql, file_name, table_name):
names = file_name.split('_')
if names[len(names) - 1] == 'list.json' or names[len(names) - 1] == 'comments.json':
return
if file_name.find('.json') == -1:
return
log('insert_to_sql file_name:%s' % file_name)
with open(file_name, 'r') as f:
plugin_source = f.read()
f.close()
plugin = json.loads(plugin_source)
content = plugin.get('content')
id = content.get('id', '')
name = content.get('title', '')
asset_url = 'https://www.assetstore.unity3d.com/en/#!/content/%s' % id
rating = content.get('rating', '')
rating_count = rating.get('count', '0')
if rating_count == None:
rating_count = 0
rating_average = rating.get('average', '0')
pubdate = content.get('pubdate', '')
category = content.get('category', '')
category_lable = category.get('label', '')
version = content.get('version')
price = content.get('price', '')
if price == '':
price_USD = 0
price_JPY = 0
price_DKK = 0
price_EUR = 0
else:
price_USD = price.get('USD', '0')
price_JPY = price.get('JPY', '0')
price_DKK = price.get('DKK', '0')
price_EUR = price.get('EUR', '0')
sizetext = content.get('sizetext', '')
publisher = content.get('publisher', '')
publisher_name = publisher.get('label', '')
publisher_url = publisher.get('url', '')
publisher_support_url = publisher.get('support_url', '')
publisher_email = publisher.get('support_email', '')
first_published_at = content.get('first_published_at', '')
rating_comments_count = 0
rating_comments_ratio = 0
rating_five = 0
rating_five_ratio = 0
rating_four = 0
rating_three = 0
rating_two = 0
rating_one = 0
comment_file_name = '%s_%s' % (file_name[:-5], 'comments.json')
if os.path.exists(comment_file_name):
with open(comment_file_name, 'r') as f:
comment_source = f.read()
f.close()
plugin_comments = json.loads(comment_source)
comments = plugin_comments.get('comments', '')
rating_comments_count = plugin_comments.get('count', '0')
if rating_count != None and rating_count != '0' and rating_count != 0:
rating_comments_ratio = int(rating_comments_count) * 1.0 / int(rating_count)
rating_five = comment_source.count('"rating": "5"')
if rating_comments_count != None and rating_comments_count != '0' and rating_comments_count != 0:
rating_five_ratio = int(rating_five) * 1.0 / int(rating_comments_count)
rating_four = comment_source.count('"rating": "4"')
rating_three = comment_source.count('"rating": "3"')
rating_two = comment_source.count('"rating": "2"')
rating_one = comment_source.count('"rating": "1"')
command = ("INSERT IGNORE INTO {} "
"(id, name, asset_url, rating_count, rating_comments_count, rating_comments_ratio, "
"rating_average, rating_five, rating_five_ratio, rating_four, rating_three, "
"rating_two, rating_one, pubdate, category, version, price_USD, price_JPY, price_DKK, "
"price_EUR, sizetext, publisher_name, publisher_url, publisher_support_url, publisher_email, "
"first_published_at)"
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, "
"%s, %s, %s, %s, %s)".format(table_name))
msg = (id, name, asset_url, rating_count, rating_comments_count, rating_comments_ratio, rating_average,
rating_five, rating_five_ratio, rating_four, rating_three, rating_two, rating_one, pubdate,
category_lable,
version, price_USD, price_JPY, price_DKK, price_EUR, sizetext, publisher_name,
publisher_url, publisher_support_url, publisher_email, first_published_at)
sql.insert_data(command, msg)
def is_exists_sql(sql, id, table_name):
command = 'SELECT * FROM {0} WHERE id={1}'.format(table_name, id)
sql.cursor.execute(command)
rows = sql.cursor.fetchone()
if rows == None:
return False
else:
return True