def db_params(**kwargs):
  from biflorica.commons import config

  try:
    return kwargs['sqlalchemy.url'], True
  except KeyError:
    pass

  try:
    return config.get('db', 'url'), {'yes': True, 'no': False}[config.get('db', 'echo')]
  except:
    return '', False
    class _LastOffers():
      URL = API_BASE_URL + config.get('biflorica.api.requests', 'last_offers')
      PARAMS = DictDotLookup(eval("""{
        '_': {
        }, 'contentType': {
          'JSON': 'json'
        }, 'limit': {
          'PAGE': '26'
        }, 'start': {
          'TOP': '0'
        }
      }"""))

      def __init__(self, session=None):
        self._session = session

      def get(self, start=PARAMS.start.TOP(), limit=PARAMS.limit.PAGE()):
        payload = {
          '_': '{0}'.format(int(time.time()*1000)),
          'contentType': APISession._Requests._LastOffers.PARAMS.contentType.JSON(),
          'start': start,
          'limit': limit
        }
        r = self._session.get(APISession._Requests._LastOffers.URL, params=payload)
        return Requests.LastOffers(**r.json()['data'])
  class _Handbook():
    URL = API_BASE_URL + config.get('biflorica.api', 'handbook')
    PARAMS = DictDotLookup(eval("""{
      '_': {
      }, 'sections': {
        'FARM': 'farm',
        'SIZE': 'size',
        'VARIETY': 'variety',
        'TYPE': 'type',
        'SORT': 'sort',
        'TRADEMARK': 'trademark',
        'LANGUAGE': 'lang'
      }
    }"""))

    def __init__(self, session=None):
      self._session = session

    def get(self, sections=[PARAMS.sections.LANGUAGE()]):
      payload = {
        '_': '{0}'.format(int(time.time()*1000)),
        'sections[]': sections
      }
      r = self._session.get(APISession._Handbook.URL, params=payload)
      return Handbook(**r.json()['data'])
      def post(self, user=None, password=None):
        from biflorica.platform import Login as PlatformLogin
        # This page does not use encryption and sends the credentials in plain text!
        # The name of the input fields are not what they are reported in the html.
        # I had to look at the actual request in Firebug to see the real names of the
        # fields.
        if user is None:
          user = config.get('biflorica.platform', 'login')
          password = config.get('biflorica.platform', 'password')

        payload = {'login': user, 'password': password}
        # First get the cookies in the html site, we'll need them
        r = self._session.get(PlatformLogin.URL)
        tmp = r.content
        # The real login is in here, in the API login
        r = self._session.post(APISession._Users._Login.URL, data=payload)
        tmp = r.content
    class _Logout():
      URL = API_BASE_URL + config.get('biflorica.api.users', 'logout')
      PARAMS = DictDotLookup(eval("""{}"""))

      def __init__(self, session=None):
        self._session = session

      def get(self):
        r = self._session.get(APISession._Users._Logout.URL)
        tmp = r.content
    class _GetPlatformList():
      URL = API_BASE_URL + config.get('biflorica.api.requests', 'get_platform_list')
      PARAMS = DictDotLookup(eval("""{'_': {}}"""))

      def __init__(self, session=None):
        self._session = session

      def get(self):
        payload = {'_': '{0}'.format(int(time.time()*1000))}
        r = self._session.get(APISession._Requests._GetPlatformList.URL, params=payload)
        return Requests.GetPlatformList(**r.json()['data'])
    class _FilterCounters():
      URL = API_BASE_URL + config.get('biflorica.api.requests', 'filter_counters')
      PARAMS = DictDotLookup(eval("""{'_': {}}"""))

      def __init__(self, session=None):
        self._session = session

      def get(self):
        payload = {'_': '{0}'.format(int(time.time()*1000))}
        r = self._session.get(APISession._Requests._FilterCounters.URL, params=payload)
        return Requests.FilterCounters(**r.json()['data'])
    class _UnreadCount():
      URL = API_BASE_URL + config.get('biflorica.api.messages', 'unread_count')
      PARAMS = DictDotLookup(eval("""{'_': {}}"""))

      def __init__(self, session=None):
        self._session = session

      def get(self):
        payload = {'_': '{0}'.format(int(time.time()*1000))}
        r = self._session.get(APISession._Messages._UnreadCount.URL, params=payload)
        return Messages.UnreadCount(**r.json()['data'])
