@ -5,6 +5,7 @@ import traceback
import configparser
import configparser
from MarketPlaces.Utilities.utilities import *
from MarketPlaces.Utilities.utilities import *
from dateutil.relativedelta import relativedelta , FR
from dateutil.relativedelta import relativedelta , FR
from scipy.spatial import distance
def connectDataBase ( ) :
def connectDataBase ( ) :
@ -88,6 +89,27 @@ def verifyItem(cur, marketId, hrefItem):
trace = traceback . format_exc ( )
trace = traceback . format_exc ( )
print ( trace )
print ( trace )
def verifyImage ( cur , base64Image ) :
try :
cur . execute ( " lock table market_images IN ACCESS EXCLUSIVE MODE " )
cur . execute ( " select image_id from market_images where hash_image = %(hashImage)s limit 1 " ,
{ ' hashImage ' : generate_image_hash ( base64Image ) } )
recset = cur . fetchall ( )
if recset :
return recset [ 0 ] [ ' image_id ' ]
else :
return 0
except :
trace = traceback . format_exc ( )
print ( trace )
def getLastMarketPlace ( cur ) :
def getLastMarketPlace ( cur ) :
try :
try :
@ -209,17 +231,19 @@ def create_vendor(cur, row, marketId):
if newVendor :
if newVendor :
sql = " Insert into vendors (vendor_id, market_id, name_vendor, rating_vendor, successfultransactions_vendor, image_vendor, dateinserted_vendor) " \
" Values ( %s , %s , %s , %s , %s , %s , %s ) "
imageId = create_image ( cur , row [ 21 ] , row [ 23 ] )
recset = [ vendorId , marketId ,
row [ 1 ] ,
row [ 2 ] if row [ 2 ] != ' -1 ' else None ,
row [ 3 ] if row [ 3 ] != ' -1 ' else None ,
row [ 21 ] if row [ 21 ] != ' -1 ' else None ,
row [ 23 ] ]
sql = " Insert into vendors (vendor_id, market_id, name_vendor, rating_vendor, successfultransactions_vendor, image_vendor, dateinserted_vendor) " \
" Values ( %s , %s , %s , %s , %s , %s , %s ) "
cur . execute ( sql , recset )
recset = [ vendorId , marketId ,
row [ 1 ] ,
row [ 2 ] if row [ 2 ] != ' -1 ' else None ,
row [ 3 ] if row [ 3 ] != ' -1 ' else None ,
row [ 21 ] if row [ 21 ] != ' -1 ' else None ,
row [ 23 ] ]
cur . execute ( sql , recset )
else :
else :
@ -229,11 +253,13 @@ def create_vendor(cur, row, marketId):
recset = cur . fetchall ( )
recset = cur . fetchall ( )
# decode_decrypt_image_in_base64(recset[0][5])
imageId = recset [ 0 ] [ ' image_vendor ' ]
if not is_same_image ( row [ 21 ] , recset [ 0 ] [ ' image_vendor ' ] ) ) :
imageId = create_image ( cur , row [ 21 ] , row [ 23 ] )
if ( str ( recset [ 0 ] [ ' rating_vendor ' ] ) != str ( row [ 2 ] if row [ 2 ] != ' -1 ' else None ) or # there was a change in the vendor information
if ( str ( recset [ 0 ] [ ' rating_vendor ' ] ) != str ( row [ 2 ] if row [ 2 ] != ' -1 ' else None ) or # there was a change in the vendor information
str ( recset [ 0 ] [ ' successfultransactions_vendor ' ] ) != str ( row [ 3 ] if row [ 3 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' successfultransactions_vendor ' ] ) != str ( row [ 3 ] if row [ 3 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' image_vendor ' ] ) != str ( row [ 21 ] if row [ 21 ] != ' -1 ' else None ) ) :
str ( recset [ 0 ] [ ' image_vendor ' ] ) != str ( imageId ) ) :
vendorVersionId = int ( getLastVendorVersion ( cur , vendorId ) + 1 )
vendorVersionId = int ( getLastVendorVersion ( cur , vendorId ) + 1 )
@ -253,10 +279,10 @@ def create_vendor(cur, row, marketId):
sql = " Update vendors set rating_vendor = %(rating_vendor)s , successfultransactions_vendor = %(successfultransactions_vendor)s , " \
sql = " Update vendors set rating_vendor = %(rating_vendor)s , successfultransactions_vendor = %(successfultransactions_vendor)s , " \
" image_vendor = %(image_vendor)s , dateinserted_vendor = %(dateinserted_vendor)s where vendor_id = %(vendorId)s "
" image_vendor = %(image_vendor)s , dateinserted_vendor = %(dateinserted_vendor)s where vendor_id = %(vendorId)s "
cur . execute ( sql , { ' rating_vendor ' : row [ 2 ] if row [ 2 ] != ' -1 ' else None ,
cur . execute ( sql , { ' rating_vendor ' : row [ 2 ] if row [ 2 ] != ' -1 ' else None ,
' successfultransactions_vendor ' : row [ 3 ] if row [ 3 ] != ' -1 ' else None ,
' image_vendor ' : row [ 21 ] if row [ 21 ] != ' -1 ' else None ,
' dateinserted_vendor ' : row [ 23 ] ,
' vendorId ' : vendorId } )
' successfultransactions_vendor ' : row [ 3 ] if row [ 3 ] != ' -1 ' else None ,
' image_vendor ' : imageId ,
' dateinserted_vendor ' : row [ 23 ] ,
' vendorId ' : vendorId } )
return vendorId
return vendorId
@ -275,7 +301,7 @@ def create_items(cur, row, marketId, vendorId):
if newItem :
if newItem :
# decode_decrypt_image_in_base64(row[20] )
imageId = create_image ( cur , row [ 20 ] , row [ 23 ] )
sql = " Insert into items (item_id, market_id, vendor_id, name_item, description_item, cve_item, ms_item, category_item, " \
sql = " Insert into items (item_id, market_id, vendor_id, name_item, description_item, cve_item, ms_item, category_item, " \
" views_item, reviews_item, rating_item, dateadded_item, btc_item, usd_item, euro_item, quantitysold_item, " \
" views_item, reviews_item, rating_item, dateadded_item, btc_item, usd_item, euro_item, quantitysold_item, " \
@ -300,7 +326,7 @@ def create_items(cur, row, marketId, vendorId):
row [ 17 ] if row [ 17 ] != ' -1 ' else None ,
row [ 17 ] if row [ 17 ] != ' -1 ' else None ,
row [ 18 ] if row [ 18 ] != ' -1 ' else None ,
row [ 18 ] if row [ 18 ] != ' -1 ' else None ,
row [ 19 ] if row [ 19 ] != ' -1 ' else None ,
row [ 19 ] if row [ 19 ] != ' -1 ' else None ,
row [ 20 ] if row [ 20 ] != ' -1 ' else None ,
imageId ,
hrefItem ,
hrefItem ,
row [ 23 ] ,
row [ 23 ] ,
row [ 23 ] ,
row [ 23 ] ,
@ -316,7 +342,9 @@ def create_items(cur, row, marketId, vendorId):
recset = cur . fetchall ( )
recset = cur . fetchall ( )
# decode_decrypt_image_in_base64(recset[0]['image_item'])
imageId = recset [ 0 ] [ ' image_item ' ]
if not is_same_image ( row [ 20 ] , recset [ 0 ] [ ' image_item ' ] ) ) :
imageId = create_image ( cur , row [ 20 ] , row [ 23 ] )
if ( str ( recset [ 0 ] [ ' vendor_id ' ] ) != str ( vendorId ) or
if ( str ( recset [ 0 ] [ ' vendor_id ' ] ) != str ( vendorId ) or
str ( recset [ 0 ] [ ' name_item ' ] ) != str ( row [ 4 ] if row [ 4 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' name_item ' ] ) != str ( row [ 4 ] if row [ 4 ] != ' -1 ' else None ) or
@ -335,7 +363,7 @@ def create_items(cur, row, marketId, vendorId):
str ( recset [ 0 ] [ ' quantityleft_item ' ] ) != str ( row [ 17 ] if row [ 17 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' quantityleft_item ' ] ) != str ( row [ 17 ] if row [ 17 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' shippedfrom_item ' ] ) != str ( row [ 18 ] if row [ 18 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' shippedfrom_item ' ] ) != str ( row [ 18 ] if row [ 18 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' shippedto_item ' ] ) != str ( row [ 19 ] if row [ 19 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' shippedto_item ' ] ) != str ( row [ 19 ] if row [ 19 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' image_item ' ] ) != str ( row [ 20 ] if row [ 20 ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' image_item ' ] ) != str ( imageId ) or
str ( recset [ 0 ] [ ' classification_item ' ] ) != str ( row [ 24 ] if row [ 24 ] != ' -1 ' else None ) ) :
str ( recset [ 0 ] [ ' classification_item ' ] ) != str ( row [ 24 ] if row [ 24 ] != ' -1 ' else None ) ) :
itemVersionId = int ( getLastItemVersion ( cur , itemId ) + 1 )
itemVersionId = int ( getLastItemVersion ( cur , itemId ) + 1 )
@ -398,7 +426,7 @@ def create_items(cur, row, marketId, vendorId):
' quantityleft_item ' : row [ 17 ] if row [ 17 ] != ' -1 ' else None ,
' quantityleft_item ' : row [ 17 ] if row [ 17 ] != ' -1 ' else None ,
' shippedfrom_item ' : row [ 18 ] if row [ 18 ] != ' -1 ' else None ,
' shippedfrom_item ' : row [ 18 ] if row [ 18 ] != ' -1 ' else None ,
' shippedto_item ' : row [ 19 ] if row [ 19 ] != ' -1 ' else None ,
' shippedto_item ' : row [ 19 ] if row [ 19 ] != ' -1 ' else None ,
' image_item ' : row [ 20 ] if row [ 20 ] != ' -1 ' else None ,
' image_item ' : imageId ,
' lastseen_item ' : row [ 23 ] ,
' lastseen_item ' : row [ 23 ] ,
' dateinserted_item ' : row [ 23 ] ,
' dateinserted_item ' : row [ 23 ] ,
' classification_item ' : row [ 24 ] if row [ 24 ] != ' -1 ' else None ,
' classification_item ' : row [ 24 ] if row [ 24 ] != ' -1 ' else None ,
@ -413,6 +441,74 @@ def create_items(cur, row, marketId, vendorId):
return itemId
return itemId
def is_same_image ( newBase64Image , oldImageId ) :
if newBase64Image == " -1 " and oldImageId == " -1 " :
return True
if newBase64Image == " -1 " : # and oldImageId != "-1"
return False
if oldImageId == " -1 " : # and newBase64Image != "-1"
return False
sql = " select * from market_images where image_id = %(imageId)s "
cur . execute ( sql , { ' imageId ' : oldImageId } )
recset = cur . fetchall ( )
decImage = decode_decrypt_image_in_base64 ( base64Image )
hash1 = generate_image_hash ( decImage )
hash2 = recset [ 0 ] [ ' hash_image ' ]
if hash1 == hash2 :
return True
features1 = json . loads ( extract_hidden_layer_output ( dec_string ) )
features2 = json . loads ( recset [ 0 ] [ ' resnet50_image ' ] )
similarity = 1 - distance . cosine ( features1 , features2 )
return similarity < 0.8
def create_image ( cur , base64Image , date ) :
if base64Image == " -1 " :
return None
imageId = verifyImage ( base64Image )
if not imageId :
imageId = int ( getLastImage ( cur ) + 1 )
newImage = True
else :
newImage = False
if newImage :
decImage = decode_decrypt_image_in_base64 ( base64Image )
hashImage = generate_image_hash ( decImage )
hloFeatures = extract_hidden_layer_output ( decImage )
keypoints , descriptors = extract_keypoints ( decImage )
sql = " Insert into market_images(image_id, hash_image, base64_image, " \
" resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \
" Values ( %s , %s , %s , %s , %s , %s , %s ) "
recset = [ imageId ,
hashImage ,
base64Image ,
hloFeatures ,
keypoints ,
descriptors ,
date ]
cur . execute ( sql , recset )
return imageId
def create_status ( cur , marketId , date , listings , descriptions , status ) :
def create_status ( cur , marketId , date , listings , descriptions , status ) :
date = datetime . strptime ( date , " % m %d % Y " )
date = datetime . strptime ( date , " % m %d % Y " )
@ -434,10 +530,20 @@ def create_status(cur, marketId, date, listings, descriptions, status):
recset = [ marketId , date , listings , descriptions , status , date_reference ]
recset = [ marketId , date , listings , descriptions , status , date_reference ]
cur . execute ( sql , recset )
cur . execute ( sql , recset )
def create_database ( cur , con ) :
def create_database ( cur , con ) :
try :
try :
sql = " create table market_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \
" resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \
" dateinserted_item timestamp(6) with time zone not null, " \
" constraint items_pk primary key (image_id)) "
cur . execute ( sql )
sql = " create unique index unique_image ON market_images USING btree (hash_image ASC NULLS LAST) "
cur . execute ( sql )
sql = " create table marketplaces(market_id integer not null, name_market character varying(255) not null, " \
sql = " create table marketplaces(market_id integer not null, name_market character varying(255) not null, " \
" url_market character varying(255) not null, dateinserted_market timestamp(6) with time zone not null, " \
" url_market character varying(255) not null, dateinserted_market timestamp(6) with time zone not null, " \
" constraint markets_pk primary key (market_id)) "
" constraint markets_pk primary key (market_id)) "
@ -456,7 +562,8 @@ def create_database(cur, con):
" varying(255) not null, rating_vendor character varying(255), successfultransactions_vendor integer " \
" varying(255) not null, rating_vendor character varying(255), successfultransactions_vendor integer " \
" null, image_vendor character varying(10000000) null, dateinserted_vendor timestamp(6) with time zone not null, " \
" null, image_vendor character varying(10000000) null, dateinserted_vendor timestamp(6) with time zone not null, " \
" constraint vendors_pk primary key (vendor_id), " \
" constraint vendors_pk primary key (vendor_id), " \
" constraint vendors_market_id_fk foreign key (market_id) references marketplaces (market_id)) "
" constraint vendors_market_id_fk foreign key (market_id) references marketplaces (market_id), " \
" constraint vendors_image_id_fkey foreign key (image_id) references market_images (image_id)) "
cur . execute ( sql )
cur . execute ( sql )
sql = " create unique index unique_vendor ON vendors USING btree (market_id ASC NULLS LAST, name_vendor ASC NULLS LAST) "
sql = " create unique index unique_vendor ON vendors USING btree (market_id ASC NULLS LAST, name_vendor ASC NULLS LAST) "
@ -465,9 +572,10 @@ def create_database(cur, con):
sql = " create table vendors_history(vendor_id integer not null, version_vendor integer not null, market_id integer not null, name_vendor " \
sql = " create table vendors_history(vendor_id integer not null, version_vendor integer not null, market_id integer not null, name_vendor " \
" character varying(255) not null, rating_vendor character varying(255), successfultransactions_vendor " \
" character varying(255) not null, rating_vendor character varying(255), successfultransactions_vendor " \
" integer null, image_vendor character varying(10000000) null, dateinserted_vendor timestamp(6) with time zone not null, " \
" integer null, image_vendor character varying(10000000) null, dateinserted_vendor timestamp(6) with time zone not null, " \
" constraint vendors_history_pk primary key (vendor_id, version_vendor), constraint vendors_history_vendor_id_fkey foreign key ( " \
" vendor_id) references vendors (vendor_id), constraint vendors_history_market_id_fkey foreign key ( " \
" market_id) references marketplaces (market_id)) "
" constraint vendors_history_pk primary key (vendor_id, version_vendor), " \
" constraint vendors_history_vendor_id_fkey foreign key (vendor_id) references vendors (vendor_id), " \
" constraint vendors_history_market_id_fkey foreign key (market_id) references marketplaces (market_id), " \
" constraint vendors_history_image_id_fkey foreign key (image_id) references market_images (image_id)) "
cur . execute ( sql )
cur . execute ( sql )
sql = " create table items(item_id integer not null, market_id integer not null, vendor_id integer not null, name_item character " \
sql = " create table items(item_id integer not null, market_id integer not null, vendor_id integer not null, name_item character " \
@ -477,11 +585,13 @@ def create_database(cur, con):
" character varying(25) null, btc_item character varying(255) null, usd_item character varying(255) " \
" character varying(25) null, btc_item character varying(255) null, usd_item character varying(255) " \
" null, euro_item character varying(255) null, quantitysold_item integer null, quantityleft_item " \
" null, euro_item character varying(255) null, quantitysold_item integer null, quantityleft_item " \
" character varying(255) null, shippedfrom_item character varying(255) null, shippedto_item character " \
" character varying(255) null, shippedfrom_item character varying(255) null, shippedto_item character " \
" varying(255) null, image_item character varying(10000000) null, href_item character varying(255) not null, " \
" varying(255) null, image_id integer null, href_item character varying(255) not null, " \
" lastseen_item timestamp(6) with time zone not null, dateinserted_item timestamp(6) with time zone not null, " \
" lastseen_item timestamp(6) with time zone not null, dateinserted_item timestamp(6) with time zone not null, " \
" classification_item double precision null, constraint items_pk primary key (item_id), constraint " \
" items_market_id_fkey foreign key (market_id) references marketplaces (market_id),constraint " \
" items_vendor_id_fkey foreign key (vendor_id) references vendors (vendor_id)) "
" classification_item double precision null, " \
" constraint items_pk primary key (item_id), " \
" constraint items_market_id_fkey foreign key (market_id) references marketplaces (market_id), " \
" constraint items_vendor_id_fkey foreign key (vendor_id) references vendors (vendor_id), " \
" constraint items_image_id_fkey foreign key (image_id) references market_images (image_id)) "
cur . execute ( sql )
cur . execute ( sql )
sql = " create unique index unique_item ON items USING btree (market_id ASC NULLS LAST, href_item ASC NULLS LAST) "
sql = " create unique index unique_item ON items USING btree (market_id ASC NULLS LAST, href_item ASC NULLS LAST) "
@ -494,12 +604,14 @@ def create_database(cur, con):
" character varying(25) null, btc_item character varying(255) null, usd_item character varying(255) " \
" character varying(25) null, btc_item character varying(255) null, usd_item character varying(255) " \
" null, euro_item character varying(255) null, quantitysold_item integer null, quantityleft_item " \
" null, euro_item character varying(255) null, quantitysold_item integer null, quantityleft_item " \
" character varying(255) null, shippedfrom_item character varying(255) null, shippedto_item character " \
" character varying(255) null, shippedfrom_item character varying(255) null, shippedto_item character " \
" varying(255) null, image_item character varying(10000000) null, href_item character varying(255) not null, " \
" varying(255) null, image_id integer null, href_item character varying(255) not null, " \
" lastseen_item timestamp(6) with time zone not null, dateinserted_item timestamp(6) with time zone not null, " \
" lastseen_item timestamp(6) with time zone not null, dateinserted_item timestamp(6) with time zone not null, " \
" classification_item double precision null, constraint items_history_pk primary key (item_id, version_item), " \
" classification_item double precision null, " \
" constraint items_history_pk primary key (item_id, version_item), " \
" constraint items_history_item_id_fkey foreign key (item_id) references items (item_id), " \
" constraint items_history_market_id_fkey foreign key (market_id) references marketplaces (market_id), " \
" constraint items_history_market_id_fkey foreign key (market_id) references marketplaces (market_id), " \
" constraint items_history_vendor_id_fkey foreign key (vendor_id) references vendors (vendor_id), " \
" constraint items_history_vendor_id_fkey foreign key (vendor_id) references vendors (vendor_id), " \
" constraint items_history_item_id_fkey foreign key (item_id) references items (item _id)) "
" constraint items_history_image_id_fkey foreign key (image_id) references market_images (image _id)) "
cur . execute ( sql )
cur . execute ( sql )
con . commit ( )
con . commit ( )
@ -513,4 +625,4 @@ def create_database(cur, con):
if ( trace . find ( " already exists " ) == - 1 ) :
if ( trace . find ( " already exists " ) == - 1 ) :
print ( " There was a problem during the database creation. " )
print ( " There was a problem during the database creation. " )
traceback . print_exc ( )
traceback . print_exc ( )
raise SystemExit
raise SystemExit