Exemplo n.º 1
0
 def new_channel(self, pl=None, **kwarg):
     if pl:
         try:
             return getattr(platform, pl).new_channel(**kwarg)
         except AttributeError:
             logger.warning("can not find platform '%s'", pl)
             return utils.JsonDict()
     else:
         _fn_conf = path.join(SNSConf._SNSAPI_DIR_STATIC_DATA,
                              'init-channel.json.example')
         return utils.JsonDict(json.load(open(_fn_conf)))
Exemplo n.º 2
0
 def new_channel(self, pl=None, **argd):
     if pl:
         try:
             return getattr(platform, pl).new_channel(**argd)
         except AttributeError:
             logger.warning("can not find platform '%s'", pl)
             return utils.JsonDict()
     else:
         return utils.JsonDict(
             json.load(open(abspath('conf/init-channel.json.example'),
                            'r')))
Exemplo n.º 3
0
    def __init__(self, dct = None, platform = None, channel = None, conf = {}):

        self.conf = conf
        self['deleted'] = False
        self['ID'] = MessageID(platform, channel)

        self['raw'] = utils.JsonDict({})
        self['parsed'] = utils.JsonDict({'attachments' : []})
        if dct:
            self['raw'] = utils.JsonDict(dct)
            try:
                self.parse()
            except KeyError as e:
                raise snserror.type.parse(str(e))
Exemplo n.º 4
0
    def load_config(self,
                    fn_channel=DIR_DEFAULT_CONF_CHANNEL,
                    fn_pocket=DIR_DEFAULT_CONF_POCKET):
        """
        Read configs:
        * channel.conf
        * pocket.conf
        """

        count_add_channel = 0
        try:
            with open(path.abspath(fn_channel), "r") as fp:
                allinfo = json.load(fp)
                for site in allinfo:
                    if self.add_channel(utils.JsonDict(site)):
                        count_add_channel += 1
        except IOError:
            #raise snserror.config.nofile(fn_channel)
            logger.warning("'%s' does not exist. Use default", fn_channel)
        except ValueError as e:
            raise snserror.config.load("file: %s; message: %s" %
                                       (fn_channel, e))

        try:
            with open(path.abspath(fn_pocket), "r") as fp:
                allinfo = json.load(fp)
                self.jsonconf = allinfo
        except IOError:
            #raise snserror.config.nofile(fn_pocket)
            logger.warning("'%s' does not exist. Use default", fn_pocket)
        except ValueError as e:
            raise snserror.config.load("file: %s; message:%s" %
                                       (fn_channel, e))

        logger.info("Read configs done. Add %d channels" % count_add_channel)
Exemplo n.º 5
0
    def __init__(self, dct=None, platform=None, channel=None):

        self['ID'] = MessageID(platform, channel)
        #if platform:
        #    self['ID']['platform'] = platform
        #if channel:
        #    self['ID']['channel'] = channel

        self['raw'] = utils.JsonDict({})
        self['parsed'] = utils.JsonDict({})
        if dct:
            self['raw'] = utils.JsonDict(dct)
            try:
                self.parse()
            except KeyError, e:
                raise snserror.type.parse(e.message)
Exemplo n.º 6
0
    def read_channel(self, channel):
        self.jsonconf = utils.JsonDict(channel)

        if 'auth_info' in channel:
            self.auth_info.update(channel['auth_info'])
            self.auth_info.set_defaults()

        if not 'host' in self.auth_info:
            self.auth_info['host'] = 'localhost'
        if not 'port' in self.auth_info:
            self.auth_info['port'] = 12121
Exemplo n.º 7
0
 def auth_second(self):
     '''
     docstring placeholder
     '''
     # NOTE:
     # Accordion to Douban API, the token will expire
     # in one week.
     try:
         url = self.fetch_code()
         code = self._parse_code(url)
         self.client.auth_with_code(code["code"])
         self.token = utils.JsonDict({
             "access_token": self.client.token_code,
             "expires_in": 7 * 24 * 60 * 60
         })
     except Exception, e:
         logger.warning("Auth second fail. Catch exception: %s", e)
         self.token = None
Exemplo n.º 8
0
    def new_channel(full=False):
        '''
        Return a JsonDict object containing channel configurations. 

        full:
            False: only returns essential fields. 
            True: returns all fields (essential + optional). 

        '''

        c = utils.JsonDict()
        c['channel_name'] = 'new_channel_name'
        c['open'] = 'yes'

        if full:
            c['description'] = "a string for you to memorize"
            # Defaultly enabled methods in SNSPocket batch operation
            c['methods'] = ""

        return c
Exemplo n.º 9
0
 def auth_second(self):
     '''
     docstring placeholder
     '''
     # NOTE:
     # Instagram API will not return any info related to the
     # expiration data. Accordian to its document, the expiration
     # data is uncertain. Instagram makes no guarantee on it thus
     # we set it to two weeks (86400 * 14)
     try:
         url = self.fetch_code()
         code = self._parse_code(url)
         (token,
          user_info) = self.api.exchange_code_for_access_token(code["code"])
         self.token = utils.JsonDict({
             "access_token": token,
             "user_info": user_info
         })
         self.token.expires_in = 86400 * 14 + self.time()
     except Exception, e:
         logger.warning("Auth second fail. Catch exception: %s", e)
         self.token = None
Exemplo n.º 10
0
    def load_config(self, \
            fn_channel = 'conf/channel.json',\
            fn_pocket = 'conf/pocket.json'):
        """
        Read configs:
        * channel.conf
        * pocket.conf
        """

        count_add_channel = 0
        try:
            with open(abspath(fn_channel), "r") as fp:
                allinfo = json.load(fp)
                for site in allinfo:
                    if self.add_channel(utils.JsonDict(site)):
                        count_add_channel += 1
        except IOError:
            #raise snserror.config.nofile(fn_channel)
            logger.warning("'%s' does not exist. Use default", fn_channel)
        except ValueError, e:
            raise snserror.config.load("file: %s; message: %s" %
                                       (fn_channel, e.message))