-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_products.py
239 lines (183 loc) · 6.09 KB
/
extract_products.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
import json
import requests
# from warcio import ArchiveIterator
from pymongo import MongoClient
import wget
import urllib.request
import gzip
# import shutil
# import os
from termcolor import colored
import random
from bson.objectid import ObjectId
import multiprocessing
from deeppavlov import configs, build_model
import cdx_toolkit
import time
from product import extract_product_html
ner_model = build_model(configs.ner.ner_ontonotes_bert_torch, download=False)
COLLECTION_PHONE = 'Phones'
COLLECTION_EMAIL = 'Emails'
COLLECTION_LINKEDIN = 'Linkedin'
COLLECTION_FACEBOOK = 'Facebook'
COLLECTION_TWITTER = 'Twitter'
COLLECTION_YOUTUBE = 'Youtube'
COLLECTION_INSTAGRAM = 'Instagram'
##############################################################
client = MongoClient('localhost', 27017, maxPoolSize = 500)
db = client.Linkedin
collection_boobranda_total_cleaned = db['urls_05_2022_2']
collection_website_unique = db['linkedin_only_website_unique']
collection_phone = db[COLLECTION_PHONE]
collection_email = db[COLLECTION_EMAIL]
collection_linkedin = db[COLLECTION_LINKEDIN]
collection_facebook = db[COLLECTION_FACEBOOK]
collection_twitter = db[COLLECTION_TWITTER]
collection_youtube = db[COLLECTION_YOUTUBE]
collection_instagram = db[COLLECTION_INSTAGRAM]
##############################################################
def gunzip(source_filepath, dest_filepath, block_size=65536):
with gzip.open(source_filepath, 'rb') as s_file, \
open(dest_filepath, 'wb') as d_file:
while True:
block = s_file.read(block_size)
if not block:
break
else:
d_file.write(block)
def check_root_url(url):
if ( url== None ):
return False
url = url.strip()
url = url.replace('http://', '')
url = url.replace('https://', '')
url = url.replace('www.', '')
if (url.endswith('/')):
url = url[:-1]
if (len(url.split('/')) == 1):
return True
else:
return False
def check_contact_url(url):
if ( url== None ):
return False
if ('contact' in url.lower()):
return True
else:
return False
def verify_social_url (url, valid_part):
try:
url = url.replace('www.','').replace('http://','').replace('https://','')
while url.endswith('/'):
url = url[:-1]
split_list = url.split('/')
if (len(split_list) == valid_part):
return True
except Exception as e:
print(e)
return False
return False
def insert_phone_mongo (phone, url):
try:
insertJson ={'phone': phone, 'url': url}
collection_phone.insert_one(insertJson)
except Exception as e:
print(e)
def insert_email_mongo (email, url):
try:
domain = email.split('@')[-1]
insertJson ={'email': email, 'url': url, 'domain': domain.strip()}
collection_email.insert_one(insertJson)
except Exception as e:
print(e)
def insert_phone_mongo (phone, url):
try:
insertJson ={'phone': phone, 'url': url}
collection_phone.insert_one(insertJson)
except Exception as e:
print(e)
def insert_linkedin_mongo (linkedin, url):
try:
insertJson ={'linkedin': linkedin, 'url': url}
collection_linkedin.insert_one(insertJson)
except Exception as e:
print(e)
def insert_facebook_mongo (facebook, url):
try:
insertJson ={'facebook': facebook, 'url': url}
collection_facebook.insert_one(insertJson)
except Exception as e:
print(e)
def insert_twitter_mongo (twitter, url):
try:
insertJson ={'twitter': twitter, 'url': url}
collection_twitter.insert_one(insertJson)
except Exception as e:
print(e)
def insert_youtube_mongo (youtube, url):
try:
insertJson ={'youtube': youtube, 'url': url}
collection_youtube.insert_one(insertJson)
except Exception as e:
print(e)
def insert_instagram_mongo (instagram, url):
try:
insertJson ={'instagram': instagram, 'url': url}
collection_instagram.insert_one(insertJson)
except Exception as e:
print(e)
def get_from_mongo_url():
category = collection_boobranda_total_cleaned.find_and_modify(query={'tag': False}, update={'$set': {"tag": 'progress'}})
return category
def get_from_url():
return {'website': 'microsoft.com/en-us'}
while True:
try:
category = collection_website_unique.find_and_modify(query={'tag': False}, update={'$set': {"tag": 'progress'}})
return category
except Exception as e:
print(e)
time.sleep(10)
def update_mongo_url(id):
collection_boobranda_total_cleaned.find_and_modify(query={'_id': ObjectId(id)}, update={'$set': {"tag": True}})
def get_html_cdx_toolkit(url):
while True:
try:
cdx = cdx_toolkit.CDXFetcher(source='cc')
#
for obj in cdx.iter(url, limit=1):
try:
if (obj.content != None and obj.content != ''):
return obj.content
except Exception as e:
continue
return ''
except Exception as e:
print(e)
def a():
item = get_from_url()
while True:
if item == None:
time.sleep(20*1)
print(colored(' sleep !!!!' , 'magenta'))
item = get_from_url()
continue
url = item['website']
contents = get_html_cdx_toolkit(url)
if (contents == ''):
product_list = []
continue
print(colored(' produce ' + str(url) + ' started !', 'green'))
product_list = extract_product_html(contents)
if (product_list != []):
insert_instagram_mongo(product_list, url)
item = get_from_url()
###############################################################################
number_processes = 1
processes = []
for i in range(number_processes):
processes.append(multiprocessing.Process(target=a, args=[]))
for index, p in enumerate(processes):
p.start()
for index, p in enumerate(processes):
p.join()