Beispiel #1
0
    def getVoiceText(self):

        # XXX record from micphone
        path = OutputPath.getDataPath('input', 'pcm')

        record(path)
        '''
        cmd = 'aplay -r 16000 -f S16_LE -c 1 {}'.format(path)
        runCommand(cmd)
        '''

        with open(path, 'rb') as fp:

            obj = self.aipSpeech.asr(fp.read())

            error = obj.pop('err_no')

            if error is 0:
                res = obj.pop('result')

                if isinstance(res, list) and len(res) > 0:
                    return (res[0])
            else:
                print(obj.pop('err_msg'))

        return None
Beispiel #2
0
def getSlogan(skuid):

    path = OutputPath.getDataPath(skuid, 'html')

    SKU_MAIN_URL_TEMPLATE = 'http://item.m.jd.com/product/{}.html'
    url = SKU_MAIN_URL_TEMPLATE.format(skuid)

    ret = Network.saveGetUrl(path, url)
    #print 'Update', path, ':', ret

    if ret < 0:
        return None

    #PATTERN = r'<div class="prod-act">(.*?)</div>'
    PATTERN = r'<div class="prod-act">(.*?)<'

    with open(path) as fp:
        content = fp.read()

        slogan = getMatchString(content, PATTERN)
        if slogan is not None and not isinstance(slogan, unicode):
            slogan = unicode(slogan, errors='ignore')
        return slogan

    return None
Beispiel #3
0
    def generate(self, tts, content, silencePath):

        text = self.getValue(content, 'text')

        self.name = self.getValue(content, 'name')
        name = self.name

        if name:
            try:
                OutputPath.createDataPath(name)
            except UnicodeEncodeError as e:
                name = None
                print(e)

        if not name:

            m = hashlib.md5()
            m.update(text.encode('utf-8'))
            name = m.hexdigest()[:8]

            OutputPath.createDataPath(name)

        self.path = OutputPath.getDataPath(name)

        self.prepare()

        self.saveImages(content['image-urls-list'])
        self.generateTts(tts, text, silencePath)

        self.createSlider()
        self.merge()
Beispiel #4
0
    def getComplexImage(self):

        content = self.getHtml()

        path = OutputPath.getDataPath('sku-{}-complex'.format(self.skuid),
                                      'html')

        with open(path, 'w') as fp:
            fp.write(content)

        chmod(path)

        return ImageKit.fromHtml(path, pageSize=(80, 150))
Beispiel #5
0
    def getPlateImage(self):

        if self.skuimgurl is None:
            return None

        path = OutputPath.getDataPath('sku-{}-plate'.format(self.skuid),
                                      'jpeg')

        ret = Network.saveGetUrl(path, self.skuimgurl)

        if ret < 0:
            return None

        return path
Beispiel #6
0
    def voiceOut(self, stringContent):

        result = self.aipSpeech.synthesis(stringContent, 'zh', 1, {
            'vol': 15,
            'per': 0
        })

        if not isinstance(result, dict):

            path = OutputPath.getDataPath('voice', 'wav')
            with open(path, 'wb') as fp:
                fp.write(result)

            cmd = 'mplayer {}'.format(path)
            runCommand(cmd)
Beispiel #7
0
    def update(self, page=1, force=False):

        path = OutputPath.getDataPath('seckill-{}-{}'.format(self.gid, page), 'json')

        if page is 1:
            url = 'https://ms.m.jd.com/seckill/seckillList.json?gid={}'.format(self.gid)
        else:
            url = 'https://ms.m.jd.com/seckill/seckillListPage.json?isPagination=true&gid={}&page={}'.format(self.gid,
                    page)

        ret = Network.saveHttpData(path, url, force=force)
        if ret < 0:
            return False

        return self.parse(path)
Beispiel #8
0
    def getComments(self):

        url = Infor.COMMENT_URL_TEMPLATE.format(self.skuid)
        path = OutputPath.getDataPath(self.skuid, 'json')

        ret = Network.saveGetUrl(path, url)

        if ret < 0:
            return None

        with open(path) as fp:
            content = fp.read()
            obj = json.loads(content)
            return obj.pop('wareDetailComment')

        return None
Beispiel #9
0
    def getSlogan(self):

        path = OutputPath.getDataPath(self.skuid, 'html')
        url = Infor.SKU_MAIN_URL_TEMPLATE.format(self.skuid)

        ret = Network.saveGetUrl(path, url)

        if ret < 0:
            return None

        with open(path) as fp:
            content = fp.read()

            slogan = getMatchString(content, Infor.SLOGAN_PATTERN)
            if slogan is not None and not isinstance(slogan, unicode):
                slogan = unicode(slogan, errors='ignore')
            return slogan

        return None
Beispiel #10
0
    def search(self, content):

        r = Network.post(self.url, data={'content': content})

        if r is None:
            print 'No result for', content
            return False

        try:
            obj = json.loads(r.content.decode('utf-8', 'ignore'))
        except ValueError as e:
            print 'Error (', e, ') of json: "', r.content, '"'
            return False

        num = obj['num']
        if num is 0:
            print 'Error content: "', r.content, '"'
            return False

        print 'Found', num, 'SKU with "', content, '"'

        datas = obj['list']

        plates = list()
        urls = list()

        for data in datas:

            formatter = SpecialFormatter.create(data)

            plate = formatter.getPlate(self.qwd)
            url = data['skuimgurl']

            plates.append(plate)
            urls.append(url)

        now = datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
        path = OutputPath.getDataPath('search_{}'.format(now), 'jpeg')

        self.plate = '\n----------------------------\n'.join(plates)
        self.image = ImageKit.concatUrlsTo(path, urls)

        return True
Beispiel #11
0
def getComments(skuid):

    COMMENT_URL_TEMPLATE = 'http://item.m.jd.com/ware/getDetailCommentList.json?wareId={}'
    url = COMMENT_URL_TEMPLATE.format(skuid)

    path = OutputPath.getDataPath(skuid, 'json')

    ret = Network.saveGetUrl(path, url)
    #print 'Update', path, ':', ret

    if ret < 0:
        return None

    with open(path) as fp:
        content = fp.read()
        obj = json.loads(content)
        return obj.pop('wareDetailComment')

    return None
Beispiel #12
0
    def getImage(self):

        path = OutputPath.getDataPath(self.skuid, 'html')
        url = Infor.SKU_MAIN_URL_TEMPLATE.format(self.skuid)

        ret = Network.saveGetUrl(path, url)

        if ret < 0:
            return None

        with open(path) as fp:
            content = fp.read()

            m = re.search(Infor.IMAGE_PATTERN, content)

            if m is None:
                return None

            url = getMatchString(m.group(0), Infor.MARK_PATTERN)
            return url

        return None
Beispiel #13
0
    def getPriceHistoryData(executor, data):

        skuid = data['skuid']
        title = data['title']

        url = 'http://item.jd.com/{}.html'.format(skuid) # For history searching
        # Get URL for price history
        url = executor.context.requestPriceInfo(title, url)

        # Get price histories
        path = OutputPath.getDataPath(skuid, 'js')

        ret = Network.saveHttpData(path, url)
        #print 'Update', path, ':', ret, ':', title

        if ret < 0:
            return None

        obj = PriceHistoryManager.parse(path)
        if obj is None:
            return None

        return PriceHistoryManager.generatePriceHistoryData(data, obj)