«Aggiungi account» ha una sola casella: mentre Alberto scrive compaiono subito i suggerimenti locali, poi i profili di Instagram con la foto, come chiesto in #37. Il browser di Febe scrive nella casella «Cerca» di Instagram con tasti veri da una mappa chiusa (mai Invio, Tab o Esc), controlla prima il fuoco, legge la risposta della pagina e non clicca mai un risultato. La ricerca ha una categoria propria nel Governatore, con tetti larghi che non consumano quelli della raccolta, e si dà il turno con il motore sull'unica pagina; a fine sessione la pagina torna comunque al riposo. Le foto passano dal CDN con un permesso ciascuna e restano solo in memoria. Le risposte di Alberto del 15 novembre (controllo del fuoco, turni, evento requestWillBeSent, niente foto dei seguiti per ora) sono in §6.1 e in docs/decisioni.md. Su Instagram vero la ricerca parte quando i bersagli search-open e search-box saranno raccolti dal vivo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
4.9 KiB
C#
120 lines
4.9 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Febe.Web.Library;
|
|
|
|
/// <summary>A user name read from the names pasted in «Aggiungi account»; <paramref name="Line"/> is its place (from 1).</summary>
|
|
public sealed record ParsedUsername(int Line, string Username);
|
|
|
|
/// <summary>A pasted name that is not a user name, with its place and the reason in Italian.</summary>
|
|
public sealed record UsernameProblem(int Line, string Text, string Reason);
|
|
|
|
/// <summary>
|
|
/// The names pasted or chosen in «Aggiungi account» (spec §4.2, mockup 04, issue #37): the window sends one name per
|
|
/// line (it splits what Alberto pasted at line breaks, commas and semicolons). Accepts «marta.demo», «@marta.demo»,
|
|
/// «instagram.com/marta.demo», «https://www.instagram.com/marta.demo/?hl=it». Refuses addresses of posts, reels and
|
|
/// other pages, and names Instagram would not accept. Names are compared and stored in lowercase. The reasons speak
|
|
/// of «names», not lines: the window shows each as «n° nome».
|
|
/// </summary>
|
|
public static partial class UsernameParser
|
|
{
|
|
/// <summary>First path segments of the site that are pages, not profiles.</summary>
|
|
public static readonly IReadOnlySet<string> ReservedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
"p", "reel", "reels", "tv", "stories", "explore", "accounts", "direct", "about", "legal", "developer", "web", "challenge", "emails", "session",
|
|
};
|
|
|
|
public static (IReadOnlyList<ParsedUsername> Names, IReadOnlyList<UsernameProblem> Problems) Parse(string? text)
|
|
{
|
|
var names = new List<ParsedUsername>();
|
|
var problems = new List<UsernameProblem>();
|
|
var seen = new Dictionary<string, int>(StringComparer.Ordinal);
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
{
|
|
return (names, problems);
|
|
}
|
|
|
|
var lines = text.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n');
|
|
for (var i = 0; i < lines.Length; i++)
|
|
{
|
|
var number = i + 1;
|
|
var line = lines[i].Trim();
|
|
if (line.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (line.Length > 300)
|
|
{
|
|
problems.Add(new UsernameProblem(number, line[..60] + "…", "Troppo lungo per essere un nome utente."));
|
|
continue;
|
|
}
|
|
|
|
var (candidate, reason) = Extract(line);
|
|
if (candidate is null)
|
|
{
|
|
problems.Add(new UsernameProblem(number, line, reason!));
|
|
continue;
|
|
}
|
|
|
|
if (seen.TryGetValue(candidate, out var first))
|
|
{
|
|
problems.Add(new UsernameProblem(number, line, $"Già scritto: è uguale al {first}° nome."));
|
|
continue;
|
|
}
|
|
|
|
seen[candidate] = number;
|
|
names.Add(new ParsedUsername(number, candidate));
|
|
}
|
|
|
|
return (names, problems);
|
|
}
|
|
|
|
/// <summary>True for a valid user name (already lowercase, without @).</summary>
|
|
public static bool IsValid(string username) =>
|
|
Valid().IsMatch(username) && !username.StartsWith('.') && !username.EndsWith('.') && !username.Contains("..", StringComparison.Ordinal);
|
|
|
|
private static (string? Username, string? Reason) Extract(string line)
|
|
{
|
|
var text = line;
|
|
if (text.Contains("instagram.com", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
if (!text.Contains("://", StringComparison.Ordinal))
|
|
{
|
|
text = "https://" + text;
|
|
}
|
|
|
|
if (!Uri.TryCreate(text, UriKind.Absolute, out var uri)
|
|
|| !(uri.Host.Equals("instagram.com", StringComparison.OrdinalIgnoreCase) || uri.Host.EndsWith(".instagram.com", StringComparison.OrdinalIgnoreCase)))
|
|
{
|
|
return (null, "Indirizzo non riconosciuto: serve quello del profilo, per esempio instagram.com/nome.utente.");
|
|
}
|
|
|
|
var segments = uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
|
if (segments.Length == 0)
|
|
{
|
|
return (null, "L'indirizzo non contiene il nome del profilo.");
|
|
}
|
|
|
|
if (ReservedPaths.Contains(segments[0]))
|
|
{
|
|
return (null, "È l'indirizzo di un post o di un'altra pagina, non di un profilo.");
|
|
}
|
|
|
|
text = Uri.UnescapeDataString(segments[0]);
|
|
}
|
|
|
|
text = text.Trim().TrimStart('@').ToLowerInvariant();
|
|
if (text.Contains(' ', StringComparison.Ordinal))
|
|
{
|
|
return (null, "Un nome utente non contiene spazi: per cercare un nome e cognome scrivilo da solo nella casella.");
|
|
}
|
|
|
|
return IsValid(text)
|
|
? (text, null)
|
|
: (null, "Non è un nome utente valido: lettere, numeri, punti e trattini bassi, al massimo 30.");
|
|
}
|
|
|
|
[GeneratedRegex("^[a-z0-9._]{1,30}$", RegexOptions.CultureInvariant)]
|
|
private static partial Regex Valid();
|
|
}
|