Create a snapshot / thumbnail of a blog post using mvc

I need to create a snapshot of several blog links.

I have a list of texts like these "Report: Twitter will release this month's music discovery application http://on.mash.to/10L1v49 via @mashable"

I want to show the links as a snapshot of the blog, and then the text in my opinion. Or at least I need the image to be attached to the blog.

Using facebook debugging, http://developers.facebook.com/tools/debug , I get this.

fb:app_id: 122071082108 og:url: http://mashable.com/2013/03/13/twitter-music-app/ og:type: article og:title: Report: Twitter Will Release Music Discovery App This Month og:image: og:description: Twitter is planning to release a standalone music app for iOS called Twitter Music as soon as the end of this month, according to CNET. CNET reports that Twitter Music will help... og:site_name: Mashable og:updated_time: 1363267654 

I tried the same link from my C # code, turned to the link with the "q" parameter as my desired link. I got the same html as the answer, but I can not find the linked image as it goes differently for different links.

Can anyone suggest a better way to do this in mvc?

My code in the controller to access facebook debugging:

  var client = new RestClient { BaseUrl = "http://developers.facebook.com/tools/debug/og/object" }; var request = new RestRequest { DateFormat = DataFormat.Xml.ToString(), Resource = "Add", Method = Method.GET }; request.AddParameter("q", "http://on.mash.to/10L1v49"); IRestResponse response = client.Execute(request); var content = response.Content; // raw content as string 
+4
source share
1 answer

What I understand from your question, you need something like a preview of the link that we get when you insert a link in the facebook sharing area.

The Facebook debugging method returns an html page that has an image of your blog post from the specified link.

Use HtmlAgilityPack to parse your html returned from facebook debugging

 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(content); HtmlNode root = doc.DocumentNode; var imageurl = doc.DocumentNode.SelectNodes("//img/@src").LastOrDefault(); string imagesrc = imageurl.OuterHtml.ToString(); int start = imagesrc.IndexOf("url="); int to = imagesrc.IndexOf("\"", start + "url=".Length); string s = imagesrc.Substring( start + "url=".Length, to - start - "url=".Length); string a = Uri.UnescapeDataString(s); 

and .. you have a blog image. The same function can be changed to cancel the title, description and updated blog post time.

+7
source

Source: https://habr.com/ru/post/1469250/


All Articles