-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankGenerator.py
90 lines (82 loc) · 3.01 KB
/
rankGenerator.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
import pymysql.cursors
from random import randint
from config import dbConfig, rankGeneratorConfig
def getPostCount():
query = "SELECT id FROM wp_posts WHERE site = '{}' ORDER BY id DESC LIMIT 1".format(site)
cursor.execute(query)
rankGeneratorConfig['totalPosts'] = cursor.fetchone()[0]
def getPosts(limit, offset):
query = ("SELECT id, ES, US, MX, CO FROM wp_posts WHERE site = '{}' AND id > {} ORDER BY id ASC LIMIT {}".format(site, offset, limit))
cursor.execute(query)
result = cursor.fetchall()
return result
def batchInsertRank(ranks):
query = ",".join(ranks)
query = (
"INSERT INTO posts_queue ("
" `post_id`,"
" `country`,"
" `rank`,"
" `site`"
") VALUES " + query)
cursor.execute(query)
connection.commit()
def createCountryRankList(ranks, postId, country):
rankConfig[country] += 1
ranks.append("({}, '{}', {}, '{}')"
.format(postId, country, rankConfig[country], site))
return ranks
def createRankList(ranks, dbData):
if dbData[1] == 1:
ranks = createCountryRankList(ranks, dbData[0], 'ES')
if dbData[2] == 1:
ranks = createCountryRankList(ranks, dbData[0], 'US')
if dbData[3] == 1:
ranks = createCountryRankList(ranks, dbData[0], 'MX')
if dbData[4] == 1:
ranks = createCountryRankList(ranks, dbData[0], 'CO')
return ranks
def insertRank(limit, offset):
ranks = []
dbData = getPosts(limit, offset)
for data in range(limit):
ranks = createRankList(ranks, dbData[data])
batchInsertRank(ranks)
def rankGenerator():
getPostCount()
offset = 0
limit = rankGeneratorConfig['totalPosts']
if (rankGeneratorConfig['totalPosts'] < rankGeneratorConfig['batchSize']):
insertRank(limit, offset)
else:
batchCount = rankGeneratorConfig['totalPosts'] // rankGeneratorConfig['batchSize']
remainderBatch = rankGeneratorConfig['totalPosts'] % rankGeneratorConfig['batchSize']
limit = rankGeneratorConfig['batchSize']
for count in range(batchCount):
insertRank(limit, offset)
offset = (count + 1) * rankGeneratorConfig['batchSize']
if remainderBatch > 0:
limit = remainderBatch
insertRank(limit, offset)
print("ES Ranks Generated : {}".format(rankConfig['ES']))
print("US Ranks Generated : {}".format(rankConfig['US']))
print("MX Ranks Generated : {}".format(rankConfig['MX']))
print("CO Ranks Generated : {}".format(rankConfig['CO']))
def process(rankSite):
global connection, cursor
global site, rankConfig
rankConfig = {
'ES': 0,
'US': 0,
'MX': 0,
'CO': 0
}
connection = pymysql.connect(**dbConfig)
cursor = connection.cursor()
site = rankSite
print("=====================================================")
print("Generating ranks for ---> {}".format(site))
rankGenerator()
print("=====================================================")
cursor.close()
connection.close()