@ -4,6 +4,7 @@ import psycopg2
import traceback
from Forums.Utilities.utilities import *
from dateutil.relativedelta import relativedelta , FR
from scipy.spatial import distance
def connectDataBase ( ) :
@ -113,6 +114,28 @@ def verifyUser(cur, nameUser, forumId):
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 getLastForum ( cur ) :
try :
@ -169,6 +192,7 @@ def getLastUser(cur):
trace = traceback . format_exc ( )
print ( trace )
def getLastUserVersion ( cur , userId ) :
try :
@ -187,6 +211,7 @@ def getLastUserVersion(cur, userId):
trace = traceback . format_exc ( )
print ( trace )
def getLastTopicVersion ( cur , topicId ) :
try :
@ -205,6 +230,7 @@ def getLastTopicVersion(cur, topicId):
trace = traceback . format_exc ( )
print ( trace )
def getLastPostVersion ( cur , postId ) :
try :
@ -223,6 +249,7 @@ def getLastPostVersion(cur, postId):
trace = traceback . format_exc ( )
print ( trace )
def getLastPost ( cur ) :
try :
@ -241,6 +268,26 @@ def getLastPost(cur):
trace = traceback . format_exc ( )
print ( trace )
def getLastImage ( cur ) :
try :
cur . execute ( " select image_id from market_images order by image_id desc limit 1 " )
recset = cur . fetchall ( )
if recset :
return recset [ 0 ] [ ' image_id ' ]
else :
return 0
except :
trace = traceback . format_exc ( )
print ( trace )
def create_forum ( cur , row , url ) :
forumId = verifyForum ( cur , row [ 0 ] )
@ -348,8 +395,12 @@ def create_author(cur, row, forumId):
userId = verifyUser ( cur , row [ 2 ] , forumId )
if not userId :
userId = int ( getLastUser ( cur ) + 1 )
newUser = True
else :
newUser = False
if newUser :
sql = " Insert into users (user_id, forum_id, name_user, status_user, reputation_user, interest_user, " \
" signature_user, image_user, dateinserted_user) Values ( %s , %s , %s , %s , %s , %s , %s , %s , %s ) "
@ -375,19 +426,21 @@ def create_user(cur, row, forumId, index):
if newUser :
sql = " Insert into users (user_id, forum_id, name_user, status_user, reputation_user, interest_user, " \
" signature_user, image_user, dateinserted_user) Values ( %s , %s , %s , %s , %s , %s , %s , %s , %s ) "
imageId = create_image ( cur , row [ 9 ] [ index ] , row [ 8 ] )
recset = [ userId , forumId ,
row [ 10 ] [ index ] ,
row [ 11 ] [ index ] if row [ 11 ] [ index ] != ' -1 ' else None ,
row [ 12 ] [ index ] if row [ 12 ] [ index ] != ' -1 ' else None ,
row [ 13 ] [ index ] if row [ 13 ] [ index ] != ' -1 ' else None ,
row [ 14 ] [ index ] if row [ 14 ] [ index ] != ' -1 ' else None ,
row [ 9 ] [ index ] if row [ 9 ] [ index ] != ' -1 ' else None ,
row [ 8 ] ]
sql = " Insert into users (user_id, forum_id, name_user, status_user, reputation_user, interest_user, " \
" signature_user, image_user, dateinserted_user) Values ( %s , %s , %s , %s , %s , %s , %s , %s , %s ) "
cur . execute ( sql , recset )
recset = [ userId , forumId ,
row [ 10 ] [ index ] ,
row [ 11 ] [ index ] if row [ 11 ] [ index ] != ' -1 ' else None ,
row [ 12 ] [ index ] if row [ 12 ] [ index ] != ' -1 ' else None ,
row [ 13 ] [ index ] if row [ 13 ] [ index ] != ' -1 ' else None ,
row [ 14 ] [ index ] if row [ 14 ] [ index ] != ' -1 ' else None ,
imageId ,
row [ 8 ] ]
cur . execute ( sql , recset )
else :
@ -397,13 +450,15 @@ def create_user(cur, row, forumId, index):
recset = cur . fetchall ( )
#decode_decrypt_image_in_base64(recset[0]['image_user'])
imageId = recset [ 0 ] [ ' image_user ' ]
if not is_same_image ( row [ 9 ] [ index ] , recset [ 0 ] [ ' image_user ' ] ) ) :
imageId = create_image ( cur , row [ 9 ] [ index ] , row [ 8 ] )
if ( str ( recset [ 0 ] [ ' status_user ' ] ) != str ( row [ 11 ] [ index ] if row [ 11 ] [ index ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' reputation_user ' ] ) != str ( row [ 12 ] [ index ] if row [ 12 ] [ index ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' interest_user ' ] ) != str ( row [ 13 ] [ index ] if row [ 13 ] [ index ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' signature_user ' ] ) != str ( row [ 14 ] [ index ] if row [ 14 ] [ index ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' image_user ' ] ) != str ( row [ 9 ] [ index ] if row [ 9 ] [ index ] != ' -1 ' else None ) ) : # there was a change in the user information
str ( recset [ 0 ] [ ' image_user ' ] ) != str ( imageId ) ) : # there was a change in the user information
if ( str ( recset [ 0 ] [ ' status_user ' ] ) != ' Nan ' or
str ( recset [ 0 ] [ ' reputation_user ' ] ) != ' Nan ' or
@ -434,8 +489,8 @@ def create_user(cur, row, forumId, index):
' reputation_user ' : row [ 12 ] [ index ] if row [ 12 ] [ index ] != ' -1 ' else None ,
' interest_user ' : row [ 13 ] [ index ] if row [ 13 ] [ index ] != ' -1 ' else None ,
' signature_user ' : row [ 14 ] [ index ] if row [ 14 ] [ index ] != ' -1 ' else None ,
' image_user ' : row [ 9 ] [ index ] if row [ 9 ] [ index ] != ' -1 ' else None ,
' dateinserted_user ' : row [ 8 ] if row [ 8 ] != ' -1 ' else None ,
' image_user ' : imageId ,
' dateinserted_user ' : row [ 8 ] ,
' userId ' : userId } )
return userId
@ -452,8 +507,14 @@ def create_posts(cur, row, forumId, topicId):
postId = verifyPost ( cur , topicId , userId , row [ 17 ] [ i ] )
if not postId :
postId = int ( getLastPost ( cur ) + 1 )
newPost = True
else :
newPost = False
if newPost :
imageId = create_image ( cur , row [ 18 ] [ i ] , row [ 8 ] )
sql = " Insert into posts (post_id, topic_id, user_id, content_post, feedback_post, image_post, " \
" dateadded_post, dateinserted_post) Values ( %s , %s , %s , %s , %s , %s , %s , %s ) "
@ -461,8 +522,8 @@ def create_posts(cur, row, forumId, topicId):
recset = [ postId , topicId , userId ,
row [ 15 ] [ i ] if row [ 15 ] [ i ] != ' -1 ' else None ,
row [ 16 ] [ i ] if row [ 16 ] [ i ] != ' -1 ' else None ,
row [ 18 ] [ i ] if row [ 18 ] [ i ] != ' -1 ' else None ,
row [ 17 ] [ i ] if row [ 17 ] [ i ] != ' -1 ' else None ,
imageId ,
row [ 17 ] [ i ] ,
row [ 8 ] ]
cur . execute ( sql , recset )
@ -475,11 +536,13 @@ def create_posts(cur, row, forumId, topicId):
recset = cur . fetchall ( )
imageId = recset [ 0 ] [ ' image_post ' ]
if not is_same_image ( row [ 18 ] [ i ] , recset [ 0 ] [ ' image_post ' ] ) ) :
imageId = create_image ( cur , row [ 18 ] [ i ] , row [ 8 ] )
if ( str ( recset [ 0 ] [ ' content_post ' ] ) != str ( row [ 15 ] [ i ] ) or
str ( recset [ 0 ] [ ' feedback_post ' ] ) != str ( row [ 16 ] [ i ] if row [ 16 ] [ i ] != ' -1 ' else None ) or
str ( recset [ 0 ] [ ' image_post ' ] ) != str ( row [ 18 ] [ i ] if row [ 18 ] [ i ] != ' -1 ' else None ) ) : # there was a change in the post information
#decode_decrypt_image_in_base64(recset[0]['image_post'])
str ( recset [ 0 ] [ ' image_post ' ] ) != str ( imageId ) ) : # there was a change in the post information
postVersionId = int ( getLastPostVersion ( cur , postId ) + 1 )
@ -499,10 +562,78 @@ def create_posts(cur, row, forumId, topicId):
" image_post = %(image_post)s , dateinserted_post = %(dateinserted_post)s where post_id = %(postId)s "
cur . execute ( sql , { ' content_post ' : row [ 15 ] [ i ] if row [ 15 ] [ i ] != ' -1 ' else None ,
' feedback_post ' : row [ 16 ] [ i ] if row [ 16 ] [ i ] != ' -1 ' else None ,
' image_post ' : row [ 18 ] [ i ] if row [ 18 ] [ i ] != ' -1 ' else None ,
' image_post ' : imageId ,
' dateinserted_post ' : row [ 8 ] ,
' postId ' : postId } )
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 , forumId , date , listings , descriptions , status ) :
date = datetime . strptime ( date , " % m %d % Y " )
@ -529,6 +660,15 @@ def create_database(cur, con):
try :
sql = " create table forum_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 forum_images USING btree (hash_image ASC NULLS LAST) "
cur . execute ( sql )
sql = " create table forums (forum_id integer NOT NULL, name_forum character varying(255) NOT NULL, url_forum " \
" character varying(255) NOT null, dateinserted_forum timestamp(6) with time zone NOT NULL, " \
" constraint forums_pk primary key (forum_id)) "
@ -546,9 +686,10 @@ def create_database(cur, con):
sql = " create table users (user_id integer NOT NULL, forum_id integer NOT NULL, name_user character varying( " \
" 255) NOT NULL, status_user character varying(255) null, reputation_user character varying(255) null, " \
" interest_user character varying(5000) null, signature_user character varying(1000) null, " \
" image_user character varying(10000000) null, dateinserted_user timestamp(6) with time zone NOT NULL, " \
" image_user integer null, dateinserted_user timestamp(6) with time zone NOT NULL, " \
" constraint users_pk primary key (user_id), " \
" constraint users_forum_id_fk foreign key (forum_id) references forums (forum_id)) "
" constraint users_forum_id_fk foreign key (forum_id) references forums (forum_id), " \
" constraint users_image_id_fk foreign key (image_user) references forum_images (image_id)) "
cur . execute ( sql )
sql = " create unique index unique_user ON users USING btree (forum_id ASC NULLS LAST, name_user ASC NULLS LAST) "
@ -557,11 +698,12 @@ def create_database(cur, con):
sql = " create table users_history(user_id integer NOT NULL, version_user integer not null, forum_id integer NOT NULL, " \
" name_user character varying(255) NOT NULL, status_user character varying(255) null, " \
" reputation_user character varying(255) null, interest_user character varying(5000) null, " \
" signature_user character varying(1000) null, image_user character varying(10000000) null, " \
" signature_user character varying(1000) null, image_user integer null, " \
" dateinserted_user timestamp(6) with time zone NOT NULL, " \
" constraint users_history_pk primary key (user_id, version_user), " \
" constraint users_history_user_id_fk foreign key (user_id) references users (user_id), " \
" constraint users_history_forum_id_fk foreign key (forum_id) references forums (forum_id)) "
" constraint users_history_forum_id_fk foreign key (forum_id) references forums (forum_id), " \
" constraint users_history_image_id_fk foreign key (image_user) references forum_images (image_id)) "
cur . execute ( sql )
sql = " create table topics(topic_id integer NOT NULL, forum_id integer NOT NULL, author_id integer NOT NULL, " \
@ -589,11 +731,12 @@ def create_database(cur, con):
sql = " create table posts(post_id integer NOT NULL, topic_id integer NOT NULL, " \
" user_id integer NOT NULL, content_post character varying(100000) NOT null, feedback_post integer null, " \
" image_post character varying(10000000) null, dateadded_post timestamp(6) with time zone NOT NULL, " \
" image_post integer null, dateadded_post timestamp(6) with time zone NOT NULL, " \
" dateinserted_post timestamp(6) with time zone NOT NULL, " \
" constraint posts_pk primary key (post_id), " \
" constraint posts_user_id_fk foreign key (user_id) references users (user_id), " \
" constraint posts_topic_id_fk foreign key (topic_id) references topics (topic_id)) "
" constraint posts_topic_id_fk foreign key (topic_id) references topics (topic_id), " \
" constraint posts_image_id_fk foreign key (image_post) references forum_images (image_id)) "
cur . execute ( sql )
sql = " create unique index unique_post ON posts USING btree (topic_id ASC NULLS LAST, user_id ASC NULLS LAST, " \
@ -602,12 +745,13 @@ def create_database(cur, con):
sql = " create table posts_history(post_id integer NOT NULL, version_post integer not null, topic_id integer NOT NULL, " \
" user_id integer NOT NULL, content_post character varying(100000) NOT null, feedback_post integer null, " \
" image_post character varying(10000000) null, dateadded_post timestamp(6) with time zone NOT NULL, " \
" image_post integer null, dateadded_post timestamp(6) with time zone NOT NULL, " \
" dateinserted_post timestamp(6) with time zone NOT NULL, " \
" constraint posts_history_pk primary key (post_id, version_post), " \
" constraint posts_history_user_id_fk foreign key (user_id) references users (user_id), " \
" constraint posts_history_topic_id_fk foreign key (topic_id) references topics (topic_id), " \
" constraint posts_history_post_id_fk foreign key (post_id) references posts (post_id)) "
" constraint posts_history_post_id_fk foreign key (post_id) references posts (post_id), " \
" constraint posts_history_image_id_fk foreign key (image_post) references forum_images (image_id)) "
cur . execute ( sql )
con . commit ( )
@ -621,4 +765,4 @@ def create_database(cur, con):
if ( trace . find ( " already exists " ) == - 1 ) :
print ( " There was a problem during the database creation. " )
traceback . print_exc ( )
raise SystemExit
raise SystemExit