Files
rss-generator/rss-generator/Class/RSS/javtwi.cs
T
2026-09-03 19:59:23 +09:00

126 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.RegularExpressions;
namespace rss_generator.Class.RSS;
internal class javtwi
{
public static async Task CreateRSSAsync(HttpClient client)
{
var baseURL = "https://javtwi.com/new.html";
var feed = new SyndicationFeed("javtwi", "", new Uri(baseURL));
var items = new List<SyndicationItem>();
try
{
var document = await CmnUtil.GetDocument(client, baseURL);
var linkList = new List<string>();
if (document == null)
{
return;
}
var elements = document.QuerySelectorAll("div.center_contents a.center_contents");
foreach (var linkElement in elements)
{
var imgElement = linkElement.QuerySelector("a img");
var linkHref = linkElement?.GetAttribute("href") ?? string.Empty;
var imageSrc = (imgElement as IHtmlImageElement)!.Source;
if (linkHref != string.Empty && !linkList.Contains(linkHref))
{
if (linkHref.Last() == '?')
{
linkHref = linkHref.TrimEnd('?');
}
// 重複行は追加しない
if (linkList.Contains(linkHref))
{
continue;
}
linkList.Add(linkHref);
}
else
{
continue;
}
var itemUri = !string.IsNullOrEmpty(linkHref) ? new Uri(linkHref) : null;
var imageUri = !string.IsNullOrEmpty(imageSrc) ? new Uri(imageSrc) : null;
if (itemUri == null)
{
continue;
}
if (itemUri.AbsolutePath.Contains("404."))
{
continue;
}
var title = CmnUtil.GetLastPathPart(linkHref);
// Twitter Snowflake ID から正確な投稿日時(JST)を逆算
DateTime publishedDate = DateTime.Now;
var match = Regex.Match(linkHref, @"/(\d{17,20})/");
if (!match.Success && !string.IsNullOrEmpty(imageSrc))
{
match = Regex.Match(imageSrc, @"/(\d{17,20})/");
}
if (match.Success && long.TryParse(match.Groups[1].Value, out long snowflakeId))
{
try
{
// Twitter Snowflake ID (エポック: 1288834974657 ms = 2010-11-04 01:42:54.657 UTC)
long unixMs = (snowflakeId >> 22) + 1288834974657L;
var offset = DateTimeOffset.FromUnixTimeMilliseconds(unixMs).ToOffset(TimeSpan.FromHours(9));
publishedDate = offset.DateTime;
}
catch
{
publishedDate = DateTime.Now;
}
}
var body = new StringBuilder();
body.Append($@"<div><a href=""{itemUri}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer"">{title}</a></div>");
body.Append($@"<video controls poster=""{imageUri}"" preload=""metadata"" referrerpolicy=""no-referrer"" style=""max-width: 100%; height: auto;"">");
body.Append($@"<source src=""{itemUri}"" type=""video/mp4"">");
body.Append($@"<p><a href=""{itemUri}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer"">動画を開く (.mp4)</a></p>");
body.Append($@"</video>");
var item = new SyndicationItem(
title,
string.Empty,
itemUri,
CmnUtil.CreateGuid(linkHref),
publishedDate
)
{
PublishDate = publishedDate,
LastUpdatedTime = publishedDate
};
// item.Content = SyndicationContent.CreateHtmlContent(body.ToString());
// ※ RSS 2.0の場合は Content よりも Summary(<description>タグ)が使われることが多いため、
// 両方に設定しておくか、Summary側にHTMLを設定すると大半のリーダーで確実です。
item.Summary = new TextSyndicationContent(body.ToString(), TextSyndicationContentKind.Html);
items.Add(item);
}
feed.Items = items;
CmnUtil.ExportXMLFile("javtwi.xml", feed);
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] javtwi:{ex.Message}");
}
}
}