commit 525529193239bc4c2b85d68d1a4e477d9be4a781 Author: minecraftchest1@outlook.com Date: Tue Apr 29 13:12:05 2025 -0500 Initial Commit diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9caf84e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +phonenumbers==9.0.3 +vobject==0.9.9 +Flask==3.1.0 +requests==2.32.3 diff --git a/vcard-http.py b/vcard-http.py new file mode 100644 index 0000000..2c2e776 --- /dev/null +++ b/vcard-http.py @@ -0,0 +1,59 @@ +import argparse +import codecs +import json +import os +import phonenumbers +import requests +from requests.auth import HTTPDigestAuth +import urllib.request +import vobject + +from flask import Flask +app = Flask(__name__) + +contactsFile = '/tmp/cache.vcf' +#contactsFile = os.envrion['VCARD_HTTP_URL'] + +#def main(): +# parser = argparse.ArgumentParser(description='Get contacts phone number from csv file.') +# parser.add_argument('filename', help='path to csv file to read from') +# parser.add_argument('contact_name', help='contact name to retrieve number for') +# args = parser.parse_args() + +def updateCache(): + #with urllib.request.urlopen(os.envrion['VCARD_HTTP_URL']) + with open(contactsFile, "wb") as f: + f.write(requests.get( + 'https://nextcloud.minecraftchest1.us/remote.php/dav/addressbooks/users/9788b3b83b62eae1fb3646eb59b8385ca3180989fcfa515a632f481c567f2e7c/contacts/?export', + auth=('9788b3b83b62eae1fb3646eb59b8385ca3180989fcfa515a632f481c567f2e7c', 'txmHW-dwBFz-eqqf4-mbkct-rKL58')).content) + + +@app.route("/search/") +def index(contactName): + obj = vobject.readComponents(codecs.open(contactsFile, encoding='utf-8').read()) + contacts = [contact for contact in obj] + + outJson = [] + + for contact in contacts: + if contactName.lower() in contact.fn.value.lower(): + contactJson = {} + contactJson.update({'name': contact.fn.value}) + numbers = [] + + for tel in contact.contents["tel"]: + number = phonenumbers.parse(tel.value, "US") + numbers.append({"type": tel.params.get("TYPE")[0], "number": phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)}) + + contactJson.update({'numbers': numbers}) + outJson.append(contactJson) + + outJsonS = json.dumps(outJson) + return outJson + #print(outJsonS) + +@app.route("/cache") +def cache(): + updateCache() + with open(contactsFile, "r") as f: + return f.read() \ No newline at end of file