-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostsPopulator.py
More file actions
76 lines (73 loc) · 2.57 KB
/
Copy pathpostsPopulator.py
File metadata and controls
76 lines (73 loc) · 2.57 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
import pymysql.cursors
from random import randint
from faker import Faker
from config import dbConfig, postTypeChoice, postsPopulatorConfig, fakeText, countryChoice, specialChoice
# from timer import Timestamp
def batchInsertPosts(posts):
query = ",".join(posts)
query = (
"INSERT INTO `wp_posts` ("
" `site`,"
" `text`,"
" `published`,"
" `ES`,"
" `US`,"
" `MX`,"
" `CO`,"
" `type`,"
" `url`,"
" `special`"
") VALUES " + query)
cursor.execute(query)
connection.commit()
def insertPosts():
posts = []
countriesWeighted = 0
for numberOfPosts in range(1, (postsPopulatorConfig['postsCount'] + 1)):
fakeTextStart = randint(0,997000)
fakeTextEnd = fakeTextStart + 3000
text = fakeText[fakeTextStart:fakeTextEnd] + fake.pystr(max_chars = 20)
# published = timestamp.random()
published = fake.date_time_between(start_date = "-6y", end_date = "now")
countriesWeighted = countryChoice(countriesWeighted)
ES = countriesWeighted[0]
US = countriesWeighted[1]
MX = countriesWeighted[2]
CO = countriesWeighted[3]
postType = postTypeChoice()
url = fake.uri() + fake.pystr(max_chars = 10)
special = specialChoice()
posts.append("('{}', '{}', '{}', {}, {}, {}, {}, '{}', '{}', {})"
.format(
site,
text,
published,
ES,
US,
MX,
CO,
postType,
url,
special))
if (numberOfPosts % postsPopulatorConfig['batchSize']) == 0:
batchInsertPosts(posts)
posts = []
print("Posts created : {}".format(postsPopulatorConfig['batchSize']))
if posts != []:
batchInsertPosts(posts)
print("Posts created : {}".format(numberOfPosts % postsPopulatorConfig['batchSize']))
print("Posts Insertion completed for {} posts".format(numberOfPosts))
def process(postSite):
global connection, cursor, fake#, timestamp
global site
connection = pymysql.connect(**dbConfig)
cursor = connection.cursor()
fake = Faker()
# timestamp = Timestamp()
site = postSite
print("=====================================================")
print("Generating Post data for ---> {}".format(site))
insertPosts()
print("=====================================================")
cursor.close()
connection.close()