Example #1
0
    def like_feed(self):
        # like news feed

        post_ids = self.bucket['feed']['like']
        if len(post_ids) == 0:
            self.console.log('\nNo new posts from your friends to like')
            return [True]
        elif len(post_ids) > 5:
            until = 5
        else:
            until = len(post_ids)
        self.console.log('\nLiking news feed...')
        ban_counter = 0
        for i in range(0, until):
            try:
                post = self.bucket['feed']['like'][0]
                liked = post_data(self.browser, self.insta_urls['like'], post[0], False)
                if liked['response'].ok:
                    self.console.log(" * Liked %s's post %s" %(post[1], self.insta_urls['media'] %(self.bucket['codes'][post[0]])))
                elif liked['response'].status_code == 400:
                    ban_counter += 1
                    if ban_counter > 2:
                        self.bucket['feed']['done'].append(post)
                        self.bucket['feed']['like'].remove(post)
                        return [False, liked['response']]
                else:
                    raise Exception("%s could not like %s's media" %(liked['response'].status_code, post_ids[i][2]))
            except Exception as e:
                self.console.log(' * Error: %s' %(e))
            self.bucket['feed']['done'].append(post)
            self.bucket['feed']['like'].remove(post)
            sleep(10*aleatory())
        self.total_counters['like_feed'] += 1
        return [True]
Example #2
0
    def main(self):

        # --- main ---

        self.loop_count = 0
        self.clean_up_loop_count = 0
        # unfollow previous session's follows
        if self.cache['unfollow']:
            if len(self.cache['unfollow']) >= 1:
                add = [
                    self.bucket['explore']['unfollow'].append([user[0], i])
                    for i, user in enumerate(self.cache['unfollow'])
                ]
                self.clean_up(
                    on_exit=True,
                    statement='\nCleaning up last sessions follows...')
        # start
        self.console.log('\n\tStarting Bot at %s' %
                         (arrow.now().format('HH:mm:ss DD/MM/YYYY')))
        sleep(1 * aleatory())
        # check if out of hours
        if not self.run_all_day:
            if self.times['start_bot'] > self.time_now():
                self.console.log('\nOut of hours... sleeping until %s' %
                                 (self.params['bot_start_at']))
                #print self.times['start_bot'], self.time_now(), self.times['tomorrow_start']
                sleep(int(self.times['start_bot'] - self.time_now()))
        # main while loop
        while True:
            dc = self.day_counters
            self.console.backup()
            self.console.log(
                '\n\tCurrent daily count:\n\t - All operations: %s\n\t - Like news feed: %s\n\t - Likes: %s\n\t - Follows: %s\n\t - Unfollows: %s\n\t - Comments: %s'
                % (dc['all'], dc['like_feed'], dc['like'], dc['follow'],
                   dc['unfollow'], dc['comment']))
            # here we go
            try:
                if self.run_all_day:
                    # all day mode
                    while internet_connection():
                        self.main_loop()
                        if all(self.max_operation[op] == self.day_counters[op]
                               for op in self.day_counters if op != 'unfollow'
                               if op != 'all'):
                            self.reset_day_counters()
                            self.starting_operations()
                    continue
                # hour to hour mode
                while self.times['start_bot'] < self.time_now(
                ) < self.times['stop_bot']:
                    # where the magic happens
                    self.main_loop()
                # if the loop is broken, finish up
                self.clean_up(on_exit=False,
                              statement='\nFinishing operations...')
                sleep(10)
                self.console.log('\nSleeping until %s' %
                                 (self.params['bot_start_at']))
                sleep_time = int(self.times['tomorrow_start'] -
                                 self.time_now())
                if sleep_time > 0:
                    sleep(sleep_time)
                # check if it's the next day
                if self.time_now() > self.times['tomorrow_start']:
                    self.reset_day_counters()
                    self.starting_operations()

            except ConnectionError:
                self.console.log('Connection error!')
                if not internet_connection():
                    while not internet_connection():
                        self.console.log(
                            'NO INTERNET - re-establish connection to continue'
                        )
                        sleep(60)
                self.reset_day_counters()
                self.starting_operations()
                self.catch_up_operations()
                if not self.check_login():
                    self.init_requests()

            except Exception as e:
                self.console.log('Error: %s' % (e))

            except KeyboardInterrupt:
                self.clean_up(
                    on_exit=True,
                    statement=
                    '\nStopping Bot. Please wait for cleaning follows...')
                self.console.log('\nCleanup Done. Logging out...')
                self.log_out()

        exit('\nBye!\n')