コード例 #1
0
        private void ReadFile(HtmlNode node)
        {
            try
            {
                var listTrItems = node.Descendants("tr")
                                  .Where(node => node.GetAttributeValue("class", "").Equals("js-navigation-item")).ToList();

                foreach (var item in listTrItems)
                {
                    var descendants = item.Descendants("a").ToList();
                    if (descendants == null || descendants.Count == 0)
                    {
                        continue;
                    }
                    var listNodeFolder = item.Descendants("a")
                                         .Where(node => node.GetAttributeValue("class", "").Equals("js-navigation-open ")).ToList();
                    if (listNodeFolder == null || listNodeFolder.Count == 0)
                    {
                        continue;
                    }

                    var    nodeFolder    = listNodeFolder.First();
                    string fileLink      = nodeFolder.GetAttributeValue("href", "");
                    string urlBase       = "https://github.com";
                    var    htmlDoc       = OpenLink(urlBase + fileLink);
                    var    divLinesBytes = htmlDoc.DocumentNode.Descendants("div")
                                           .Where(node => node.GetAttributeValue("class", "").Equals("text-mono f6 flex-auto pr-3 flex-order-2 flex-md-order-1 mt-2 mt-md-0")).ToList();
                    if (divLinesBytes != null && divLinesBytes.Count > 0)
                    {
                        var    nodeLines       = divLinesBytes.First();
                        string textLines       = nodeLines.InnerText.Trim();
                        var    listTextLines   = textLines.Split("\n      \n    ");
                        var    strongFinalPath = htmlDoc.GetElementbyId("blob-path").Descendants("strong")
                                                 .Where(node => node.GetAttributeValue("class", "").Equals("final-path")).ToList().First();
                        string fileName     = strongFinalPath.InnerText;
                        var    listFileType = fileName.Split(".");
                        string fileType     = "";
                        if (listFileType != null && listFileType.Length > 1)
                        {
                            fileType = listFileType.Last();
                        }

                        double nLines = 0;

                        if (listTextLines.Length > 1)
                        {
                            var isNumeric = double.TryParse(listTextLines.First().Split().First(), out _);
                            if (isNumeric)
                            {
                                nLines = double.Parse(listTextLines.First().Split().First());
                            }
                            else if (listTextLines.First() == "executable file")
                            {
                                fileType = listTextLines.First();
                            }
                        }

                        string   sBytes   = Consts.ConvertToBytes(listTextLines.Last());
                        double   nBytes   = double.Parse(sBytes.Split().First());
                        BlobFile blobFile = new BlobFile(fileName, fileType, nLines, nBytes);

                        if (dict.ContainsKey(blobFile.Extension))
                        {
                            dict[blobFile.Extension].Lines += blobFile.NumberLines;
                            dict[blobFile.Extension].Bytes += blobFile.NumberBytes;
                        }
                        else
                        {
                            TotalByExtension totalByExtension = new TotalByExtension(blobFile.NumberLines, blobFile.NumberBytes);
                            dict.Add(blobFile.Extension, totalByExtension);
                        }
                    }
                    else
                    {
                        var divRepoContent = htmlDoc.DocumentNode.Descendants("div")
                                             .Where(node => node.GetAttributeValue("class", "").Equals("repository-content ")).ToList();
                        ReadFile(divRepoContent.First());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }