Esempio n. 1
0
def extract_stock2(code):
    fields = {
              'sjl' : '592920',
              'zsz' : '3541450',
              'ltsz' : '3475914',
              }
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()
    url = "http://d.10jqka.com.cn/v2/realhead/hs_%s/last.js"%(code)
    data = utils.fetch_url(url)
    data = re.sub(r'quotebridge.*?\((.*)\)', r'\1', data)
    jo = json.loads(data)['items']
    if jo is not None:
        try:
            jo['3541450'] = "%.2f"%(float(jo['3541450']) / 100000000)
            jo['3475914'] = "%.2f"%(float(jo['3475914']) / 100000000)
            keys = fields.keys()
            vals = ["'"+ (jo[fields[k]] or '')+"'" for k in keys]
            keys.append('code')
            vals.append("'%s'"%(code))
            updates = [keys[i]+"="+vals[i] for i in range(0, len(keys))]
        except:
            utils.print_with_time("url=%s"%(url))
            traceback.print_exc()
            return
         
        sql = "INSERT INTO stock (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s"%(', '.join(keys), ', '.join(vals), ', '.join(updates))
#         print sql
        cursor.execute(sql)
        cnx.commit()
    cursor.close()
    cnx.close()
Esempio n. 2
0
def extract_stock(code):
    fields = {
              'code' : 'stockcode',
              'name' : 'stockname',
              'fieldcode' : 'fieldcode',
              'fieldname' : 'fieldname',
              'fieldjp' : 'fieldjp',
              'syl' : 'syl',
              'xj' : 'xj',
              }
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()
    url = "http://stockpage.10jqka.com.cn/spService/%s/Header/realHeader"%(code)
    jo = utils.fetch_json(url)
    if jo is not None:
        try:
            keys = fields.keys()
            vals = ["'"+ (jo[fields[k]] or '')+"'" for k in keys]
            updates = [keys[i]+"="+vals[i] for i in range(0, len(keys))]
        except:
            utils.print_with_time("url=%s"%(url))
            traceback.print_exc()
            return
        
        sql = "INSERT INTO stock (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s"%(', '.join(keys), ', '.join(vals), ', '.join(updates))
#         print sql
        cursor.execute(sql)
        cnx.commit()
    cursor.close()
    cnx.close()
Esempio n. 3
0
def main():
    parser = argparse.ArgumentParser(description='stock extractor')
    parser.add_argument('-a', '--all', required=False, action='store_true', help='refresh all info')
    parser.add_argument('-s', '--stock_codes', type=str, nargs='*', help='stock codees')
    parser.add_argument('-m', '--mystock', required=False, type=int, default=0, help='mystock type')
    res = parser.parse_args()
    if res.stock_codes is not None:
        for i in range(len(res.stock_codes)):
            code = res.stock_codes[i]
            if res.all:
                extract_code(code)
            else:
                extract_stock(code)
                utils.print_with_time("extract_stock %s"%(code))
                time.sleep(settings.sleepTime)
    elif res.mystock is not None:
        cnx = mysqllib.get_connection()
        cursor = cnx.cursor()
        sql = "SELECT code from mystock"
        if res.mystock <> 0:
            sql = sql + " where `type`=%d"%(res.mystock)
        cursor.execute(sql)
        for (code,) in cursor:
            if res.all:
                extract_code(code)
            else:
                extract_stock(code)
                utils.print_with_time("extract_stock %s"%(code))
                time.sleep(settings.sleepTime)
    else:
        parser.print_help()
