import mysql.connector, phpserialize
deadhosts = ["clipwatching", "aparat", "playtube", "gounlimited"]
con = mysql.connector.connect(
host='localhost',
user='wordpress',
passwd='yourpass',
database='wordpress',
auth_plugin='mysql_native_password'
)
cur = con.cursor()
sqlfetch = "SELECT meta_id,meta_value from wp_postmeta where meta_key = \'repeatable_fields\' and meta_value REGEXP %s"
val = ("|".join(deadhosts),)
cur.execute(sqlfetch,val)
deadlinks = cur.fetchall()
sqldel = "DELETE FROM wp_postmeta WHERE meta_id = %s"
sqlupdate = "UPDATE wp_postmeta SET meta_value = %s WHERE meta_id = %s"
removedLinks = 0
tested = 0
for dead in list(deadlinks):
tested += 1
output = phpserialize.unserialize(dead[1].encode())
startLen = len(output)
for src in list(output):
if any(x in output[src][b'url'].decode() for x in deadhosts):
removedLinks += 1
del output[src]
if len(output) < startLen:
if len(output) != 0:
nuoutput = dict()
nulinkidx = 0
for src in output:
nuoutput[nulinkidx] = output[src]
nulinkidx += 1
vals = (phpserialize.serialize(nuoutput).decode(),dead[0])
cur.execute(sqlupdate,vals)
con.commit()
else:
val = (dead[0],)
cur.execute(sqldel,val)
con.commit()
print(removedLinks," dead links removed")