def configure(old_profile=None): if not old_profile: old_profile = {'user': '', 'password': ''} profile = {'user': '', 'password': ''} print "Please enter your Facebook user so" print "I can communicate with Facebook" success = False while not success: profile['user'] = user_input("Facebook", default=old_profile['user']) pw = user_input(password=True) if profile['user']: print("\nI will now connect to Facebook...") try: fbchat.Client( profile['user'], pw ) success = True except: print("\nYour credentials are wrong. Please check them") continue else: print("\nYou will not be able to use Facebook") success = True return {} profile['password'] = base64.b64encode(pw) return profile
def get_reminders(old_profile, api): rem = api.reminders all_lists = rem.lists.keys() print "\nGreat! I can also help you with reminders. These are the" print "reminder lists you have. Which list do you want as your" print "primary list?" for i in range(len(all_lists)): print "<%d> %s" % (i+1, all_lists[i]) try: list_name = old_profile['reminders'][0] default = int(all_lists.index(list_name))+1 except: default = '' primary = [all_lists[int(user_input("Primary list", default=default))-1]] try: default = ','.join( str(int(all_lists.index(old_profile['reminders'][i]))+1) for i in range(1, len(old_profile['reminders'])) ) except: default = '' print "Do you want me to access other lists? If so, enter their" print "indices seperated with commas." dev_string = user_input("Other lists", default=default) return primary + [all_lists[int(i)-1] for i in dev_string.split(",")]
def add_light(name="", on_endpoint="", off_endpoint=""): name = user_input("Light's name", default=name).lower() on_endpoint = user_input("Light's ON endpoint", default=on_endpoint) off_endpoint = user_input("Light's OFF endpoint", default=off_endpoint) if name and on_endpoint and off_endpoint: profile["lights"].append({"name": name, "on_endpoint": on_endpoint, "off_endpoint": off_endpoint}) return True else: return False
def configure(old_profile=None): if old_profile is None: old_profile = {"name": "", "id": ""} weather_client = yweather.Client() print "For weather requests please enter your location" location = user_input("Location", default=old_profile["name"]) while location: woeid = weather_client.fetch_woeid(location) if not woeid: print ("Weather not found. Please try another location.") location = raw_input("Location: ") continue weather = weather_client.fetch_weather(woeid) print "I have found %s, %s in %s (%s, %s)" % ( weather["location"]["city"], weather["location"]["region"], weather["location"]["country"], weather["geo"]["lat"], weather["geo"]["long"], ) if yn_input("Is this correct", default=True): return {"name": location, "id": woeid} else: location = raw_input("Location: ") return {"name": "", "id": ""}
def get_apiKeys(old_profile): # We need to find all invocations of profile['keys'] all_keys = [] for dirName, subdirList, fileList in os.walk(fullpath("")): for fname in fileList: filename, file_extension = os.path.splitext(fname) if file_extension == ".py": path = os.path.join(dirName, fname) lparenthesis = "\[['\"]" # matches [' and [" rparenthesis = "['\"]\]" # matches '] and "] with open(path) as fp: all_keys += re.findall( "profile" + lparenthesis + "keys" + rparenthesis + lparenthesis + "(.+)" + rparenthesis, fp.read() ) profile = {} print "I have compiled a list of all API keys you will need to enter." for key in all_keys: if key in old_profile: default = old_profile[key] else: default = "" profile[key] = user_input( "Please enter the API key for " + key, default=default ) return profile
def configure(old_profile=None): if not old_profile: old_profile = { 'cred': {'id': '', 'password': ''}, 'device': {}, 'reminders': [] } profile = { 'cred': {'id': '', 'password': ''}, 'device': {}, 'reminders': [] } print("\nPlease enter your Apple ID so I can communicate with iCloud") success = False while not success: profile['cred']['id'] = user_input( "Apple ID", default=old_profile['cred']['id'] ) pw = user_input(password=True) if profile['cred']['id']: print("\nI will now connect to iCloud...") try: api = PyiCloudService( profile['cred']['id'], pw ) success = True except pyicloud.exceptions.PyiCloudFailedLoginException: print("\nYour credentials are wrong. Please check them") continue else: print("\nYou will not be able to use iCloud") success = True return {} profile['cred']['password'] = base64.b64encode(pw) profile['device'] = get_devices(old_profile, api) profile['reminders'] = get_reminders(old_profile, api) return profile
def queryAccount(user='', server=''): user = user_input("Username", default=user) pw = user_input(password=True) server = user_input("Server", default=server) if user == '' or pw == '' or server == '': return None obj = imaplib.IMAP4_SSL(server) try: obj.login(user, pw) obj.logout() except: return None return { 'user': user, 'password': base64.b64encode(pw), 'server': server }
def get_timezone(old_profile): print("\nPlease enter a timezone from the list located in the TZ* " + "column at http://en.wikipedia.org/wiki/" + "List_of_tz_database_time_zones, or none at all.") tz = user_input("Timezone", default=old_profile['timezone']) while tz: try: timezone(tz) return tz except: print("Not a valid timezone. Try again.") tz = raw_input("Timezone: ")
def queryFeed(URL='', entries=-1): url = user_input("URL", default=URL) entries = user_input( "How many items do you want from this feed", default=str(entries) if entries is not -1 else '' ) try: entries = int(entries) except: return None try: d = feedparser.parse(url) entries = d['entries'] except: return None return { 'entries': entries, 'URL': url }
def configure(old_profile=None): profile = {"lights": [], "status_endpoint": "", "thermometer_key": "", "lock_key": ""} def add_light(name="", on_endpoint="", off_endpoint=""): name = user_input("Light's name", default=name).lower() on_endpoint = user_input("Light's ON endpoint", default=on_endpoint) off_endpoint = user_input("Light's OFF endpoint", default=off_endpoint) if name and on_endpoint and off_endpoint: profile["lights"].append({"name": name, "on_endpoint": on_endpoint, "off_endpoint": off_endpoint}) return True else: return False print "I can control your home." if old_profile is None: old_profile = {"lights": [], "status_endpoint": "", "thermometer_key": "", "lock_key": ""} else: print "I have found light endpoints." if yn_input("Do you want to change them", default=False): for light in old_profile["lights"]: print "%s with endpoints (%s, %s)" % (light["name"], light["on_endpoint"], light["off_endpoint"]) add_light(light["name"], light["on_endpoint"], light["off_endpoint"]) else: profile["lights"] = old_profile["lights"] while True: if yn_input("Do you want to add a light", default=True): if not add_light(): break else: break print "To get status information I need a JSON formatted URL endpoint" profile["status_endpoint"] = user_input("Status endpoint", default=old_profile["status_endpoint"]) if profile["status_endpoint"]: profile["thermometer_key"] = user_input("Thermometer key", default=old_profile["thermometer_key"]) profile["lock_key"] = user_input("Lock key", default=old_profile["lock_key"]) return profile
def get_devices(old_profile, api): def find_name_for_key(name): for n, k in old_profile['device'].iteritems(): if k == name: return n return '' print "I will now list you your devices. Please select which" print "devices you want to use for Find My iPhone" devices = api.devices keys = devices.keys() for i in range(len(keys)): print "<%d> %s" % ( i+1, unicode(devices[keys[i]]).encode('utf-8') ) print "Please enter the numbers of the devices you want me to" print "use. Seperate multiple numbers with commas" try: default = ",".join( str(keys.index(i)+1) for i in old_profile['device'].values() ) except ValueError: default = "" profile_devives = {} dev_string = user_input("Devices", default=default) keys = [keys[int(i)-1] for i in dev_string.split(",")] for key in keys: print "How to you want me to refer to", print unicode(devices[key]).encode('utf-8')+'' n = user_input('', default=find_name_for_key(key)) profile_devives[n] = key return profile_devives
def get_mail(old_profile=None, first_name="", last_name=""): if not old_profile: old_profile = { 'address': '', 'user': '', 'server': '', 'password': '', 'recipient': '' } print "IRIS uses your Gmail to send notifications" success = False mp = {} mp['address'] = user_input("Email address", default=old_profile['address']) default_user = mp['address'] if re.findall("(g(oogle)?mail)", mp['address']): user, host = tuple(mp['address'].split('@')) sug = user + "+IRIS@" + host print """You have a gmail account. I can change your address to %s. This won't stop you from receiving emails but allow you to filter for my notifications easily!""" % sug if yn_input(query="Do you want me to do that", default=False): mp['address'] = sug while not success: mp['user'] = user_input( "SMTP User", default=old_profile['user'] or default_user ) pw = user_input(password=True) mp['server'] = user_input( "SMTP Server", default=old_profile['server'] or "smtp.gmail.com" ) if mp['user']: print("\nI will now connect to SMTP...") try: session = app_utils.smtp_login(mp['server'], mp['user'], pw) session.quit() success = True except app_utils.SMTPAuthenticationError: print("\nYour credentials are wrong. Please check them") else: print("\nYou will not be able to use SMTP") success = True return {} mp['password'] = base64.b64encode(pw) if first_name and last_name: mp['recipient'] = "%s %s <%s>" % (first_name, last_name, mp['address']) else: mp['recipient'] = mp['address'] if yn_input("Do you want me to send a test mail", default=False): app_utils.emailUser( {'mail': mp}, subject="IRIS TEST MAIL", body="Test" ) return mp
def simple_request(var, cleanVar): input = user_input(cleanVar, default=old_profile[var]) if input: profile[var] = input