This is a silly little snippet has been being generated by the venerable ChatGPT website (I’m not the author). I could have written this by hand, but why waste time with the syntax when one could drink coffee while having AI do the tedious typing for us? Anyhow, the script isn’t really practical in production as the more efficient source to data mining is Active Directory or LDAPS, not some sort of local SQL silo. In case you’re wondering about the output of XML, this file can be hosted by a web service such as NGINX or Apache to make the it accessible via HTTP/S. Then, Zoom phones can download these to populate a ‘local directory’ within their menus automatically. If I explain any further, it would bore you. Just enjoy looking at the code as I do.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import mysql.connector import xml.etree.ElementTree as ET import hashlib import os import pickle xml_output = '/usr/local/bin/ZoomPhoneDirectory.xml' hash_file = '/usr/local/bin/ZoomPhoneDirectory.hash' host = "localhost" user = "banana" password = "monkey" database = "DBNAME" # Function to connect to the database and fetch data def fetch_data(): connection = mysql.connector.connect( host = host, user = user, password = password, database = database ) cursor = connection.cursor() cursor.execute( "SELECT last_name, first_name, DID, extension FROM assignments" ) result = cursor.fetchall() cursor.close() connection.close() return result # Function to check if a value is valid (not None, not 'none', not empty, and not a single space) def is_valid(value): return value and value.strip().lower() not in { 'none' } # Function to generate XML from fetched data def generate_xml(data): root = ET.Element( "FuseIPPhoneDirectory" ) for entry in data: last_name, first_name, DID, extension = entry # Check if the entries are valid if is_valid(last_name) and is_valid(first_name) and is_valid(DID) and is_valid(extension): directory_entry = ET.SubElement(root, "DirectoryEntry" ) name = ET.SubElement(directory_entry, "Name" ) name.text = f "{last_name}, {first_name}" telephone_did = ET.SubElement(directory_entry, "Telephone" ) telephone_did.text = DID telephone_ext = ET.SubElement(directory_entry, "Telephone" ) telephone_ext.text = extension tree = ET.ElementTree(root) tree.write(xml_output, encoding = 'utf-8' , xml_declaration = True ) # Function to compute the hash of the data def compute_hash(data): data_str = ''.join( str (row) for row in data) return hashlib.md5(data_str.encode()).hexdigest() # Function to check if the hash of the current data matches the previous hash def is_data_identical(current_hash): if os.path.exists(hash_file): with open (hash_file, 'rb' ) as f: previous_hash = pickle.load(f) return current_hash = = previous_hash return False # Function to save the current hash def save_hash(current_hash): with open (hash_file, 'wb' ) as f: pickle.dump(current_hash, f) if __name__ = = "__main__" : data = fetch_data() current_hash = compute_hash(data) if is_data_identical(current_hash): print ( "Data is identical to the previous run. Skipping XML generation." ) else : generate_xml(data) save_hash(current_hash) print (f "XML file '{xml_output}' generated successfully." ) |