Esempio n. 4
0
def main():
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()
    url = "http://www.jisilu.cn/jisiludata/newstock.php?qtype=apply"
    jo = utils.fetch_json(url)
    for row in jo['rows']:
        cell = row['cell']
        name = row['id']
        sid = cell['stock_cd']
        apply_dt = transform_date(cell['apply_dt'])
        utils.print_with_time("%s %s %s" % (sid, name, apply_dt))

        try:
            keys = ['code', 'name', 'date']
            keys = ["`" + f + "`" for f in keys]
            vals = [sid, name, apply_dt]
            vals = ["'" + f + "'" for f in vals]
            updates = [keys[i] + "=" + vals[i] for i in range(0, len(keys))]
        except:
            traceback.print_exc()
            return

        sql = "INSERT INTO new_stock (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s" % (
            ', '.join(keys), ', '.join(vals), ', '.join(updates))
        #         print sql
        cursor.execute(sql)
        cnx.commit()
    cursor.close()
    cnx.close()
Esempio n. 5
0
def st_hangye(hangye):
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()
    sql = "SELECT code from stock WHERE fieldjp='%s'" % (hangye)
    cursor.execute(sql)
    for (code, ) in cursor:
        st_stock(code)
Esempio n. 6
0
def st_stock(code, verbose, max_pe=1000000):
    code = code.strip()
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()
    year = datetime.datetime.today().year - settings.avgmgsyYear
    sql = "SELECT AVG(jbmgsy) as avg_jbmgsy from stock_year WHERE `year`>=%d and code=%s GROUP BY code" % (
        year, code)
    cursor.execute(sql)
    for (tp, ) in cursor:
        avg_jbmgsy = tp
        break
    sql = "SELECT mgjzc from stock_year WHERE code=%s ORDER BY `year` DESC LIMIT 1" % (
        code)
    cursor.execute(sql)
    for (tp, ) in cursor:
        mgjzc = tp
        break
    sql = "SELECT name, fieldjp, xj, syl from stock WHERE code=%s" % (code)
    cursor.execute(sql)
    for (name, fieldjp, xj, syl) in cursor:
        #         name = utils.pad_str(name, 10)
        try:
            if float(syl) == 0:
                roe = 0
            else:
                roe = float(xj) / float(mgjzc) / syl
            if float(syl) <= max_pe and float(syl) > 0 and float(xj) / float(
                    avg_jbmgsy) < max_pe and float(xj) / float(avg_jbmgsy) > 0:
                utils.print_with_time(
                    u"%s\t%s\t%s\t现价=%s\t平均每股收益=%.3f\t每股净资产=%s\tPE=%.2f\tPE(avg)=%.2f\tPB=%.2f\tROE=%.2f"
                    % (code, name, fieldjp, xj, float(avg_jbmgsy), mgjzc,
                       float(syl), float(xj) / float(avg_jbmgsy),
                       float(xj) / float(mgjzc), roe))
        except:
            if max_pe > 1000:
                utils.print_with_time(
                    u"%s\t%s\t%s\t现价=%s\t平均每股收益=%.3f\t每股净资产=%s\tPE=%.2f" %
                    (code, name, fieldjp, xj, float(avg_jbmgsy), mgjzc,
                     float(syl)))
    if verbose:
        fields = ['year', 'jbmgsy', 'mgjzc', 'mgxjl']
        sql = "SELECT %s from stock_year WHERE `year`>=%d and code=%s order by `year` desc" % (
            ','.join(fields), year, code)
        cursor.execute(sql)
        utils.print_with_time("\t".join(fields))
        for row in cursor:
            #             print "\t".join([str(i) for i in row])
            utils.print_with_time("\t".join([str(i) for i in row]))
Esempio n. 7
0
def main():
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()

    today = datetime.datetime.today()
    ts = today.strftime("%Y-%m-%d")
    sql = "select code, name from new_stock where `date`='%s'" % (ts)
    cursor.execute(sql)
    stocks = []
    for (code, name) in cursor:
        stocks.append("%s-%s" % (code, name))
    if len(stocks) > 0:
        ns = "申购新股:%s" % (' '.join([f.encode('utf-8') for f in stocks]))
        es = EmailSender()
        es.send_mail("申购新股", ns)
        es.close()
        utils.print_with_time(ns.decode('utf-8'))
    utils.print_with_time("Done")

    cursor.close()
    cnx.close()
