示例#1
0
        public async Task <ArchiveResponse> DownloadArchive(string docid, DocumentType type, int retry)
        {
            ArchiveResponse response = new ArchiveResponse();
            string          url      = string.Format("/api/{0}/documents/{1}?type={2}", Version, docid, (int)type);
            int             i        = 0;

            do
            {
                if (i > 0)
                {
                    SaveLog($"  retry[{i}] {docid} {type}");
                    Debug.Write($"retry Download[{i}] ");
                    await Task.Delay(2000);
                }
                try {
                    debug.ProgramCodeInfo.SetDebugQueue();
                    using (HttpResponseMessage res = await client.GetAsync(url)) {
                        debug.ProgramCodeInfo.SetDebugQueue();
                        string filename = res.Content.Headers.ContentDisposition.FileName.Replace("\"", "");
                        //string filename = $"{docid}_{(int)type}";
                        System.Net.Http.Headers.MediaTypeHeaderValue contenttype = res.Content.Headers.ContentType;
                        if (filename == "404.json")
                        {
                            filename = url;
                        }
#pragma warning disable CS4014
                        SaveLog(GetLog(res.StatusCode, RequestType.Archive, contenttype, filename));
#pragma warning restore CS4014
                        using (Stream stream = await res.Content.ReadAsStreamAsync()) {
                            using (MemoryStream ms = new MemoryStream()) {
                                stream.CopyTo(ms);
                                byte[] buffer = ms.ToArray();
                                stream.Flush();
                                response.Update(buffer, res.StatusCode, filename, contenttype);
                                debug.ProgramCodeInfo.SetDebugQueue();
                                return(response);
                            }
                        }
                    }
                } catch (TaskCanceledException ex) {
                    response.Update(ex);
#pragma warning disable CS4014
                    SaveLog(GetLog(response));
#pragma warning restore CS4014
                    debug.ProgramCodeInfo.SetDebugQueue();
                } catch (Exception ex) {
                    response.Update(ex);
#pragma warning disable CS4014
                    SaveLog(GetLog(response));
#pragma warning restore CS4014
                    debug.ProgramCodeInfo.SetDebugQueue();
                    return(response);
                }
                i++;
            } while (i <= retry);
            return(response);
        }
示例#2
0
        private async void DgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ("pdfFlag,attachDocFlag,englishDocFlag".Contains((sender as DataGridView).Columns[e.ColumnIndex].Name))
            {
                object value = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (value != null && value.ToString() == "1")
                {
                    DateTime date     = DateTime.Parse(dgvList.Rows[e.RowIndex].Cells["date"].Value.ToString());
                    int      id       = int.Parse(disclosures.DvDocuments[e.RowIndex]["id"].ToString());
                    string   docid    = disclosures.DvDocuments[e.RowIndex]["docID"].ToString();
                    string[] fields   = new string[] { "xbrlFlag", "pdfFlag", "attachDocFlag", "englishDocFlag" };
                    int      index    = Array.IndexOf(fields, dgvList.Columns[e.ColumnIndex].Name);
                    string   filepath = string.Format(@"{0}\Documents\{1}\{2}_{3}.{4}", setting.Directory, date.Year, docid, index + 1, index == 1 ? "pdf" : "zip");
                    string   field    = fields[index].Replace("Doc", "").Replace("Flag", "");
                    string   download = dgvList.CurrentRow.Cells[field].Value.ToString();
                    if (download == "404")
                    {
                        StatusLabel1.Text = string.Format("{0:HH:mm:ss} {1}[{2}] 404[Not Found] in table", DateTime.Now, docid, field);
                        return;
                    }
                    //int retry = 1;
                    ArchiveResponse response = await disclosures.ChangeDocumentAsync(id, docid, (RequestDocument.DocumentType) Enum.ToObject(typeof(RequestDocument.DocumentType), index + 1), setting.Retry);

                    dgvList.Refresh();
                    if (response != null)
                    {
                        StatusLabel1.Text = string.Format("{1:HH:mm:ss} 書類取得API status[{0}] {2}ダウンロード {3}", response.EdinetStatusCode.Message, DateTime.Now, index == 1 ? "pdf" : "xbrl", response.Filename);
                        //filepath = string.Format(@"{0}\Documents\{1}\{2}", setting.Directory, year, disclosures.ArchiveResult.Name);
                    }
                    else
                    {
                        StatusLabel1.Text = DateTime.Now.ToString("HH:mm:ss") + " ダウンロード済み書類 ";
                        //filepath = disclosures.ArchiveResult.Name;
                    }
                    //MessageBox.Show(e.RowIndex.ToString() + "行 " + e.ColumnIndex + "列 " + dgvList.Columns[e.ColumnIndex].Name);
                    if (dgvList.Columns[e.ColumnIndex].Name == "pdfFlag")
                    {
                        if (File.Exists(filepath))
                        {
                            string url = string.Format("file://{0}#toolbar=0&navpanes=0", filepath.Replace("\\", "/"));
                            browser.Navigate(url);
                        }
                    }
                    else
                    {
                    }
                }
            }
        }
示例#3
0
        public async Task <HttpResponseMessage> RequestDownload(string docid, DocumentType type)
        {
            string url = string.Format("/api/{0}/documents/{1}?type={2}", Version, docid, (int)type);

            try {
                return(await client.GetAsync(url));
            } catch (Exception ex) {
                ArchiveResponse response = new ArchiveResponse();
                response.Update(ex);
#pragma warning disable CS4014
                SaveLog(GetLog(response));
#pragma warning restore CS4014
                return(null);
            }
        }
示例#4
0
        private string GetLog(ArchiveResponse response)
        {
            StringBuilder sb = new StringBuilder();

            try {
                if (response.Exception != null)
                {
                    return(response.Exception.Message);
                }
                sb.Append(GetLog(response.HttpStatusCode, RequestType.Archive, response.HeaderContentType, response.Filename, 0, response.Exception));
                if (response.Exception == null)
                {
                    sb.AppendFormat("\t{0}", response.Filename);
                }
            } catch (Exception ex) {
                throw(ex);
            }
            return(sb.ToString());
        }
示例#5
0
        private async void DgvList_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgvList.CurrentCell == null || IsReading | disclosures.DvDocuments.Count == 0 || disclosures.DvDocuments[dgvList.CurrentCell.RowIndex]["id"].ToString() == "")
            {
                return;
            }
            StatusLabel1.Text = "";
            disclosures.TableContents.Clear();
            currentRow2 = -1;
            if (!browser.DocumentText.Contains("metadata"))
            {
                browser.DocumentText = "";
            }
            if (!IsReading & dgvList.CurrentCell != null)
            {
                if (dgvList.CurrentCell.RowIndex != currentRow1)
                {
                    currentRow1 = dgvList.CurrentCell.RowIndex;
                    if (disclosures.DvContents.Count > 0)
                    {
                        disclosures.DvContents.Table.Rows.Clear();
                    }
                    ;
                    if (disclosures.TableElements.Rows.Count > 0)
                    {
                        disclosures.TableElements.Rows.Clear();
                    }
                    ;
                }
                string docid = dgvList.CurrentRow.Cells["docID"].Value.ToString();
                int    id    = int.Parse(dgvList.CurrentRow.Cells["id"].Value.ToString());
                Nullable <RequestDocument.DocumentType> type = null;
                if (dgvList.Rows[dgvList.CurrentCell.RowIndex].Cells["xbrlFlag"].Value.ToString() == "1")
                {
                    type = RequestDocument.DocumentType.Xbrl;
                }
                else if (dgvList.Rows[dgvList.CurrentCell.RowIndex].Cells["pdfFlag"].Value.ToString() == "1")
                {
                    type = RequestDocument.DocumentType.Pdf;
                }
                if (type != null)
                {
                    int year = 20 * 100 + id / 100000000;
                    //string filepath = null;
                    string download = dgvList.CurrentRow.Cells[type == RequestDocument.DocumentType.Xbrl ? "xbrl" : "pdf"].Value.ToString();
                    if (download == "404")
                    {
                        StatusLabel1.Text = string.Format("{0:HH:mm:ss} {1}[{2}] 404[Not Found] in table", DateTime.Now, docid, type == RequestDocument.DocumentType.Xbrl ? "xbrl" : "pdf");
                        return;
                    }

                    //int retry = 1;
                    ArchiveResponse response = await disclosures.ChangeDocumentAsync(id, docid, type.Value, setting.Retry);

                    dgvContents.DataSource = disclosures.DvContents;
                    dgvList.Refresh();
                    if (response == null)
                    {
                        StatusLabel1.Text = DateTime.Now.ToString("HH:mm:ss") + " ダウンロード済み書類";
                    }
                    else if (response.Exception != null)
                    {
                        StatusLabel1.Text = string.Format("{0:HH:mm:ss} ダウンロードできません {1}", DateTime.Now, response.Exception.Message + response.Exception.InnerException != null ? response.Exception.InnerException.Message : "");
                        return;
                    }
                    if (response != null)
                    {
                        if (response.EdinetStatusCode != null)
                        {
                            StatusLabel1.Text = string.Format("{1:HH:mm:ss} 書類取得API status[{0}] {2}ダウンロード {3}", response.EdinetStatusCode.Status, DateTime.Now, type == RequestDocument.DocumentType.Pdf ? "pdf" : "xbrl", response.Filename);
                        }
                        else
                        {
                            StatusLabel1.Text = string.Format("{1:HH:mm:ss} 書類取得API status[{0}] {2}ダウンロード {3}", response.HttpStatusCode.ToString(), DateTime.Now, type == RequestDocument.DocumentType.Pdf ? "pdf" : "xbrl", response.Filename);
                        }
                        if (dgvList.CurrentRow.Cells["docTypeCode"].Value.ToString() == "350" | dgvList.CurrentRow.Cells["docTypeCode"].Value.ToString() == "360")
                        {
                            if (dgvList.CurrentRow.Cells["summary"].Value.ToString() == "")
                            {
                                await disclosures.UpdateSummary(id, true);

                                dgvList.Refresh();
                            }
                        }
                    }
                    else
                    {
                        //filepath = disclosures.ArchiveResult.Name;
                    }
                    if (type == RequestDocument.DocumentType.Pdf)
                    {
                        string filepath = string.Format(@"{0}\Documents\{1}\{2}_2.pdf", setting.Directory, year, docid);
                        if (File.Exists(filepath))
                        {
                            string url = string.Format("file://{0}#toolbar=0&navpanes=0", filepath.Replace("\\", "/"));
                            browser.Navigate(url);
                        }
                        //browser.DocumentStream = new MemoryStream(response.Buffer);
                    }
                }
            }
        }