import logging
from ..parsers import DictParser

from base_scraper import TwittrScrapr

log = logging.getLogger('scrapers.TimelineScrapr')


class TimelineScrapr(TwittrScrapr):
    def __init__(self, api_keys, writer):
        super(TimelineScrapr, self).__init__(api_keys, writer)

    def _fetch_user_timeline(self, user):
        finished_pagination = False
        new_max = None

        results = []

        parser = DictParser()
        while not finished_pagination:
            self.check_rate_limit()
            call_result = self.api.get_user_timeline(screen_name=user, count=200, include_rts=1, trim_user=True,
                                                     max_id=new_max)

            if len(call_result) > 0:
                results.extend([parser.parse(t, user=user) for t in call_result])
                new_max = str(int(call_result[-1]['id_str']) - 1)
            else:
                finished_pagination = True

            self.reset_time = self.api.get_lastfunction_header('x-rate-limit-reset')
            self.calls_remaining = self.api.get_lastfunction_header('x-rate-limit-remaining')

        return results

    @TwittrScrapr.error_handler
    def fetch_user_statuses(self, writer):
        for user in self.scrape_queue:
            log.info('Fetching tweets for {}'.format(user))
            res = self._fetch_user_timeline(user)
            log.info('Got {} tweets'.format(len(res)))
            writer.writerows(res)
