Exemple #1
0
    def __update_relationships(self, principle):
        if self.user_id is None:
            return False

        principle_to_url_prototype = {
            'followed_by': selfFollowedBy,
            'follows': selfFollows
        }

        if principle not in principle_to_url_prototype.keys():
            return

        if get_self_id() != self.user_id:
            # relationship info accessible for access-token owner only
            return

        print "Updating relationship:", principle

        ids_to_names_dictionary = dict()
        url = get_url('', get_access_token(), principle_to_url_prototype[principle])
        while url is not None:
            raw_data = urllib2.urlopen(url).read()
            json_data = json.loads(raw_data)
            users = json_data['data']
            list_of_dictionaries = [{entry['id']: entry['username']} for entry in users]
            for d in list_of_dictionaries:
                ids_to_names_dictionary.update(d)
            url = get_next_url(json_data)
        aim_directory = self.dir + principle + '/'
        if not os.path.exists(aim_directory):
            os.mkdir(aim_directory)
        final_file = open(aim_directory + str(datetime.now().date()) + '.json', 'w')
        json.dump(ids_to_names_dictionary, final_file)
Exemple #2
0
 def __load_info(self):
     url = get_url(self.id, self.access_token, userUrlPrototype)
     try:
         response = urllib2.urlopen(url)
     except urllib2.HTTPError:
         return
     except urllib2.URLError:
         return
     raw_data = response.read()
     data = loads(raw_data)
     if "error_message" in data["meta"].keys():
         print data["meta"]["error_message"]
         return
     self.info.update(data["data"])
Exemple #3
0
 def update_media_data(self):
     if self.user_id is None:
         return False
     print "Updating media"
     data_url = get_url(self.user_id, self.access_token, recentMediaUrlPrototype)
     data = list()
     filename = "__tmpFile"
     f = open(filename, 'w')
     while data_url is not None:
         response = urllib2.urlopen(data_url)
         raw_data = response.read()
         f = open(filename, 'w')
         f.write(raw_data)
         f = open(filename, 'r')
         json_all = json.load(f)
         data += json_all['data']
         if self.media_count is not None:
             print int(100 * (len(data) / float(self.media_count))), "%"
         data_url = get_next_url(json_all)
     f.close()
     os.remove(filename)
     final_file = open(self.dir + "mediaData.json", 'w')
     json.dump(data, final_file)
Exemple #4
0
    def update_user_data(self):
        if self.user_id is None:
            return False
        print "Updating user data"
        data_url = get_url(self.user_id, self.access_token, userUrlPrototype)
        response = urllib2.urlopen(data_url)
        raw_data = response.read()
        filename = self.dir + "userData.json"
        f = open(filename, 'w')
        f.write(raw_data)
        f = open(filename)
        json_data = json.load(f)
        counts = json_data['data']['counts']
        self.media_count = counts['media']

        if not os.path.exists(self.dynamic_data):
            dyn_data = {str(self.current_date): counts}
        else:
            f_dynamic = open(self.dynamic_data, 'r')
            dyn_data = json.load(f_dynamic)
            dyn_data[str(self.current_date)] = counts

        f_dynamic = open(self.dynamic_data, 'w+')
        json.dump(dyn_data, f_dynamic, separators=(',\n', ':'))