Esempio n. 1
0
        public void Run(IrcMessage theMessage)
        {
            List <Labordaten> daten = LaborDaten.GetItem(true);

            if (LaborDaten.Renewed == DateTime.MinValue)
            {
                theMessage.Answer("Ich konnte leider keine Daten von der Laborwebseite abrufen und mein Cache ist leer");
                return;
            }
            if (!LaborDaten.IsUpToDate)
            {
                theMessage.Answer("Es war mir nicht möglich den Labor Cache zu erneuern. Grund: " + LaborDaten.LastUpdateFail.Message + ". Verwende Cache vom " + LaborDaten.Renewed.ToString());
            }

            if (String.IsNullOrEmpty(theMessage.CommandLine))
            {
                theMessage.Answer("Aktuelle Labor Daten: " + daten.Select(x => $"{x.Typ}: {x.Datum}").Join(", ") + " - Zum Labor: " + Toolbox.ShortUrl("http://www.avm.de/de/Service/Service-Portale/Labor/index.php"));
            }
            else
            {
                string     BoxName = BoxDatabase.GetShortName(theMessage.CommandLine);
                Labordaten first   = daten.FirstOrDefault(x => x.Typ == BoxName);
                if (first != null)
                {
                    theMessage.Answer($"Die neueste {first.Typ} labor Version ist am {first.Datum} erschienen mit der Versionsnummer: {first.Version} - Laborseite: {first.Url}");
                }
                else
                {
                    theMessage.Answer("Eine solche Labor Firmware ist mir nicht bekannt");
                }
            }
        }
Esempio n. 2
0
        private List <Labordaten> UpdateLaborCache(List <Labordaten> AlteLaborDaten)
        {
            var               context         = BrowsingContext.New(Configuration.Default.WithDefaultLoader());
            IDocument         LaborStartSeite = context.OpenAsync(BaseUrl).Result;
            List <Labordaten> NeueLaborDaten  = LaborStartSeite.QuerySelectorAll <IHtmlAnchorElement>("#content-section div.csc-space-after-1 div.csc-default a.button-link").Where(x => x.Href.Contains("/fritz-labor/")).Select(link =>
            {
                string fallbackDate = null;
                var lastUpdate      = link.ParentElement.PreviousElementSibling;
                if (lastUpdate.TextContent.StartsWith(KeywordLetztesUpdate))
                {
                    fallbackDate = lastUpdate.TextContent.Substring(KeywordLetztesUpdate.Length);
                }
                IDocument detailPage = link.NavigateAsync().Result;
                IHtmlParagraphElement downloadInformations = detailPage.QuerySelector <IHtmlParagraphElement>("p:contains('Downloadinformationen:')") ?? detailPage.QuerySelector <IHtmlParagraphElement>("p:contains('Informationen zum Download:')");

                if (downloadInformations == null)
                {
                    return(null);
                }

                Labordaten daten = new Labordaten();
                daten.Typ        = detailPage.Title.EndsWith(TitleAVM) ? detailPage.Title.Remove(detailPage.Title.Length - TitleAVM.Length) : detailPage.Title;
                daten.Version    = downloadInformations.ChildNodes.Where(x => x.TextContent.StartsWith(KeywordVersion)).Select(x => x.TextContent.Substring(KeywordVersion.Length)).FirstOrDefault();
                daten.Datum      = downloadInformations.ChildNodes.Where(x => x.TextContent.StartsWith(KeywordDatum)).Select(x => x.TextContent.Substring(KeywordDatum.Length)).FirstOrDefault() ?? fallbackDate;
                daten.Url        = link.Href;
                return(daten);
            }).ToList();

            try
            {
                NeueLaborDaten.AddRange(GetFTPBetas());
            }
            catch (FtpCommandException ex)
            {
                if (ex.Message == "There are too many connections from your internet address.")
                {
                    Log.Warning("Abrufen von FTP Labors wegen zu vielen Connections fehlgeschlagen");
                }
                else
                {
                    Log.Error(ex, "Abrufen von FTP Labors fehlgeschlagen");
                }
            }

            if (NeueLaborDaten.Count > 0)
            {
                return(NeueLaborDaten);
            }
            return(AlteLaborDaten);
        }
Esempio n. 3
0
        private List <Labordaten> GetFTPBetas()
        {
            List <Tuple <Labordaten, string> > ftpBetas = new List <Tuple <Labordaten, string> >();

            var betaCache = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "betaCache");

            if (!Directory.Exists(betaCache))
            {
                Directory.CreateDirectory(betaCache);
            }

            using (FtpClient ftp = new FtpClient())
            {
                ftp.Host        = "ftp.avm.de";
                ftp.Credentials = new NetworkCredential("anonymous", "");
                ftp.SetWorkingDirectory("/fritz.box/beta");

                List <FtpListItem> files = ftp.GetListing().Where(x => x.Type == FtpFileSystemObjectType.File).ToList();
                foreach (FtpListItem file in files)
                {
                    Labordaten daten = new Labordaten();
                    daten.Datum = (file.Modified == DateTime.MinValue && ftp.HasFeature(FtpCapability.MDTM) ? ftp.GetModifiedTime(file.FullName) : file.Modified).ToString("dd.MM.yyyy HH:mm:ss");
                    daten.Url   = "ftp://ftp.avm.de" + file.FullName;

                    string target = Path.Combine(betaCache, file.Name);
                    if (!File.Exists(target))
                    {
                        using (Stream f = ftp.OpenRead(file.Name))
                            using (FileStream fi = File.Create(target))
                                f.CopyTo(fi);
                    }

                    ftpBetas.Add(new Tuple <Labordaten, string>(daten, target));
                }
            }

            foreach (Tuple <Labordaten, string> fw in ftpBetas)
            {
                try
                {
                    using (Stream file = File.OpenRead(fw.Item2))
                        using (ZipArchive archive = new ZipArchive(file, ZipArchiveMode.Read))
                        {
                            ZipArchiveEntry firmware = archive.Entries.FirstOrDefault(x => x.Name.Contains("_Labor.") || x.Name.Contains(".Labor.") || x.Name.Contains("_LabBETA.") || x.Name.Contains(".LabBETA."));
                            if (firmware == null)
                            {
                                Log.Error("Firmware {Firmware} hat keine erkannte Labor Firmware", fw.Item2);
                                continue;
                            }

                            string RawName = firmware.Name;

                            fw.Item1.Version = Regex.Match(RawName, @"((\d{2,3}\.)?\d\d\.\d\d(-\d{1,6})?).image$").Groups[1].Value;
                            if (!BoxDatabase.TryGetShortName(RawName, out string tmp))
                            {
                                if (RawName.LastIndexOf(' ') != -1)
                                {
                                    fw.Item1.Typ = RawName.AsSpan(RawName.LastIndexOf(' ')).Trim().ToString();
                                }
                                else
                                {
                                    fw.Item1.Typ = RawName.Trim();
                                }
                            }
                            else
                            {
                                fw.Item1.Typ = tmp;
                            }
                        }
                }
                catch (InvalidDataException ex) //'System.IO.InvalidDataException' in System.IO.Compression.dll("Das Ende des Datensatzes im zentralen Verzeichnis wurde nicht gefunden.")
                {
                    var    betaCachePath     = Path.GetDirectoryName(fw.Item2);
                    var    name              = Path.GetFileNameWithoutExtension(fw.Item2);
                    var    extension         = Path.GetExtension(fw.Item2);
                    string corruptedFilePath = Path.Combine(betaCachePath, $"{name}_corrupted_{DateTime.Now:dd_MM_yyyy_hh_mm_ss}{extension}");
                    File.Move(fw.Item2, corruptedFilePath);
                    Log.Error(ex, "Korruptes Zip {Filename} gefunden. Zip umbenannt zu {NewFilename}", fw.Item2, corruptedFilePath);
                }
            }

            return(ftpBetas.Select(x => x.Item1).ToList());
        }