Exemple #1
0
from locationsharinglib import Service
cookies_file = 'location_sharing.txt'
google_email = '*****@*****.**'
service = Service(cookies_file=cookies_file,
                  authenticating_account=google_email)
# print(service.get_shared_people())
for person in service.get_shared_people():
    # print(person)
    print("ID: {}".format(person.id))
    print("Address: {}".format(person.address))
    print("Full Name: {}".format(person.full_name))
    print("Last Seen: {}".format(person.datetime))
    print("Nickname: {}".format(person.nickname))
    print("Charging: {}".format(person.charging))
    print("Battery: {}".format(person.battery_level))
    print("Lat: {}, Lon: {}".format(person.latitude, person.longitude))
    print("Picture: {}".format(person.picture_url))
Exemple #2
0
class FrontEnd:
    def __init__(self, username, cookie):
        try:
            self.service = Service(cookie, username)
        except InvalidCookies:
            print("Invalid cookie.")
            raise InvalidCookies
        print("Authenticated.\n")
        print(list(self.service.get_all_people()))
        self.now = None
        self.you = None
        self.people = []
        # dictionary of people by id
        self.person_d = {}
        self.person_n = {}
        self.load()
        self.auto_update()

    def load(self):
        if not os.path.isfile(FNAME):
            return
        for line in open(FNAME, 'r'):
            parts = line.split(",")
            if len(parts) != 3:
                continue
            pid, lat, lon = parts
            home = (float(lat), float(lon))
            self.person_d[pid] = home
        print("Loaded.")

    def auto_update(self):
        self.update()
        # update every N seconds (on a thread)
        self.t = Timer(POLLING_DURATION, self.auto_update)
        self.t.start()

    def update(self):
        self.now = datetime.datetime.now()
        if PRINT_UPDATES:
            print("Updating... (%s)\n> " % self.now.strftime("%H:%M:%S"))
        you = self.service.get_authenticated_person()

        if self.person_d.get(you.id) is None:
            self.you = You(you)
        else:
            # both 'homes' and people stored in the person dictionary
            if type(self.person_d[you.id]) == tuple:
                self.you = You(you, home=self.person_d[you.id])
            else:
                self.you = self.person_d[you.id]
                self.you.update(you)
        self.person_d[you.id] = self.you

        self.people = []
        for p in self.service.get_shared_people():
            new_p = None
            if p.id in self.person_d.keys():
                # both 'homes' and people stored in the person dictionary
                if type(self.person_d[p.id]) == tuple:
                    new_p = Person(p, you=self.you, home=self.person_d[p.id])
                else:
                    new_p = self.person_d[p.id]
                    new_p.update(p, self.you)
            else:
                new_p = Person(p, you=self.you)
            self.person_d[p.id] = new_p
            self.people.append(new_p)
            self.person_n[new_p.nickname.lower()] = new_p

        # sort by furthest distance
        self.people.sort(key=lambda p: p.distance)

    def print_all(self):
        self.you.print()
        print("")
        for i, p in enumerate(self.people):
            print("%2d. " % (i + 1), end="")
            p.print()
        print("(Refreshed at %s.)" % self.now.strftime('%Y-%m-%d %H:%M:%S'))

    def who_find(self):
        if len(self.people):
            return "I've got no-one to find."
        res = "I can find "
        lst = [str(p.nickname) for p in self.people]
        if len(lst) == 1:
            res += lst[0] + "."
        else:
            res += ", ".join(lst[:-1]) + " and " + lst[-1] + "."

    def who_home(self):
        people_at_home = []
        for i, p in enumerate(self.people):
            if p.at_home:
                people_at_home.append(p)

        if len(people_at_home) == 0:
            if self.you.at_home:
                return "You are at home by yourself."
            else:
                return "No-one is home."
        else:
            res = ""
            if self.you.at_home:
                res += "You are at home with "
            lst = [str(p.nickname) for p in people_at_home]
            if len(lst) == 1:
                res += lst[0] + "."
            else:
                res += ", ".join(lst[:-1]) + " and " + lst[-1] + "."
            return res

    def who_out(self):
        people_out = []
        for i, p in enumerate(self.people):
            if not p.at_home:
                people_out.append(p)

        if len(people_out) == 0:
            if self.you.at_home:
                return "Everyone is home."
            else:
                return "Everyone but you is home."
        else:
            res = ""
            if not self.you.at_home:
                res += "You, "
            lst = [str(p.nickname) for p in people_out]
            if len(lst) == 1:
                res += lst[0]
            else:
                res += ", ".join(lst[:-1]) + " and " + lst[-1]
            return res + " are out."

    def set_home(self, name):
        if name == 'me':
            self.you.set_home()
            self.save()
            return "Set your home to where you are."
        else:
            p = self.person_n.get(name.lower())
            if p is None:
                return "Could not find {}.".format(name)
            else:
                p.set_home()
                self.save()
                return "Set home of {}.".format(p.nickname)

    def save(self):
        f = open(FNAME, "w")
        for p in self.person_d.values():
            x = p.serialise()
            if x is not None:
                f.write(x)

    def whereis(self, name):
        if name == 'me':
            return self.you.get_info_str()
        else:
            p = self.person_n.get(name.lower())
            if p is None:
                return "Could not find where \"{}\" is.".format(name)
            else:
                return p.get_info_str()