Esempio n. 8
0
def extract_year(code):
    indexs = {
              1 : 'jbmgsy',
              7 : 'mgjzc',
              13 : 'mgxjl',
              }
    cnx = mysqllib.get_connection()
    cursor = cnx.cursor()
    url = "http://stockpage.10jqka.com.cn/basic/%s/main.txt"%(code)
    jo = utils.fetch_json(url)
#     print data; return
    if jo is not None:
        dmap = {}
        for index in indexs.keys():
            title = jo['title'][index]
            title = '_'.join(title)
            years = jo['year'][0]
            values = jo['year'][index]
            for y in range(0, len(years)):
                year = years[y]
                value = values[y]
                if not dmap.has_key(year):
                    dmap[year] = {}
                dmap[year][indexs[index]] = value
    #             print '%s\t%s\t%s'%(title, year, value)
        for year, ydata in dmap.items():
            fields = indexs.values()
            values = [ydata[f] or '0' for f in fields]
            updates = [fields[i]+"="+values[i] for i in range(0, len(fields))]
    #         print fields
    #         print values
            sql = "INSERT INTO stock_year (code, year, %s) VALUES ('%s', %s, %s) ON DUPLICATE KEY UPDATE %s"%(', '.join(fields), code, year, ', '.join(values), ', '.join(updates))
    #         print sql
            cursor.execute(sql)
        cnx.commit()
    cursor.close()
    cnx.close()
Esempio n. 9
0
def main():
    parser = argparse.ArgumentParser(description='stock statistics')
    parser.add_argument('-r',
                        '--refresh',
                        required=False,
                        action='store_true',
                        help='refresh stock price')
    parser.add_argument('-v',
                        '--verbose',
                        required=False,
                        action='store_true',
                        help='show verbose')
    parser.add_argument('-s',
                        '--stock_codes',
                        type=str,
                        nargs='*',
                        help='stock codees')
    parser.add_argument('-y', '--hangye', type=str, help='hangye jp')
    parser.add_argument('-f',
                        '--filter',
                        required=False,
                        type=float,
                        help='filter stock by pe')
    parser.add_argument('-m',
                        '--mystock',
                        required=False,
                        type=int,
                        default=0,
                        help='mystock type')
    res = parser.parse_args()
    if res.stock_codes is not None:
        for i in range(len(res.stock_codes)):
            code = res.stock_codes[i]
            if res.refresh:
                if i > 0:
                    time.sleep(settings.sleepTime)
                extractor.extract_stock(code)
            st_stock(code, res.verbose)
    elif res.hangye is not None:
        cnx = mysqllib.get_connection()
        cursor = cnx.cursor()
        sql = "SELECT code from stock WHERE fieldjp='%s'" % (res.hangye)
        cursor.execute(sql)
        for (code, ) in cursor:
            if res.refresh:
                extractor.extract_stock(code)
            st_stock(code, res.verbose)
            if res.refresh:
                time.sleep(settings.sleepTime)
    elif res.filter:
        cnx = mysqllib.get_connection()
        cursor = cnx.cursor()
        sql = "SELECT code from stock order by fieldcode"
        cursor.execute(sql)
        for (code, ) in cursor:
            st_stock(code, res.verbose, res.filter)
    elif res.mystock is not None:
        cnx = mysqllib.get_connection()
        cursor = cnx.cursor()
        sql = "SELECT code from mystock"
        if res.mystock <> 0:
            sql = sql + " where `type`=%d" % (res.mystock)
        cursor.execute(sql)
        for (code, ) in cursor:
            if res.refresh:
                extractor.extract_stock(code)
            st_stock(code, res.verbose)
            if res.refresh:
                time.sleep(settings.sleepTime)
    else:
        parser.print_help()