Created
June 23, 2025 18:18
-
-
Save guillaC/c1d797920bb552ced0c7f31aba1ea624 to your computer and use it in GitHub Desktop.
Checks whether a subreddit already exists or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.Text; | |
class Program | |
{ | |
static async Task Main() | |
{ | |
var villes = new List<string> | |
{ | |
"Moskva", "London", "Berlin", "Madrid", "Roma", "Paris", "București", "Wien", | |
"Hamburg", "Budapest", "Warszawa", "Barcelona", "Milano", "Praha", "Sofia", "Bruxelles", | |
"Birmingham", "Köln", "Napoli", "Stockholm", "Manchester", "Amsterdam", "Lisboa", "København", | |
"Dublin", "Helsinki", "Lyon", "València", "München", "Frankfurt", "Stuttgart", "Düsseldorf", | |
"Dortmund", "Essen", "Leipzig", "Hannover", "Nürnberg", "Dresden", "Bochum", "Wuppertal" | |
}; | |
using var client = new HttpClient(); | |
client.DefaultRequestHeaders.Clear(); | |
client.DefaultRequestHeaders.Add("User-Agent", | |
"Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_5 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13G36 Safari/601.1"); | |
client.DefaultRequestHeaders.Add("Accept", "text/html"); | |
foreach (var ville in villes) | |
{ | |
var subreddit = RemoveAccents(ville.Replace(" ", "").Replace("-", "").Replace("&", "")); | |
var url = $"https://www.reddit.com/r/{subreddit}/"; | |
try | |
{ | |
var response = await client.GetAsync(url); | |
var html = await response.Content.ReadAsStringAsync(); | |
bool notFound = html.Contains("Community not found"); | |
if (notFound) | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"{ville}> Pas de subreddit"); | |
} | |
else | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine($"{ville}> Subreddit existe"); | |
} | |
Console.ResetColor(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"{ville}> Erreur ({ex.Message})"); | |
} | |
await Task.Delay(1000); | |
} | |
} | |
public static string RemoveAccents(string text) | |
{ | |
var normalizedString = text.Normalize(NormalizationForm.FormD); | |
var stringBuilder = new StringBuilder(); | |
foreach (var c in normalizedString) | |
{ | |
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); | |
if (unicodeCategory != UnicodeCategory.NonSpacingMark) | |
{ | |
stringBuilder.Append(c); | |
} | |
} | |
return stringBuilder.ToString().Normalize(NormalizationForm.FormC); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment