Last active
April 28, 2019 14:48
-
-
Save sparky3387/53eb486576f6ff91959c3aab9a5093ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import codecs | |
import sys | |
UTF8Writer = codecs.getwriter('utf8') | |
sys.stdout = UTF8Writer(sys.stdout) | |
import tempfile | |
import mysql.connector | |
from smb.SMBConnection import SMBConnection | |
from smb.smb_structs import OperationFailure | |
import argparse | |
from urlparse import urlparse | |
import os.path | |
parser = argparse.ArgumentParser("clean-kodidb") | |
parser.add_argument('--mysql-host', required=True) | |
parser.add_argument('--mysql-user', required=True) | |
parser.add_argument('--mysql-pass', required=True) | |
parser.add_argument('--kodi-dbname', required=True) | |
parser.add_argument('--dummy-run') | |
args = vars(parser.parse_args()) | |
mydb = mysql.connector.connect( | |
host=args["mysql_host"], | |
user=args["mysql_user"], | |
passwd=args["mysql_pass"], | |
database=args["kodi_dbname"] | |
) | |
mycursor = mydb.cursor() | |
mycursor.execute("select idPath,strPath from MyVideos116.path where strPath like 'smb%'") | |
myresult = mycursor.fetchall() | |
saved_hosts = {} | |
for url in myresult: | |
parsed_uri = urlparse(url[1]) | |
if parsed_uri.scheme == "smb": | |
if parsed_uri.netloc not in saved_hosts: | |
print("New SMB host discovered: smb://"+parsed_uri.netloc+", please enter login details, these are saved for the session") | |
user = raw_input("Enter a username (or enter if not required): ") | |
password = raw_input("Enter a password (or enter if not required): ") | |
saved_hosts[parsed_uri.netloc] = [user,password] | |
conn = SMBConnection(user, password, "clear-kodidb", "", use_ntlm_v2 = True) | |
assert conn.connect(parsed_uri.netloc, 139) | |
path = parsed_uri.path | |
sections=[]; temp = ""; | |
while path != '/': | |
temp = os.path.split(path) | |
path = temp[0] | |
sections.insert(0,temp[1]) | |
sections.pop() | |
rempath = "" | |
for item in sections[1:]: | |
rempath += "/"+item | |
try: | |
conn.listPath(sections[0],rempath) | |
except OperationFailure: | |
if args["dummy_run"] == "no": | |
print "Deleting "+url[1]+" from the database" | |
mycursor.execute("delete from MyVideos116.path where idPath="+str(url[0])) | |
mydb.commit() | |
else: | |
print(url[1]+" is in the mysql database, but is not found in the samba share") | |
if args["dummy_run"] is None: | |
print('Not deleting any rows in database as "--dummy-run no" has not been specified in arguments') | |
#print(parsed_uri) | |
mycursor.close() | |
mydb.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment