@@ -56,18 +56,23 @@ def getImage(soup, url):
56
56
res = ogImg or twitterImg or metaImg or img
57
57
res = res .get ("content" , None ) or res .get_text () or res .get ("src" , None )
58
58
59
+ count = 0
60
+ for i in range (0 , len (res )):
61
+ if (res [i ] == "." or res [i ] == "/" ):
62
+ count += 1
63
+ else :
64
+ break
65
+ res = res [count ::]
59
66
if ((not res == None ) and ((not "https://" in res ) or (not "https://" in res ))):
60
- res .replace ("." , "" )
61
- if (not res [0 ] == "/" ):
62
- res = "/" + res
63
- res = url + res
67
+ res = url + "/" + res
64
68
if (res == None ):
65
69
res = "Not available"
66
70
67
71
return res
68
72
69
-
70
73
# print dictionary
74
+
75
+
71
76
def printData (data ):
72
77
print ("\n Title : " , data ["title" ])
73
78
print ("Description : " , data ["description" ])
@@ -95,13 +100,17 @@ def printData(data):
95
100
db = {}
96
101
# create file if it doesn't exist
97
102
if not os .path .exists ('Link-Preview/db.json' ):
98
- f = open ('Link-Preview/db.json' , 'w' )
103
+ f = open ('Link-Preview/db.json' , "w" )
99
104
f .write ("{}" )
100
105
f .close ()
101
106
102
107
# read db
103
- with open ('Link-Preview/db.json' , 'r' ) as file :
104
- db = json .loads (file .read ())
108
+ with open ('Link-Preview/db.json' , 'r+' ) as file :
109
+ data = file .read ()
110
+ if (len (data ) == 0 ):
111
+ data = "{}"
112
+ file .write (data )
113
+ db = json .loads (data )
105
114
106
115
# check if it exists
107
116
if (url in db ):
0 commit comments