Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

XKCD Viewer

Just something I made while I was stumped at work.

[Image: QqeRjQ0.png]

Fair warning, if you're still on a screen with a < 900px height, you're going to have a bad time trying to use this.
Also, no error handling. It's not supposed to be a serious project.

I don't really care what you do with this, go crazy.

[ Source Download (MEGA) | Source Download (WikiShare) ]

Constructive criticism is also appreciated :)
Hello,

I was trying to download your application to Critique it but i could not download it for some reason, every time i just got this message.

[Image: a7GoYU9.png]

I ended up making my own function for the random image. Would be good to compare how we managed it :)

c# code but if you can code .net you should be able to read c#
Code
public Image GetImage()
      {
      HttpWebRequest Hr = (HttpWebRequest)WebRequest.Create(randomimageurl);
      Hr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
      Hr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
      Hr.Referer = "http://xkcd.com/";
      Hr.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

      HttpWebResponse resp = (HttpWebResponse)Hr.GetResponse();
      if (resp.ResponseUri != null)
      {
      // Debug.Print(resp.ResponseUri.ToString());
      HttpWebRequest Hr1 = (HttpWebRequest)WebRequest.Create(randomimageurl);
      Hr1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
      Hr1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
      Hr1.Referer = "http://xkcd.com/";
      Hr1.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

      HttpWebResponse resp1 = (HttpWebResponse)Hr1.GetResponse();
      StreamReader Sr = new StreamReader(resp1.GetResponseStream());
      String page = Sr.ReadToEnd();
      Sr.Close();

      page = page.Split(new string[] { "Image URL (for hotlinking/embedding): " }, StringSplitOptions.RemoveEmptyEntries)[1];
      page = page.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)[0];
      Debug.Print(page);
      Image I = Image.FromStream(new MemoryStream(new WebClient().DownloadData(page.Trim())));
      resp.Close();
      resp1.Close();

      return I;
      }

      return null;
      }
(May 21, 2015 03:45 PM)sweetysuckscock Wrote:  Hello,

I was trying to download your application to Critique it but i could not download it for some reason, every time i just got this message.

[Image: a7GoYU9.png]

I ended up making my own function for the random image. Would be good to compare how we managed it :)

c# code but if you can code .net you should be able to read c#
Code
public Image GetImage()
      {
      HttpWebRequest Hr = (HttpWebRequest)WebRequest.Create(randomimageurl);
      Hr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
      Hr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
      Hr.Referer = "http://xkcd.com/";
      Hr.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

      HttpWebResponse resp = (HttpWebResponse)Hr.GetResponse();
      if (resp.ResponseUri != null)
      {
      // Debug.Print(resp.ResponseUri.ToString());
      HttpWebRequest Hr1 = (HttpWebRequest)WebRequest.Create(randomimageurl);
      Hr1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
      Hr1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
      Hr1.Referer = "http://xkcd.com/";
      Hr1.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

      HttpWebResponse resp1 = (HttpWebResponse)Hr1.GetResponse();
      StreamReader Sr = new StreamReader(resp1.GetResponseStream());
      String page = Sr.ReadToEnd();
      Sr.Close();

      page = page.Split(new string[] { "Image URL (for hotlinking/embedding): " }, StringSplitOptions.RemoveEmptyEntries)[1];
      page = page.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)[0];
      Debug.Print(page);
      Image I = Image.FromStream(new MemoryStream(new WebClient().DownloadData(page.Trim())));
      resp.Close();
      resp1.Close();

      return I;
      }

      return null;
      }

To be honest, I think you may have complicated this a little bit 😝 I split most stuff into different classes/modules. Here's the button click event:

Code
Private Sub RandomButton_Click(sender As Object, e As EventArgs) Handles RandomButton.Click
      Dim R As New Random()
      Comic = New XKCDComic(R.Next(1, MaxComicID))
      AdjustViewer(Comic)
    End Sub

All the AdjustViewer method does is update all the appropriate controls on the form.

Here's the constructor for the XKCDComic class:

Code
Public Sub New(ComicID As Integer)
      Dim ComicData As String
      If ComicID = 0 Then
      ComicData = HTTP.GetComicData(String.Format(BaseURL, ""))
      Else
      ComicData = HTTP.GetComicData(String.Format(BaseURL, ComicID))
      End If
      Dim DataParser As New Parser(ComicData)
      CID = DataParser.ParseIDVal("num")
      DateC = New DateTime(DataParser.ParseInt("year"), DataParser.ParseInt("month"), DataParser.ParseInt("day"))
      Trans = DataParser.ParseString("transcript")
      PTitle = DataParser.ParseString("title")
      ILink = DataParser.ParseString("img")
      AText = DataParser.ParseString("alt")
    End Sub

The Parser class and HTTP module are also both my own thing as well :) I'll update the OP with another download link. Quick note, every post has a JSON file you can just download the information from. The format is "http://xkcd.com/{0}/info.0.json", where {0} is the ID. If you want just the most recent, you can access "http://xkcd.com/info.0.json".
Ah thank you very much ill go check these out. Thanks for adding another link to :3

I knew I should of tested for a json version. So much easier.

EDIT: Having gone through the code i really like the layout and thought gone into this, my only question would be, why did you not put the json straight to a class using newtonsoft json libary?

Apart from that I can only say well done :) great neat bit of readable code there :)

Even tho chrome thinks is a malicious file lol. guess just vb.net in a zip sends warnings lol

usage Json newtonsoft's.

Code
Dim obj As JSON_result
    obj = JsonConvert.DeserializeObject(Of ComicClass)(JsonString from the webcall)

Json Class

Code
Public Class ComicClass
    Public Property month() As String
        Get
            Return m_month
        End Get
        Set
            m_month = Value
        End Set
    End Property
    Private m_month As String
    Public Property num() As Integer
        Get
            Return m_num
        End Get
        Set
            m_num = Value
        End Set
    End Property
    Private m_num As Integer
    Public Property link() As String
        Get
            Return m_link
        End Get
        Set
            m_link = Value
        End Set
    End Property
    Private m_link As String
    Public Property year() As String
        Get
            Return m_year
        End Get
        Set
            m_year = Value
        End Set
    End Property
    Private m_year As String
    Public Property news() As String
        Get
            Return m_news
        End Get
        Set
            m_news = Value
        End Set
    End Property
    Private m_news As String
    Public Property safe_title() As String
        Get
            Return m_safe_title
        End Get
        Set
            m_safe_title = Value
        End Set
    End Property
    Private m_safe_title As String
    Public Property transcript() As String
        Get
            Return m_transcript
        End Get
        Set
            m_transcript = Value
        End Set
    End Property
    Private m_transcript As String
    Public Property alt() As String
        Get
            Return m_alt
        End Get
        Set
            m_alt = Value
        End Set
    End Property
    Private m_alt As String
    Public Property img() As String
        Get
            Return m_img
        End Get
        Set
            m_img = Value
        End Set
    End Property
    Private m_img As String
    Public Property title() As String
        Get
            Return m_title
        End Get
        Set
            m_title = Value
        End Set
    End Property
    Private m_title As String
    Public Property day() As String
        Get
            Return m_day
        End Get
        Set
            m_day = Value
        End Set
    End Property
    Private m_day As String
End Class
(May 22, 2015 01:22 AM)sweetysuckscock Wrote:  Ah thank you very much ill go check these out. Thanks for adding another link to :3

I knew I should of tested for a json version. So much easier.

EDIT: Having gone through the code i really like the layout and thought gone into this, my only question would be, why did you not put the json straight to a class using newtonsoft json libary?

Apart from that I can only say well done :) great neat bit of readable code there :)

Even tho chrome thinks is a malicious file lol. guess just vb.net in a zip sends warnings lol

usage Json newtonsoft's.

Code
Dim obj As JSON_result
    obj = JsonConvert.DeserializeObject(Of ComicClass)(JsonString from the webcall)

Json Class

Code
Public Class ComicClass
    Public Property month() As String
        Get
            Return m_month
        End Get
        Set
            m_month = Value
        End Set
    End Property
    Private m_month As String
    Public Property num() As Integer
        Get
            Return m_num
        End Get
        Set
            m_num = Value
        End Set
    End Property
    Private m_num As Integer
    Public Property link() As String
        Get
            Return m_link
        End Get
        Set
            m_link = Value
        End Set
    End Property
    Private m_link As String
    Public Property year() As String
        Get
            Return m_year
        End Get
        Set
            m_year = Value
        End Set
    End Property
    Private m_year As String
    Public Property news() As String
        Get
            Return m_news
        End Get
        Set
            m_news = Value
        End Set
    End Property
    Private m_news As String
    Public Property safe_title() As String
        Get
            Return m_safe_title
        End Get
        Set
            m_safe_title = Value
        End Set
    End Property
    Private m_safe_title As String
    Public Property transcript() As String
        Get
            Return m_transcript
        End Get
        Set
            m_transcript = Value
        End Set
    End Property
    Private m_transcript As String
    Public Property alt() As String
        Get
            Return m_alt
        End Get
        Set
            m_alt = Value
        End Set
    End Property
    Private m_alt As String
    Public Property img() As String
        Get
            Return m_img
        End Get
        Set
            m_img = Value
        End Set
    End Property
    Private m_img As String
    Public Property title() As String
        Get
            Return m_title
        End Get
        Set
            m_title = Value
        End Set
    End Property
    Private m_title As String
    Public Property day() As String
        Get
            Return m_day
        End Get
        Set
            m_day = Value
        End Set
    End Property
    Private m_day As String
End Class

I know of Newtonsoft's library, just decided to do my own thing. I don't think there is any advantages to using it on such a small input.
(May 22, 2015 06:05 AM)iGuessSo Wrote:  I know of Newtonsoft's library, just decided to do my own thing. I don't think there is any advantages to using it on such a small input.

speed and less code for one lol. Plus use the correct tools for the job, if you parsing json use a json parsing engine etc.

But its always good to learn how to split strings from different formats.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by bsdpunkDec 6, 2014 03:08 AM 12 303 Dec 6, 2014 03:08 AM
Last Post: White Light
[.NET] by MurphyJul 23, 2013 09:59 PM 5 207 Jul 23, 2013 09:59 PM
Last Post: Felix.Frogg
[.NET] by iRtehLeet™Apr 5, 2013 10:11 AM 249 7,492 Apr 5, 2013 10:11 AM
Last Post: iRtehLeet™