F10资料-同花顺(下载 c#)

Published

F10资料-同花顺

using System;
using MyTable;
namespace SocketClientBase.Http.F10
{

    /// <summary>
    /// 下载板块与股票代码  by freeman 20190625
    /// </summary>
    public class DownloadBlock : DownloadBaseF10
    {
        int m_nParseIndex = 0;
        int m_nTableBlockRow = 0;
        _Table m_pTableBlock = new _Table();
        _Table m_pTableStock = new _Table();
        int m_nPage = 1;
        public DownloadBlock()
            : base("板块与股票代码")
        {
            m_pTableBlock.AddCol("分类方式", typeof(string), 32);
            m_pTableBlock.AddCol("分类名称", typeof(string), 32);
            m_pTableBlock.AddCol("连接对象", typeof(string), 256);

            m_pTableStock.AddCol("分类方式", typeof(string), 32);
            m_pTableStock.AddCol("分类名称", typeof(string), 32);
            m_pTableStock.AddCol("连接对象", typeof(string), 256);
            m_pTableStock.AddCol("股票代码", typeof(string), 16);
            m_pTableStock.AddCol("股票名称", typeof(string), 16);

            _SetWebAddr("stockpage.10jqka.com.cn");

        }
        override protected bool OnSetObject()
        {
            string sObject = "";
            if(m_nParseIndex == 0)
                sObject = "/";
            else if (m_nParseIndex == 1)
            {                
                sObject = "/ajax/stockindex.php";
                RequestSetMethod("POST");
                RequestSetPost(m_pTableBlock.GetValueByString(m_nTableBlockRow, 2) + "," + m_nPage.ToString ());//1145,1");
            }
            RequestSetObject(sObject);
            RequestDefaultValue();
            RequestSend();

            return true;
        }
        override protected bool OnBegin()
        {
            SetRemoteAddr("stockpage.10jqka.com.cn");
            m_nTableBlockRow = 0;
            m_nParseIndex = 0;
            m_pTableBlock.ClearRow();
            m_pTableStock.ClearRow();

            _ResetValidCount();

            SetInfo("下载板块与股票代码...");
            return true;
        }
        override protected bool OnEnd()
        {
            ResponseReplace("\t", "");
            ResponseReplace("\r", "");
            ResponseReplace("\n", "");
            //RecvWriteData("recv01.cpp");
            int nValidCount;
            if (m_nParseIndex == 0)
            {
                nValidCount = _HangYe();
                if (nValidCount < 0)
                {
                    SetInfo("下载板块错误");
                    return true;
                }
                _AddValidCount(nValidCount);
                nValidCount = _GaiNian();
                if (nValidCount < 0)
                {
                    SetInfo("下载概念错误");
                    return true;
                }
                _AddValidCount(nValidCount);
                _WriteContent("板块与股票代码", ref m_pTableBlock);
                m_nParseIndex++;
            }
            else if (m_nParseIndex == 1)
            {
                nValidCount = _ChengFenGuPiao();
                if (nValidCount >= 0)
                {
                    _AddValidCount(nValidCount);
                    m_nPage++;
                }
                else
                {
                    m_nPage = 1;
                    m_nTableBlockRow++;
                }
                if (m_nTableBlockRow >= m_pTableBlock.GetRowCount())
                {
                    _WriteContent("板块成分股票", ref m_pTableStock);
                    m_pTableStock.Sort(3, 0);
                    _WriteContent("板块成分股票按代码排序", ref m_pTableStock);
                    _ParseOnlyCode();

                    int nNowDate = DateTime.Now.Year * 10000 + DateTime.Now.Month * 100 + DateTime.Now.Day;
                    SetInfo("完成," + nNowDate.ToString() + "," + GetValidCount().ToString());
                    return true;
                }
                SetInfo(m_pTableBlock.GetValueByString(m_nTableBlockRow, 1) + "," + (m_nTableBlockRow + 1).ToString() + " of " + m_pTableBlock.GetRowCount().ToString() + ",页:" + m_nPage.ToString() + "," + GetValidCount ().ToString ());
            }
            return false;
        }
        override protected void OnStop()
        {
        }
        //////////////////////////
        int _HangYe()
        {
            string sBlockName, sObject;
            int nStartPos, nEndPos, nRecvLen, i, j, nRow;
            nStartPos = ResponseGetHeadLength();
            nRecvLen = RecvGetLen();
            nStartPos = RecvFind(nStartPos, nRecvLen, "hytradecode", true);
            if (nStartPos < 0)
                return -1;
            i = RecvFind(nStartPos, nRecvLen, "<table>", true);
            if ((i + 128) < nStartPos || i < 0)
                return -1;
            nStartPos = i;
            nEndPos = RecvFind(nStartPos, nRecvLen, "</table>", true);
            if (nEndPos < 0)
                return -1;
            int nValidCount = 0;
            i = nStartPos;
            while (true)
            {
                i = RecvFind(i, nEndPos, "keyid=\"", true);
                if (i < 0)
                    break;
                i += 7;
                j = RecvFind(i, nEndPos, "\"", true);
                if ((i + 16) < j)
                    return -1;
                sObject = RecvGetString(i, j - i);
                if (!(sObject[0] >= '0' && sObject[0] <= '9'))
                {
                    i = j;
                    continue;
                }
                i = j;
                i = RecvFind(i, nEndPos, ">", true);
                i += 1;
                j = RecvFind(i, nEndPos, "<", true);
                if ((i + 32) < j)
                    return -1;
                sBlockName = RecvGetString(i, j - i);
                i = j;
                nRow = m_pTableBlock.AddRow();
                m_pTableBlock.SetValue(nRow, 0, "同花顺行业");
                m_pTableBlock.SetValue(nRow, 1, sBlockName);
                sObject = "type=getflush&str=" + sObject;
                m_pTableBlock.SetValue(nRow, 2, sObject);

                nValidCount++;
            }
            return nValidCount;
        }
        int _GaiNian()
        {
            string sBlockName, sObject;
            int nStartPos, nEndPos, nRecvLen, i, j, nRow;
            nStartPos = ResponseGetHeadLength();
            nRecvLen = RecvGetLen();
            nStartPos = RecvFind(nStartPos, nRecvLen, "gntradecode", true);
            if (nStartPos < 0)
                return -1;
            i = RecvFind(nStartPos, nRecvLen, "<table>", true);
            if ((i + 128) < nStartPos || i < 0)
                return -1;
            nStartPos = i;
            nEndPos = RecvFind(nStartPos, nRecvLen, "</table>", true);
            if (nEndPos < 0)
                return -1;
            int nValidCount = 0;
            i = nStartPos;
            while (true)
            {
                i = RecvFind(i, nEndPos, "keyid=\"", true);
                if (i < 0)
                    break;
                i += 7;
                j = RecvFind(i, nEndPos, "\"", true);
                if ((i + 16) < j)
                    return -1;
                sObject = RecvGetString(i, j - i);
                if (!(sObject[0] >= '0' && sObject[0] <= '9'))
                {
                    i = j;
                    continue;
                }
                i = j;
                i = RecvFind(i, nEndPos, ">", true);
                i += 1;
                j = RecvFind(i, nEndPos, "<", true);
                if ((i + 32) < j)
                    return -1;
                sBlockName = RecvGetString(i, j - i);
                i = j;
                nRow = m_pTableBlock.AddRow();
                m_pTableBlock.SetValue(nRow, 0, "同花顺概念");
                m_pTableBlock.SetValue(nRow, 1, sBlockName);
                sObject = "type=getconcept&str=" + sObject;
                m_pTableBlock.SetValue(nRow, 2, sObject);

                nValidCount++;
            }
            return nValidCount;
        }
        int _ChengFenGuPiao()
        {
            //RecvWriteData("recv02.cpp");
            string s = _Utf8ToGB2312(ResponseGetContent());
            if(s.IndexOf("codename") < 0)
                return -1;
            if(s.IndexOf("stockcode") < 0)
                return -1;
            string sBlockName, sBlockType, sObject, sStockName, sStockCode;

            sBlockType = m_pTableBlock.GetValueByString(m_nTableBlockRow, 0);
            sBlockName = m_pTableBlock.GetValueByString(m_nTableBlockRow, 1);

            int nValidCount = 0;
            int i, j;
            i = 0;
            while (true)
            {
                i = s.IndexOf("codename", i);
                if (i < 0)
                    break;
                i += 8;
                if (i >= s.Length)
                    break;
                while (i < s.Length)
                {
                    if (!(s[i] == '\"' || s[i] == ':' || s[i] == ' ' || s[i] == ',' || s[i] == '}' || s[i] == ']'))
                        break;
                    i++;
                }
                j = i;
                while (i < s.Length)
                {
                    if ((s[i] == '\"' || s[i] == ':' || s[i] == ' ' || s[i] == ',' || s[i] == '}' || s[i] == ']'))
                        break;
                    i++;
                }
                sStockName = s.Substring(j, i - j);
                i = s.IndexOf("stockcode", i);
                if (i < 0)
                    break;
                i += 9;
                while (i < s.Length)
                {
                    if (!(s[i] == '\"' || s[i] == ':' || s[i] == ' ' || s[i] == ',' || s[i] == '}' || s[i] == ']'))
                        break;
                    i++;
                }
                j = i;
                while (i < s.Length)
                {
                    if ((s[i] == '\"' || s[i] == ':' || s[i] == ' ' || s[i] == ',' || s[i] == '}' || s[i] == ']'))
                        break;
                    i++;
                }
                sStockCode = s.Substring(j, i - j);
                sObject = "/" + sStockCode + "/";
                int nRow = m_pTableStock.AddRow();
                m_pTableStock.SetValue(nRow, 0, sBlockType);
                m_pTableStock.SetValue(nRow, 1, sBlockName);
                m_pTableStock.SetValue(nRow, 2, sObject);
                m_pTableStock.SetValue(nRow, 3, sStockCode);
                m_pTableStock.SetValue(nRow, 4, sStockName);

                nValidCount++;

            }
            //_WriteFile("recv04.cpp", ref s, false);
            return nValidCount;
        }
        void _ParseOnlyCode()
        {

            string sCode, sObject, sCodeBak, sName;
            int i;
            sCodeBak = "";
            for (i = 0; i < m_pTableStock.GetRowCount(); i++)
            {
                sCode = m_pTableStock.GetValueByString(i, 3);
                if (!(sCode[0] >= '0' && sCode[0] <= '9'))
                    continue;
                if (sCode != sCodeBak)
                {
                    sCodeBak = sCode;
                    sObject = m_pTableStock.GetValueByString(i, 2);
                    sName = m_pTableStock.GetValueByString(i, 4);
                    _AddCode(sCode, sName, sObject);
                }
            }
            _WriteCode();
        }
    }
}