public void TestParsingUrlTest() { OpenGraph graph = OpenGraph.ParseUrl(SpacedLink); Assert.Contains("<html", graph.OriginalHtml); this.AssertSpaced(graph); }
public void TestParsingUrlTest() { OpenGraph graph = OpenGraph.ParseUrl(SpacedLink); Assert.Contains("<html", graph.OriginalHtml, StringComparison.InvariantCultureIgnoreCase); this.AssertSpaced(graph); }
public void TestGetOpenGraphImageFromUri() { Uri testUri = new Uri("http://www.bbc.com/news/technology-32194196"); var graph = OpenGraph.ParseUrl(testUri); Assert.AreEqual("http://ichef.bbci.co.uk/news/1024/media/images/80755000/jpg/_80755021_163765270.jpg", graph.Image.ToString(), "Unable to extract domain from given Uri."); }
public void TestUrlDecodingUrlValues() { var expectedUrl = "https://tn.periscope.tv/lXc5gSh6UPaWdc37LtVCb3UdtSfvj2QNutojPK2du5YWrNchfI4wXpwwHKTyfDhmfT2ibsBZV4doQeWlhSvI4A==/chunk_314.jpg?Expires=1781852253&Signature=U5OY3Y2HRb4ETmakQAPwMcv~bqu6KygIxriooa41rk64RcDfjww~qpVgMR-T1iX4S9NxfvXHLMT3pEckBDEOicsNO7oUAo4NieH9GRB2Sv0EA7swxLojD~Zn98ThNWTF5fSzv6SSPjyvctsqBiRmvAN6x7fmMH6l3vzx8ePSCgdEm8-31lUAz7lReBNZQjYSi~C8AwqZVI0Mx6y8lNKklL~m0e6RTGdvr~-KIDewU3wpjSdX7AgpaXXjahk4x-ceUUKcH3T1j--ZjaY7nqPO9fbMZFNPs502A32mrcmaZCzvaD~AuoH~u3y44mJVjzHRrpTxHIBklqHxAgc7dzverg__&Key-Pair-Id=APKAIHCXHHQVRTVSFRWQ"; var og = OpenGraph.ParseUrl("https://www.periscope.tv/w/1DXxyZZZVykKM"); Assert.Equal(expectedUrl, og.Image.ToString()); }
public void TestParsingUrlValidateEncodingIsCorrect() { var expectedContent = "Создайте себе горное настроение с нашим первым фан-китом по игре #SteepGame&#33; -&gt; http://ubi.li/u8w9n"; var tags = OpenGraph.ParseUrl("https://vk.com/wall-41600377_66756"); Assert.Equal(expectedContent, tags.Metadata["og:description"].First().Value); }
// return remote page title from URI public static string GetTitleFromUri(string @remoteUri) { try { // try using Open Graph to get target page title var graph = OpenGraph.ParseUrl(@remoteUri, "Voat.co OpenGraph Parser"); if (!string.IsNullOrEmpty(graph.Title)) { var tmpStringWriter = new StringWriter(); HttpUtility.HtmlDecode(graph.Title, tmpStringWriter); return(tmpStringWriter.ToString()); } // Open Graph parsing failed, try getting HTML TITLE tag instead HtmlWeb htmlWeb = new HtmlWeb(); HtmlDocument htmlDocument = htmlWeb.Load(@remoteUri); if (htmlDocument != null) { var titleNode = htmlDocument.DocumentNode.Descendants("title").SingleOrDefault(); if (titleNode != null) { return(titleNode.InnerText); } } return(null); } catch (Exception ex) { return(null); } }
public void FactParsingUrlValidateEncodingIsCorrect() { var expectedContent = "Создайте себе горное настроение с нашим первым фан-китом по игре #SteepGame&#33; -&gt; http://ubi.li/u8w9n"; var tags = OpenGraph.ParseUrl("https://vk.com/wall-41600377_66756"); Assert.True(tags["description"] == expectedContent); }
public OpenGraphProfileOutput GetOpenGraph(string url) { var result = OpenGraph.ParseUrl(url); return(new OpenGraphProfileOutput() { Image = result?.Image?.ToString(), Title = result?.Title, }); }
public void TestMetaCharsetParsesCorrectly() { var expectedTitle = "Réalité virtuelle : 360° de bonheur à améliorer"; var expectedDescription = "Le cinéma à 360° a désormais son festival. Organisé par le Forum des images, le premier Paris Virtual Film Festival a donc vu le jour. Narration, réalisation, montage… une révolution balbutiante est en marche. Tour d'horizon."; var ogs = OpenGraph.ParseUrl("http://www.telerama.fr/cinema/realite-virtuelle-360-de-bonheur-a-ameliorer,144339.php?utm_medium=Social&utm_source=Twitter&utm_campaign=Echobox&utm_term=Autofeed#link_time=1466595239"); Assert.Equal(expectedTitle, ogs.Title); Assert.Equal(expectedDescription, ogs.Metadata["og:description"].First().Value); }
public async Task TestParsingUrlsWithoutScheme() { var withoutScheme = SpacedLink.Replace("http://", string.Empty); var withHttpsScheme = SpacedLink.Replace("http", "https"); OpenGraph.ParseUrl(withHttpsScheme); await OpenGraph.ParseUrlAsync(withHttpsScheme); OpenGraph.ParseUrl(withoutScheme); await OpenGraph.ParseUrlAsync(withoutScheme); }
public void TestMetaCharsetParsesCorrectly() { var expectedTitle = "Réalité virtuelle : 360° de bonheur à améliorer"; var expectedDescription = "Le cinéma à 360° a désormais son festival. Organisé par le Forum des images, le premier Paris Virtual Film Festival a donc vu le jour...."; var ogs = OpenGraph.ParseUrl("http://www.telerama.fr/cinema/realite-virtuelle-360-de-bonheur-a-ameliorer,144339.php?utm_medium=Social&utm_source=Twitter&utm_campaign=Echobox&utm_term=Autofeed#link_time=1466595239"); Assert.AreEqual(expectedTitle, ogs["title"]); Assert.AreEqual(expectedDescription, ogs["description"]); }
public ImageOutput GetOpenGraph(string url) { var result = OpenGraph.ParseUrl(url); return(new ImageOutput() { Image = result?.Image?.ToString(), ImageBase64 = ConvertImageURLToBase64(result?.Image?.ToString()), Title = result?.Title, }); }
public async Task TestParsingUrlsWithoutScheme() { var withoutScheme = SpacedLink.Replace("http://", string.Empty, StringComparison.InvariantCultureIgnoreCase); var withHttpsScheme = SpacedLink.Replace("http", "https", StringComparison.InvariantCultureIgnoreCase); OpenGraph.ParseUrl(withHttpsScheme); await OpenGraph.ParseUrlAsync(withHttpsScheme).ConfigureAwait(false); OpenGraph.ParseUrl(withoutScheme); await OpenGraph.ParseUrlAsync(withoutScheme).ConfigureAwait(false); }
public void ParseUrl_AmazonUrl_Test() { OpenGraph graph = OpenGraph.ParseUrl("http://www.amazon.com/Spaced-Complete-Simon-Pegg/dp/B0019MFY3Q"); Assert.AreEqual("http://www.amazon.com/dp/B0019MFY3Q/ref=tsm_1_fb_lk", graph.Url.ToString()); Assert.IsTrue(graph.Title.StartsWith("Spaced: The Complete Series")); Assert.IsTrue(graph["description"].Contains("Spaced")); Assert.IsTrue(graph.Image.ToString().StartsWith("http://ecx.images-amazon.com/images/I")); Assert.AreEqual("movie", graph.Type); Assert.AreEqual("Amazon.com", graph["site_name"]); }
public void TestGetOpenGraphImageFromUri() { //HACK: This test is most likely Geo Sensitive, thus it fails on a U.S. network. //This test needs to be performed on static server based resource instead. Uri testUri = new Uri("http://www.bbc.com/news/technology-32194196"); var graph = OpenGraph.ParseUrl(testUri); List <string> acceptable = new List <string>() { "http://ichef.bbci.co.uk/news/1024/media/images/80755000/jpg/_80755021_163765270.jpg", //'merica test "http://ichef-1.bbci.co.uk/news/1024/media/images/80755000/jpg/_80755021_163765270.jpg", //'merica test part 2 "http://news.bbcimg.co.uk/media/images/80755000/jpg/_80755021_163765270.jpg" //Yuro test }; var expected = graph.Image.ToString(); var passed = acceptable.Any(x => x.Equals(expected, StringComparison.OrdinalIgnoreCase)); Assert.IsTrue(passed, "OpenGraph was unable to find an acceptable image path"); }
// return remote page title from URI public static string GetTitleFromUri(string @remoteUri) { try { var graph = OpenGraph.ParseUrl(@remoteUri); if (!string.IsNullOrEmpty(graph.Title)) { var tmpStringWriter = new StringWriter(); HttpUtility.HtmlDecode(graph.Title, tmpStringWriter); return(tmpStringWriter.ToString()); } var req = (HttpWebRequest)WebRequest.Create(@remoteUri); req.Timeout = 3000; var sr = new StreamReader(req.GetResponse().GetResponseStream()); var buffer = new Char[256]; var counter = sr.Read(buffer, 0, 256); while (counter > 0) { var outputData = new String(buffer, 0, counter); var match = Regex.Match(outputData, @"<title>([^<]+)", RegexOptions.IgnoreCase); if (match.Success) { var tmpStringWriter = new StringWriter(); HttpUtility.HtmlDecode(match.Groups[1].Value, tmpStringWriter); return(tmpStringWriter.ToString()); } counter = sr.Read(buffer, 0, 256); } return(null); } catch (Exception) { return(null); } }
public void TestParsingUrlTest() { OpenGraph graph = OpenGraph.ParseUrl(SpacedLink); this.AssertSpaced(graph); }
// this class is where the magic starts, and takes actions upon receiving messages public async Task MessageReceivedAsync(SocketMessage rawMessage) { if (!(rawMessage is SocketUserMessage message)) { return; } // ensures we don't process system/other bot messages if (message.Source != MessageSource.User) { // Add exception to this rule if desired: if (string.IsNullOrEmpty(_config["AllowBotMessages"]) || _config["AllowBotMessages"].ToLower() != "true") { return; } } //DMs: if (message.Channel.GetType() == typeof(SocketDMChannel)) { //TODO: Move this to a module file: if (message.Content.ToLower().StartsWith("debug")) { if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { long guilds = 0; foreach (DiscordSocketClient shard in _client.Shards) { guilds += shard.Guilds.Count(); } //Server count: await message.ReplyAsync("Server Count: " + guilds); //Shard count: await message.ReplyAsync("Shards: " + _client.Shards.Count()); //IP check: try { OpenGraph graph = OpenGraph.ParseUrl("https://api.ipify.org/", ""); await message.ReplyAsync("IP: " + graph.OriginalHtml); } catch (Exception e) { await message.ReplyAsync("Could not connect to server. Error: " + e); } } } else if (message.Content.ToLower().StartsWith("guilds")) { //Guild list if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { //TODO: Export to CSV file string serverList = Format.Bold("Servers:"); foreach (SocketGuild guild in _client.Guilds) { String serverLine = "\n" + guild.Name + " \tBoost: " + guild.PremiumTier + " \tUsers: " + guild.MemberCount + " \tLocale: " + guild.PreferredLocale; //Discord max message length: if (serverList.Length + serverLine.Length > 2000) { await message.ReplyAsync(serverList); serverList = ""; } serverList += serverLine; } await message.ReplyAsync(serverList); } } else if (message.Content.ToLower().StartsWith("toggle error")) { //toggle error DMs if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { notifyOwnerOnError = !notifyOwnerOnError; if (notifyOwnerOnError) { await message.ReplyAsync("Error notifications enabled."); } else { await message.ReplyAsync("Error notifications disabled."); } } } else if (message.Content.ToLower().StartsWith("users")) { if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { long users = 0; foreach (DiscordSocketClient shard in _client.Shards) { foreach (SocketGuild guild in shard.Guilds) { users += guild.MemberCount; } } await message.ReplyAsync("Users: " + users); } } else if (message.Content.ToLower().StartsWith("accounts")) { if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { foreach (IGAccount user in InstagramProcessor.AccountFinder.Accounts) { if (user.OTPSecret != null) { try { var code = Security.GetTwoFactorAuthCode(user.OTPSecret); await message.ReplyAsync("Username: "******"\n2FA Code: " + code + "\nLast Failed: " + user.Blacklist); } catch (Exception e) { await message.ReplyAsync("Failed to get 2FA code."); Console.WriteLine("2FA Code error: " + e); } } else { await message.ReplyAsync("Username: "******"\nLast Failed: " + user.Blacklist); } } } } else if (message.Content.ToLower().StartsWith("sync")) { if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { if (_subscriptions.CurrentlyCheckingAccounts()) { await message.ReplyAsync("Already doing that."); } else { // Run this async to avoid blocking the current thread: // Use discard since im not interested in the output, only the process. _ = _subscriptions.GetLatestsPosts(); //Let the user know its being worked on: await message.ReplyAsync("Working on it."); } } } else if (message.Content.ToLower().StartsWith("overwrite")) { if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { // Load all registered commands: var commands = await _client.Rest.GetGlobalApplicationCommands(); // Delete all commands: foreach (var command in commands) { await command.DeleteAsync(); } // Re-register commands: await _interact.RegisterCommandsGloballyAsync(true); // Alert user: await message.ReplyAsync("Slash commands resynced."); } } else if (message.Content.ToLower().StartsWith("clearstate")) { if (!string.IsNullOrEmpty(_config["OwnerID"]) && message.Author.Id == ulong.Parse(_config["OwnerID"])) { // Clear statefiles: string stateFile = Path.Combine(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "StateFiles"); if (Directory.Exists(stateFile)) { Directory.Delete(stateFile, true); Directory.CreateDirectory(stateFile); } else { await message.ReplyAsync("Folder not found. Skipping folder removal."); } // Clear loaded accounts: InstagramProcessor.AccountFinder.Accounts = new List <IGAccount>(); InstagramProcessor.AccountFinder.LoadAccounts(); await message.ReplyAsync("State files removed."); } } return; } // sets the argument position away from the prefix we set int argPos = 0; int endUrlLength = 0; bool foundPrefix = false; // get each prefix from the configuration file foreach (string prefix in _config.GetSection("Prefix").GetChildren().ToArray().Select(c => c.Value).ToArray()) { //check for valid prefix: if (message.Content.Contains(prefix)) { argPos = message.Content.IndexOf(prefix) + prefix.Length; endUrlLength = message.Content.Substring(argPos).Replace("\n", " ").IndexOf(" "); foundPrefix = true; break; } } if (!foundPrefix) { return; } var context = new ShardedCommandContext(_client, message); //create new string from command string commandText; if (endUrlLength <= 0) { commandText = message.Content.Substring(argPos).Replace("/", " "); } else { commandText = message.Content.Substring(argPos, endUrlLength).Replace("/", " "); } //Check for profile link: if (InstagramProcessor.isProfileLink(new Uri("https://instagram.com/" + commandText.Replace(" ", "/")))) { //Little hack to add command to url (profile is also reserved by ig so no conflicts): commandText = "profile " + commandText; } //Split url down to params: String[] userInput = commandText.Split(" "); foreach (CommandInfo command in _commands.Commands) { if (command.Name.Equals(userInput[0])) { await _commands.ExecuteAsync(context, commandText, _services); } else if (command.Name.Equals(userInput[1])) { commandText = commandText.Replace(userInput[0] + " ", ""); Console.WriteLine(commandText); await _commands.ExecuteAsync(context, commandText, _services); } } }
// generate a thumbnail if submission is a direct link to image or video public static async Task <string> ThumbnailFromSubmissionModel(Message submissionModel) { var extension = Path.GetExtension(submissionModel.MessageContent); // this is a direct link to image if (extension != String.Empty) { if (extension == ".jpg" || extension == ".JPG" || extension == ".png" || extension == ".PNG" || extension == ".gif" || extension == ".GIF") { try { var thumbFileName = await GenerateThumbFromUrl(submissionModel.MessageContent); return(thumbFileName); } catch (Exception) { // thumnail generation failed, skip adding thumbnail return(null); } } // try generating a thumbnail by using the Open Graph Protocol try { var graphUri = new Uri(submissionModel.MessageContent); var graph = OpenGraph.ParseUrl(graphUri, userAgent: "Voat.co OpenGraph Parser"); // open graph failed to find og:image element, abort thumbnail generation if (graph.Image == null) { return(null); } var thumbFileName = await GenerateThumbFromUrl(graph.Image.ToString()); return(thumbFileName); } catch (Exception) { // thumnail generation failed, skip adding thumbnail return(null); } } // this is not a direct link to an image, it could be a link to an article or video // try generating a thumbnail by using the Open Graph Protocol try { var graphUri = new Uri(submissionModel.MessageContent); var graph = OpenGraph.ParseUrl(graphUri, userAgent: "Voat.co OpenGraph Parser"); // open graph failed to find og:image element, abort thumbnail generation if (graph.Image == null) { return(null); } var thumbFileName = await GenerateThumbFromUrl(graph.Image.ToString()); return(thumbFileName); } catch (Exception) { // thumnail generation failed, skip adding thumbnail return(null); } }