Esempio n. 1
0
    def _search(self):
        line = self.widget_[1].get('1.0', 'end').strip()
        if line == '':
            showerror(message='请输入查询内容!')
            return
        else:
            self._segment()
            self.widget_[11].delete('1.0', 'end')
            self.widget_[13].delete('1.0', 'end')

            fool_result, jieba_result = api.query(line)
            result_list, jieba_list = api.format(fool_result), api.format(
                jieba_result)

            content = ''.join([
                f'{index}.{phonenum},{all_name}\n'
                for index, (phonenum, all_name) in enumerate(result_list, 1)
            ])

            jieba_content = ''.join([
                f'{index}.{phonenum},{all_name}\n'
                for index, (phonenum, all_name) in enumerate(jieba_list, 1)
            ])

            self.widget_[11].insert('insert', content)
            self.widget_[13].insert('insert', jieba_content)

            params = (self.widget_[i].get('1.0', 'end').strip()
                      for i in range(1, len(self.widget_), 2))
            api._save_query(*params)
    def test_query_articles(self, change, collect_query_parameters,
                            generate_queries, run_queries, transform_results,
                            to_json):
        # def test_save_an_article(self, change):
        #when
        query({}, None)

        #verify get_database_object_invoked
        change.assert_called()

        collect_query_parameters.assert_called()

        generate_queries.assert_called()
        run_queries.assert_called()

        transform_results.assert_called()
        to_json.assert_called()
Esempio n. 3
0
def Execute_Audit(header, entry, action, audit):
    """
    Method to handle the audit stage of an order. Calculate the profit/perf of
    a trade.
    header    (req) - Header xml object from the xml ordee config file
    entry     (req) - Entry xml object from the xml ordee config file
    action    (req) - Action xml object from the xml ordee config file
    audit     (req) - Audit xml object from the xml ordee config file
    """
    pair = header.get("pair")
    indate = datetime.datetime.strptime(header.get("date"), 
                                        "%Y-%m-%d %H:%M:%S.%f")
    nowdate = datetime.datetime.now()
    if action.get("date") != None :
        outdate = datetime.datetime.strptime(action.get("date"), 
                                             "%Y-%m-%d %H:%M:%S.%f")
        Log("pending till "+str(outdate + datetime.timedelta(hours=1)))
    else:
        Log("no audit (no data)")
        outdate = nowdate
        audit.set("status", "ready")
    if(nowdate > (outdate + datetime.timedelta(hours=1)) ) : 
        TotalBuy = 0.0
        TotalSell = 0.0
        TotalFees = 0.0
        RawPerf = 0.0
        NetPerf = 0.0
        Output = api.query("GetTradeHistory", {'Market':pair, 'Count':25})
        if (Output["Data"] != []):
            for line in Output["Data"]:
                linedate_str = str(line["TimeStamp"]).replace("T"," ")[:-2]+"0"
                linedate = datetime.datetime.strptime(linedate_str,
                                                      "%Y-%m-%d %H:%M:%S.%f")
                linedate = linedate + datetime.timedelta(hours=1)
                if (linedate > (indate + datetime.timedelta(hours=-1)) and 
                    linedate < (outdate + datetime.timedelta(hours=1))) :
                    if(line["Type"] == "Buy"):
                        TotalBuy = TotalBuy + line["Total"]
                        TotalFees = TotalFees + line["Fee"]
                    if(line["Type"] == "Sell"):
                        TotalSell = TotalSell + line["Total"]
                        TotalFees = TotalFees + line["Fee"]
    
            audit.set("Buy", '{:.8f}'.format(TotalBuy))
            audit.set("Sell", '{:.8f}'.format(TotalSell))
            audit.set("Fees", '{:.8f}'.format(TotalFees))
            RawProfit = TotalSell - TotalBuy
            audit.set("RawProfit", '{:.8f}'.format(RawProfit))
            NetProfit = TotalSell - TotalBuy - TotalFees
            audit.set("NetProfit", '{:.8f}'.format(NetProfit))
            if(TotalBuy > 0):
                RawPerf = RawProfit/TotalBuy
            audit.set("RawPerf", '{:.8f}'.format(RawPerf))
            if(TotalBuy > 0):
                NetPerf = NetProfit/TotalBuy
            audit.set("NetPerf", '{:.8f}'.format(NetPerf))
            audit.set("status", "ready")
Esempio n. 4
0
def find_node_by(predicate, value, resourceType):
    q = (
        f"query node($value: string) {{\n"
        f"node(func: eq({predicate}, $value)) @filter(has({resourceType})) {{\n"
        f"uid\n"
        f"expand(_all_)\n"
        f"}}\n"
        f"}}\n")

    resp = api.query(q, params={'$value': value})
    if len(resp['node']) > 0:
        return resp['node'][0]['uid']
    return None
Esempio n. 5
0
def Check_Buy_Orders(pair):
    """
    Method to check if buy orders are existing for a given pair. Parameters:
    pair     (req) - Market pair, string in format XXX_YYY ("XMR_BTC")
    """
    Output = Get_Cache("GetMarket", [pair])
    pairid = Output["Data"]["TradePairId"]
    Output = api.query("GetOpenOrders", {'TradePairId':pairid})
    buy_orders = False
    for order_item in Output["Data"]:
        if order_item["Type"] == "Buy":
            buy_orders = True
    return buy_orders
Esempio n. 6
0
def Get_Cache(method, req = None):
    """
    Method used to call the api or retrieve the data in cache if the api has 
    already been called with the same parameters. I had to create this method 
    to fix some bugs caused by the api refusing too many calls. Parameters
    are the same than those passed to the api
    method   (req) - api method called
    req      (opt) - List or dictionnary of parameters to pass to the api
    """
    data = method
    if req:
     for param in req:
         data += '|' + str(param)
    if data in CACHE.keys():
        Output = CACHE[data]
    else:
        Output = api.query(method, req)
        CACHE[data] = Output
    return Output
Esempio n. 7
0
    def run_query(self):
        nicks = self.nicks.text()
        if ' ' in nicks:
            nicks = nicks.split(' ')
        response = query(
            nicks,
            self.channel.text(),
            self.datestart.dateTime().toPyDateTime(),
            self.dateend.dateTime().toPyDateTime()
        )
        results = loads(response.text)
        body = results['body']

        row_count = len(body)
        self.output.setColumnCount(3)
        self.output.setRowCount(row_count)
        headers = ['created_at', 'nick', 'message']
        self.output.setHorizontalHeaderLabels(headers)

        for row in range(row_count):  # add items from array to QTableWidget
            for column, title in enumerate(headers):
                self.output.setItem(row, column, QTableWidgetItem(body[row][title]))
Esempio n. 8
0
    ])

ITEM_LINKS = set(ITEM_LINKS)  # remove duplicates
for item_link in ITEM_LINKS:
    req = requests.get(item_link)
    if req.status_code != 200:
        continue

    parser = BeautifulSoup(req.text, 'html.parser')
    sku_element = parser.find('p', {'class': 'product-sku'})
    if not sku_element:
        continue

    sku_id = sku_element.text.strip('SKU ')
    # we now have the SKU, we can figure out where it's in stock
    stock = query(sku=sku_id)
    in_stock_locations = []
    for pair in stock:
        _id = pair['id']
        if _id not in id_map:
            continue

        quantity = pair['stock']
        if quantity <= 0:
            continue

        in_stock_locations.append(id_map[_id])

    if in_stock_locations:
        info = f'Available at: {", ".join(in_stock_locations)}'
    else:
Esempio n. 9
0
def search():
    '''
    提供api给调用,对传入的文本进行分词检索, 并返回结果
    '''

    # 获取需要分词的句子
    text = request.args.get('text', '')
    text = text.strip()
    print('question is:', text)

    # 传入参数为空抛出异常
    if not text: raise exceptions.NotAcceptable()

    # 获取分词
    jieba_seg, jieba_keywords = get_jieba_seg(text)
    fool_seg, fool_keywords = get_fool_seg(text)

    # 将分词结果中的词替换为数据库中存在的词
    jieba_seg, jieba_index = api.replace(jieba_seg)
    adding_keywords = [jieba_seg[index] for index in jieba_index]

    # 将关键词中的词也替换为数据库中存在的词, 并将分词中存在的关键词加入到关键词中
    jieba_keywords, jieba_index = api.replace(jieba_keywords)
    adding_keywords += jieba_keywords
    jieba_keywords = adding_keywords

    # 去重 保持有序
    unique = set()
    order_seg = []
    for seg in jieba_seg:
        if seg not in unique:
            order_seg.append(seg)
            unique.add(seg)
    jieba_seg = order_seg

    unique.clear()
    order_seg = []
    for seg in jieba_keywords:
        if seg not in unique:
            order_seg.append(seg)
            unique.add(seg)
    jieba_keywords = order_seg
    jieba_keywords = api.remove(jieba_keywords)

    # 将分词结果中的词替换为数据库中存在的词
    fool_seg, fool_index = api.replace(fool_seg)
    adding_keywords = [fool_seg[index] for index in fool_index]

    # 将关键词中的词也替换为数据库中存在的词, 并将分词中存在的关键词加入到关键词中
    fool_keywords, fool_index = api.replace(fool_keywords)
    adding_keywords += fool_keywords
    fool_keywords = adding_keywords

    # 去重 保持有序
    unique = set()
    order_seg = []
    for seg in fool_seg:
        if seg not in unique:
            order_seg.append(seg)
            unique.add(seg)
    fool_seg = order_seg

    unique.clear()
    order_seg = []
    for seg in fool_keywords:
        if seg not in unique:
            order_seg.append(seg)
            unique.add(seg)
    fool_keywords = order_seg

    # 使用es根据关键词进行检索
    jieba_result = api.query(jieba_keywords)
    fool_result = api.query(fool_keywords)

    # 构造结果作为api返回
    jieba_result = api.format(jieba_result)
    fool_result = api.format(fool_result)

    # 构造结果将结果存入数据库中
    jieba_content = [
        f'{index}.{data["phoneNum"]},{data["allName"]}\n'
        for index, data in enumerate(jieba_result, 1)
    ]

    fool_content = [
        f'{index}.{data["phoneNum"]},{data["allName"]}\n'
        for index, data in enumerate(fool_result, 1)
    ]

    jieba_seg = ','.join(jieba_seg)
    jieba_keywords = ','.join(jieba_keywords)
    fool_seg = ','.join(fool_seg)
    fool_keywords = ','.join(fool_keywords)
    jieba_content = ''.join(jieba_content)
    fool_content = ''.join(fool_content)

    # 需要存入数据库中的结果
    save = [
        text, jieba_seg, fool_seg, jieba_keywords, fool_keywords, fool_content,
        jieba_content
    ]

    Thread(target=api._save_query, args=(*save, )).start()
    # api._save_query(*save)

    return dumps({
        'jiebaSeg': jieba_seg,
        'jiebaKeywords': jieba_keywords,
        'foolSeg': fool_seg,
        'foolKeywords': fool_keywords,
        'jiebaSearchResult': jieba_result,
        'foolSearchResult': fool_result,
    })
Esempio n. 10
0
def dump(q):
    resp = api.query(q)
    print(dump_json(resp))
Esempio n. 11
0
def Execute_Action(header, entry, action):
    """
    Method to handle the action stage of an order. It creates the target sell
    order, check the prive level all the time and launche the stoploss process
    if the price stays below the stoploss level defined in the order more that
    [DEFAULT_COUNTDOWN] times. Stoploss cancels the target order sell and sells
    the coins at the market price
    header    (req) - Header xml object from the xml ordee config file
    entry     (req) - Entry xml object from the xml ordee config file
    action    (req) - Action xml object from the xml ordee config file
    """
    pair = header.get("pair")
    if action.get("status") in (None, "") :
        action.set("status", "init")

    if (action.get("status") == "init"):
        Log("status - init")
        if (entry.get("price") == None):
            action.set("status", "ready")
        else:
            price = float(entry.get("price"))
            target = float(header.get("target"))
            stoploss = float(header.get("stoploss"))
            target_price = price*(1+target)+0.00000001
            stoploss_price = price*(1-stoploss)+0.00000001
            action.set("stoploss", '{:.8f}'.format(stoploss_price))
            action.set("target", '{:.8f}'.format(target_price))
            action.set("status", "sell")

    if (action.get("status") == "sell"):
        Log("status - sell order")
        price = float(entry.get("price"))
        target = float(action.get("target"))
        stoploss = float(action.get("stoploss"))
        Output = Get_Cache("GetMarket", [pair])
        pairid = Output["Data"]["TradePairId"]
        tradetype = 'Sell'
        currency = pair[:pair.index('_')]
        Output = api.query("GetBalance", {'Currency':currency})
        amount = float(Output["Data"][0]["Total"])
        Log(tradetype + " " + pair + " " + '{:.8f}'.format(amount) + 
            " at " + '{:.8f}'.format(target))
        Output = api.query("SubmitTrade", {'TradePairId':pairid, 
                                           'Type':tradetype, 
                                           'Rate':target, 'Amount':amount})
        time.sleep(15)
        has_buy_orders = Check_Buy_Orders(pair)
        if not has_buy_orders :
            action.set("status", "active")

    if (action.get("status") == "active"):
        Log("status - active")
        pair = header.get("pair")
        currency = pair[:pair.index('_')]        
        Output = Get_Cache("GetMarket", [pair])            
        pairid = Output["Data"]["TradePairId"]
        refprice = float(Output["Data"]["LastPrice"])
        bidprice = float(Output["Data"]["BidPrice"])
        price = float(entry.get("price"))
        target = float(action.get("target"))
        stoploss = float(action.get("stoploss"))
        spacer = max(0, min(22, int(22*(refprice-stoploss)/(price-stoploss))))
        spacer += max(0, min(17, int(17*(refprice-price)/(target-price))))
        Log(" "*spacer + "| last:"+'{:.8f}'.format(refprice))
        Log("| stoploss:" + '{:.8f}'.format(stoploss) + 
            " | buy:"+'{:.8f}'.format(price) + 
            " | target:"+'{:.8f}'.format(target)+" |")
        if (refprice <= stoploss):
            if (action.get("countdown") == None):
                action.set("countdown", str(DEFAULT_COUNTDOWN))
            countdown = int(action.get("countdown"))
            Log("stoploss countdown:" + str(countdown))
            if (countdown <= 0):
                Log("stoploss process launched")
                #check minimum trade
                minimumtradepair = Get_Minimum_Trade_Amount(pair)
                minimumamount = minimumtradepair/bidprice
                minimumamount = float('{:.8f}'.format(minimumamount))
                Output = api.query("GetBalance", {'Currency':currency})
                amount = float(Output["Data"][0]["Total"])
                available = float(Output["Data"][0]["Available"])
                Log("bidprice*amount:"+'{:.8f}'.format(bidprice*amount))
                Log("available:"+'{:.8f}'.format(available))
                if (bidprice*amount > minimumtradepair):
                    Output = api.query("CancelTrade", {'Type':'TradePair', 
                                                       'TradePairId':pairid})
                    Log(Output)
                    time.sleep(7)
                else:
                    Log("trade not canceled")
                Output = api.query("GetBalance", {'Currency':currency})
                available = float(Output["Data"][0]["Available"])
                print ("available:"+'{:.8f}'.format(available))
                if (available>minimumamount):
                    Output = api.query("SubmitTrade", {'TradePairId':pairid, 
                                                       'Type':'Sell', 
                                                       'Rate':bidprice, 
                                                       'Amount':available})
                    Log("Exit trade sent")
                    Log(Output)
                    time.sleep(15)
                else:
                    Log("trade not submitted")
            else:
                action.set("countdown", str(countdown-1))
        else:
            if (action.get("countdown") != None):
                del action.attrib["countdown"]

        price = float(entry.get("price"))
        baseamount = float(header.get("amount"))
        amount = baseamount/price
        Output = api.query("GetBalance", {'Currency':currency})
        if (Output["Data"] != []):
            total = float(Output["Data"][0]["Total"])
            if (total==0.0):
                    action.set("status", "ready")
                    action.set("date", str(datetime.datetime.now()))
            Log("check amount {:.8f}".format(total))
        Log("           | {:.8f}".format(amount))
Esempio n. 12
0
def Execute_Entry(header, entry):
    """
    Method to handle the entry stage of an order. It creates the buy order and
    check that it is correctly filled. Parameters as follows:
    header    (req) - Header xml object from the xml ordee config file
    entry     (req) - Entry xml object from the xml ordee config file
    """
    pair = header.get("pair")
    stoploss = float(header.get("stoploss"))
    if entry.get("status") in (None, "") :
        entry.set("status", "init")

    if (entry.get("status") == "init"):
        Log("status - init")
        Output = api.query("GetMarket", [pair])
        pairid = Output["Data"]["TradePairId"]
        refprice = float(Output["Data"]["AskPrice"])
        Log("ref price: "+'{:.8f}'.format(refprice))
        baseamount = float(header.get("amount"))
        Log("base amount : "+'{:.8f}'.format(baseamount))
        amount = baseamount/refprice
        Log("amount : "+'{:.8f}'.format(amount))
        currency = pair[:pair.index('_')]
        Output = api.query("GetBalance", {'Currency':currency})
        if (Output["Data"] == None):
            already = float(0.0)
        else:
            already = float(Output["Data"][0]["Total"])
        Log("already : "+'{:.8f}'.format(already))
        amount = amount - already
        minimumtradepair = Get_Minimum_Trade_Amount(pair)*(1-stoploss)
        minimumamount = float('{:.8f}'.format(minimumtradepair/refprice))
        Log("minimumamount: "+'{:.8f}'.format(minimumamount))
        if(amount<minimumamount):
            entry.set("status", "ready")
            entry.set("message", "amount too small")
            Log("....amount too small")
        else: 
            tradetype = 'Buy'
            Output = api.query("SubmitTrade", {'TradePairId':pairid, 
                                               'Type':tradetype, 
                                               'Rate':refprice, 
                                               'Amount':amount})
            Log ("Buy ID : "+ str(pairid) + "  " + "{:.8f}".format(amount)+ 
                 " @ " + '{:.8f}'.format(refprice) + " = " + 
                 "{:.8f}".format(baseamount))
            Log(Output)
            entry.set("price", '{:.8f}'.format(refprice))
            entry.set("status", "sent")
            entry.set("countdown", str(DEFAULT_COUNTDOWN))
            Log ("sent")
            time.sleep(8)

    if (entry.get("status") == "sent"):
        Log("status - sent")
        countdown = int(entry.get("countdown"))
        has_buy_orders = Check_Buy_Orders(pair)
        if not has_buy_orders :
            minimumtradepair = Get_Minimum_Trade_Amount(pair)*(1-stoploss)
            price = float(entry.get("price"))
            minimumamount = float('{:.8f}'.format(minimumtradepair/price))
            Log("minimumamount: "+'{:.8f}'.format(minimumamount))
            currency = pair[:pair.index('_')]
            Output = api.query("GetBalance", {'Currency':currency})
            if (Output["Data"] == None):
                available = float(0.0)
            else:
                available = float(Output["Data"][0]["Available"])
            Log("available: "+'{:.8f}'.format(available))
            countdown = 0
            if(available>=minimumamount):
                countdown = DEFAULT_COUNTDOWN
                Log("filled")
                entry.set("status", "ready")
        Log("countdown : "+str(countdown))
        if (countdown > 0):
            countdown = countdown - 1
            entry.set("countdown", str(countdown))
        else:
            Log("retry")
            pair = header.get("pair")
            Output = Get_Cache("GetMarket", [pair])
            pairid = Output["Data"]["TradePairId"]
            Output = api.query("CancelTrade", {'Type':'TradePair', 
                                               'TradePairId':pairid})
            Log(Output)
            Log("Cancel Trade")
            entry.set("status", "init")
Esempio n. 13
0
    if not postid or not company:
        print('\nconfig.py文件错误!', i, '\n')
        exit(0)

    # 如果是一个新的数据
    if not data.get(postid):
        data[postid] = {}
        data[postid]['last_time'] = '0'

    # 状态已经是已签收
    if data.get(postid).get('state', '0') == '3':
        data_n[postid] = data[postid]
        continue

    result = query(company, postid)

    if not result:
        print('获取api数据出错!', i, '\n')
        continue

    last_time = result.get('data')[0].get('time')
    context = result.get('data')[0].get('context')
    full = result.get('data')

    if last_time != data.get(postid).get('last_time'):
        data[postid]['last_time'] = last_time
        data[postid]['state'] = result.get('state')
        sc_noti(company, postid, context, full)

    # 写入数据,舍弃旧数据
Esempio n. 14
0
def get_submission_comment_ids(submission_id: str) -> List[str]:
	url = f"https://api.pushshift.io/reddit/submission/comment_ids/{submission_id}"
	comment_ids = api.query(url)
	return comment_ids