class Login():
    URL = PLATFORM_BASE_URL + config.get('biflorica.platform', 'login_url')
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from biflorica.commons import config

PLATFORM_BASE_URL = config.get('biflorica.platform', 'base_url')


class Login():
    URL = PLATFORM_BASE_URL + config.get('biflorica.platform', 'login_url')
  class _MarketPlace():
    URL = API_BASE_URL + config.get('biflorica.api', 'marketplace')
    PARAMS = DictDotLookup(eval("""{
      '_': {
      }, 'contentType': {
        'JSON': 'json'
      }, 'efiltersChecks': {
        'FALSE': 'false',
        'TRUE': 'true'
      }, 'hideCompetitors': {
        'FALSE': 'false',
        'TRUE': 'true'
      }, 'limit': {
        'PAGE': '26'
      }, 'onlyTrusted': {
        'FALSE': 'false',
        'TRUE': 'true'
      }, 'show': {
        'SELLERS': 'sellers',
        'BUYERS': 'buyers',
        'ALL': 'all'
      }, 'sort': {
        'UTIME': 'utime',
        'FARM': 'farm',
        'TYPE': 'type',
        'VARIETY': 'variety',
        'PRICE40': 'price40',
        'PRICE50': 'price50',
        'PRICE60': 'price60',
        'PRICE70': 'price70',
        'PRICE80': 'price80',
        'PRICE90': 'price90',
        'PRICE100': 'price100',
        'PRICE100P': 'price100p',
        'HB': 'hb',
        'EXP': 'exp',
        'ADATE': 'adate'
      }, 'direct': {
        'DESCENDING': 'desc',
        'ASCENDING': 'asc'
      }, 'start': {
        'TOP': '0'
      }, 'farm': {
      }, 'type': {
      }, 'variety': {
      }, 'size': {
      }
    }"""))

    def get(self, start=PARAMS.start.TOP(), 
            limit=PARAMS.limit.PAGE(), 
            sort=PARAMS.sort.UTIME(), 
            direct=PARAMS.direct.DESCENDING(), 
            show=PARAMS.show.ALL(), 
            onlyTrusted=PARAMS.onlyTrusted.FALSE(), 
            hideCompetitors=PARAMS.hideCompetitors.FALSE(), 
            efiltersChecks=PARAMS.efiltersChecks.FALSE(), 
            farm=[], type=[], variety=[], size=[]):
      # The '_' parameter is composed of 13 ditigs:
      # *- 10 at the beginning for the UNIX timestamp (seconds since 1/1/1970)
      # *- 3 ending numbers which I believe are just miliseconds
      payload = {
        '_': '{0}'.format(int(time.time()*1000)),
        'contentType': APISession._MarketPlace.PARAMS.contentType.JSON(),
        'efiltersChecks': efiltersChecks,
        'hideCompetitors': hideCompetitors,
        'limit': limit,
        'onlyTrusted': onlyTrusted,
        'show[]': show,
        'sort': sort,
        'direct': direct,
        'start': start,
        'farm[]': farm,
        'type[]': type,
        'variety[]': variety,
        'size[]': size
      }
      r = self._session.get(APISession._MarketPlace.URL, params=payload)
      return MarketPlace(**r.json()['data'])

    def __init__(self, session=None):
      self._session = session
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests, time, json

from biflorica.commons import config
from commons import DictDotLookup
from objects import Handbook, Messages, Requests, Log, Filters, MarketPlace

API_BASE_URL = config.get('biflorica.api', 'base_url')

class APISession():
  def __init__(self, headers=None):
    self.session = requests.Session()
    self.session.headers.update(headers)

    self.marketplace = self._MarketPlace(self.session)
    self.log = self._Log(self.session)
    self.filters = self._Filters(self.session)
    self.handbook = self._Handbook(self.session)
    self.messages = self._Messages(self.session)
    self.requests = self._Requests(self.session)
    self.users = self._Users(self.session)

  class _MarketPlace():
    URL = API_BASE_URL + config.get('biflorica.api', 'marketplace')
    PARAMS = DictDotLookup(eval("""{
      '_': {
      }, 'contentType': {
        'JSON': 'json'
      }, 'efiltersChecks': {