# Path: baobab/utils/authentication.py
# class Authentication(object):
# 
#     def __init__(self, consumer_key, consumer_secret, token_key, token_secret):
#         consumer = Consumer(key=consumer_key, secret=consumer_secret)
#         token = Token(key=token_key, secret=token_secret)
# 
#         proxy_info = None
#         if hasattr(settings, 'PROXY_HOST') and \
#                 hasattr(settings, 'PROXY_PORT'):
#             proxy_info = ProxyInfo(
#                 proxy_type=PROXY_TYPE_HTTP,
#                 proxy_host=settings.PROXY_HOST,
#                 proxy_port=settings.PROXY_PORT)
#         self.client = Client(
#             consumer=consumer,
#             token=token,
#             proxy_info=proxy_info)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         pass
# 
# Path: baobab/socialnetwork/base.py
# class SocialNetworkBase(object):
# 
#     """
#     interface for all social network, each social network have to
#     inherited from this class
#     """
# 
#     name = None
# 
#     @classmethod
#     def is_configured(cls):
#         raise NotImplementedError()
# 
#     @classmethod
#     def get_max_char(cls, default):
#         raise NotImplementedError()
# 
#     def publish(self, msg, url):
#         """
#         should raise if it failed
#         """
#         raise NotImplementedError()
# 
#     def  __enter__(self):
#         raise NotImplementedError()
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         raise NotImplementedError()
# 
# class UnPublishedException(Exception):
#     pass


from datetime import datetime, timedelta
from django.conf import settings
from baobab.utils.authentication import Authentication
from .base import SocialNetworkBase, UnPublishedException
import json
import logging
import pytz
import urllib
            return content['text']
        return ''

    def get_last_msg(self):
        url = '%s/account/verify_credentials.json' % self.url_api
        msg_err = 'Getting last tweet'
        content = self._request(url, method='GET', msg_err=msg_err)
        if content:
            return content['status']
        return None

    def _do_manual_rate_limit(self, url):
        limit = self.manual_rate_limit.get(url, None)
        if not limit:
            return
        if not limit['request_window'] or \
                limit['request_window'] < datetime.now(pytz.timezone('UTC')):
            minutes = limit['window_request']
            limit['request_window'] = (datetime.now(pytz.timezone('UTC')) +
                                       timedelta(minutes=minutes))
            limit['request_done'] = 1
        else:
            limit['request_done'] += 1
        if limit['request_done'] == limit['max_request']:
            self.rate_limit[url] = limit['request_window']
            LOG.warning('Rate limit will be exceeded for: %s', url)

    def _request(self, url, method='GET', body='', msg_err=''):
        if self._is_rate_limited(url):
            return None

# Path: baobab/utils/authentication.py
# class Authentication(object):
# 
#     def __init__(self, consumer_key, consumer_secret, token_key, token_secret):
#         consumer = Consumer(key=consumer_key, secret=consumer_secret)
#         token = Token(key=token_key, secret=token_secret)
# 
#         proxy_info = None
#         if hasattr(settings, 'PROXY_HOST') and \
#                 hasattr(settings, 'PROXY_PORT'):
#             proxy_info = ProxyInfo(
#                 proxy_type=PROXY_TYPE_HTTP,
#                 proxy_host=settings.PROXY_HOST,
#                 proxy_port=settings.PROXY_PORT)
#         self.client = Client(
#             consumer=consumer,
#             token=token,
#             proxy_info=proxy_info)
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         pass
# 
# Path: baobab/socialnetwork/base.py
# class SocialNetworkBase(object):
# 
#     """
#     interface for all social network, each social network have to
#     inherited from this class
#     """
# 
#     name = None
# 
#     @classmethod
#     def is_configured(cls):
#         raise NotImplementedError()
# 
#     @classmethod
#     def get_max_char(cls, default):
#         raise NotImplementedError()
# 
#     def publish(self, msg, url):
#         """
#         should raise if it failed
#         """
#         raise NotImplementedError()
# 
#     def  __enter__(self):
#         raise NotImplementedError()
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         raise NotImplementedError()
# 
# class UnPublishedException(Exception):
#     pass


# Path: baobab/socialnetwork/twitter.py
from datetime import datetime, timedelta
from django.conf import settings
from baobab.utils.authentication import Authentication
from .base import SocialNetworkBase, UnPublishedException
import json
import logging
import pytz
import urllib

            return content['text']
        return ''

    def get_last_msg(self):
        url = '%s/account/verify_credentials.json' % self.url_api
        msg_err = 'Getting last tweet'
        content = self._request(url, method='GET', msg_err=msg_err)
        if content:
            return content['status']
        return None

    def _do_manual_rate_limit(self, url):
        limit = self.manual_rate_limit.get(url, None)
        if not limit:
            return
        if not limit['request_window'] or \
                limit['request_window'] < datetime.now(pytz.timezone('UTC')):
            minutes = limit['window_request']
            limit['request_window'] = (datetime.now(pytz.timezone('UTC')) +
                                       timedelta(minutes=minutes))
            limit['request_done'] = 1
        else:
            limit['request_done'] += 1
        if limit['request_done'] == limit['max_request']:
            self.rate_limit[url] = limit['request_window']
            LOG.warning('Rate limit will be exceeded for: %s', url)

    def _request(self, url, method='GET', body='', msg_err=''):
        if self._is_rate_limited(url):
            return None

        try: