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() updateCache()