Advertisement
Guest User

cgwallpapers

a guest
Feb 5th, 2015
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 40.51 KB | None | 0 0
  1. #Region " Usage Examples "
  2.  
  3. 'Public Class Form1
  4.  
  5. '    Public GameWallpaper As New GameWallpapersAPI(username:="e@mail.com", password:="password")
  6.  
  7. '    Dim wallIds As IEnumerable(Of Integer) = Enumerable.Range(0, 9999)
  8. '    Dim wallRes As GameWallpapersAPI.WallpaperResolution = GameWallpapersAPI.WallpaperResolution.Res1920x1080
  9. '    Dim wallInfo As GameWallpapersAPI.GameWallpaperInfo
  10.  
  11. '    Private Sub Button1_Click() Handles Button1.Click
  12.  
  13. '        MessageBox.Show(CStr(GameWallpaper.CheckLogin), "Account Login success?",
  14. '                        MessageBoxButtons.OK, MessageBoxIcon.Information)
  15.  
  16. '        Dim outputFolder As String = "C:\GameWallpapers"
  17. '        Dim fileSuccess As String = "C:\GameWallpaper Success.txt"
  18. '        Dim fileError As String = "C:\GameWallpaper Error.txt"
  19.  
  20. '        IO.File.WriteAllText(fileSuccess, "")
  21. '        IO.File.WriteAllText(fileError, "")
  22. '        If Not IO.Directory.Exists(outputFolder) Then
  23. '            IO.Directory.CreateDirectory(outputFolder)
  24. '        End If
  25.  
  26. 'For Each id As Integer In wallIds
  27.  
  28. '    Debug.WriteLine(CStr(id))
  29.  
  30. '    ' Download wallpaper.
  31. '    wallInfo = GameWallpaper.DownloadWallpaper(id, wallRes)
  32.  
  33. '    If wallInfo.ImageStream IsNot Nothing Then
  34.  
  35. '        ' Save image stream to disk.
  36. '        If wallInfo.ImageStream.Length <> 0 Then ' Image is ok.
  37.  
  38. '            Using img As Image = Image.FromStream(wallInfo.ImageStream)
  39. '                img.Save(IO.Path.Combine(outputFolder, wallInfo.Filename), wallInfo.ImageFormat)
  40. '            End Using
  41.  
  42. '            ' Record wallpaper success.
  43. '            IO.File.AppendAllText(fileSuccess, wallInfo.Url & Environment.NewLine)
  44.  
  45. '        Else ' Image contains errors.
  46. '            ' Record wallpaper error.
  47. '            IO.File.AppendAllText(fileError, wallInfo.Url & Environment.NewLine)
  48.  
  49. '        End If
  50.  
  51. '    Else
  52. '        ' Record wallpaper error.
  53. '        IO.File.AppendAllText(fileError, wallInfo.Url & Environment.NewLine)
  54.  
  55. '    End If
  56.  
  57. '    Threading.Thread.Sleep(3000)
  58.  
  59. 'Next id
  60.  
  61. '    End Sub
  62.  
  63. 'End Class
  64.  
  65. #End Region
  66.  
  67. #Region " Imports "
  68.  
  69. Imports System.Net
  70. Imports System.Web
  71. Imports System.IO
  72.  
  73. #End Region
  74.  
  75. #Region " GameWallpapersApi "
  76.  
  77. ''' <summary>
  78. '''
  79. ''' </summary>
  80. Public NotInheritable Class GameWallpapersAPI
  81.  
  82. #Region " Properties "
  83.  
  84.     ''' <summary>
  85.     ''' Gets the GameWallpapers account username.
  86.     ''' </summary>
  87.     ''' <value>The GameWallpapers account username.</value>
  88.     Public ReadOnly Property Username As String
  89.         Get
  90.             Return Me.username1
  91.         End Get
  92.     End Property
  93.     ''' <summary>
  94.     ''' The GameWallpapers account username.
  95.     ''' </summary>
  96.     Private ReadOnly username1 As String
  97.  
  98.     ''' <summary>
  99.     ''' Gets the GameWallpapers account password.
  100.     ''' </summary>
  101.     ''' <value>The GameWallpapers account password.</value>
  102.     Public ReadOnly Property Password As String
  103.         Get
  104.             Return Me.password1
  105.         End Get
  106.     End Property
  107.     ''' <summary>
  108.     ''' The GameWallpapers account password.
  109.     ''' </summary>
  110.     Private ReadOnly password1 As String
  111.  
  112.     ''' <summary>
  113.     ''' Determines whether the user is logged in GameWallpapers.
  114.     ''' </summary>
  115.     Private isLogged As Boolean
  116.  
  117.     ''' <summary>
  118.     ''' Gets the main GameWallpapers url.
  119.     ''' </summary>
  120.     ''' <value>The main GameWallpapers url.</value>
  121.     Public ReadOnly Property UrlMain As String
  122.         Get
  123.             Return "http://www.gamewallpapers.com/"
  124.         End Get
  125.     End Property
  126.  
  127.     ''' <summary>
  128.     ''' Gets the GameWallpapers login url.
  129.     ''' </summary>
  130.     ''' <value>The GameWallpapers login url.</value>
  131.     Public ReadOnly Property UrlLogin As String
  132.         Get
  133.             Return "http://www.gamewallpapers.com/login.php"
  134.         End Get
  135.     End Property
  136.  
  137.     ''' <summary>
  138.     ''' Gets the GameWallpapers get-wallpaper query string format.
  139.     ''' </summary>
  140.     ''' <value>The GameWallpapers get-wallpaper query string format.</value>
  141.     Public ReadOnly Property UrlLoginQueryFormat As String
  142.         Get
  143.             Return "action={0}&email={1}&wachtwoord={2}"
  144.         End Get
  145.     End Property
  146.  
  147.     ''' <summary>
  148.     ''' Gets the GameWallpapers get-wallpaper.
  149.     ''' </summary>
  150.     ''' <value>The GameWallpapers get-wallpaper.</value>
  151.     Public ReadOnly Property UrlGetWallpaper As String
  152.         Get
  153.             Return "http://www.gamewallpapers.com/members/getwallpaper.php"
  154.         End Get
  155.     End Property
  156.  
  157.     ''' <summary>
  158.     ''' Gets the GameWallpapers get-wallpaper query string format.
  159.     ''' </summary>
  160.     ''' <value>The GameWallpapers get-wallpaper query string format.</value>
  161.     Public ReadOnly Property UrlGetWallpaperQueryFormat As String
  162.         Get
  163.             Return "id={0}&res={1}"
  164.         End Get
  165.     End Property
  166.  
  167.     ''' <summary>
  168.     ''' Gets the GameWallpaper cookie container.
  169.     ''' </summary>
  170.     ''' <value>The cookie container.</value>
  171.     Public ReadOnly Property CookieCollection As CookieCollection
  172.         Get
  173.             Return Me.cookieCollection1
  174.         End Get
  175.     End Property
  176.     ''' <summary>
  177.     ''' The GameWallpapers cookie container.
  178.     ''' </summary>
  179.     Private cookieCollection1 As CookieCollection
  180.  
  181.     ''' <summary>
  182.     ''' Gets or sets the http user agent.
  183.     ''' </summary>
  184.     ''' <value>The http user agent.</value>
  185.     Public Property UserAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
  186.  
  187.     ''' <summary>
  188.     ''' Gets the GameWallpapers headers for a Login POST request.
  189.     ''' </summary>
  190.     ''' <value>The GameWallpapers headers for a Login POST request.</value>
  191.     Public ReadOnly Property RequestHeadersPostLogin As WebHeaderCollection
  192.         Get
  193.  
  194.             Dim headers As New WebHeaderCollection
  195.             With headers
  196.                 .Add("Accept-Language", "en-us,en;q=0.5")
  197.                 .Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  198.                 .Add("Keep-Alive", "300")
  199.             End With
  200.             Return headers
  201.  
  202.         End Get
  203.     End Property
  204.  
  205.     ''' <summary>
  206.     ''' Gets the GameWallpapers headers for a GET request.
  207.     ''' </summary>
  208.     ''' <value>The GameWallpapers headers for a GET request.</value>
  209.     Public ReadOnly Property RequestHeadersGet As WebHeaderCollection
  210.         Get
  211.             Return Me.RequestHeadersPostLogin
  212.         End Get
  213.     End Property
  214.  
  215.     ''' <summary>
  216.     ''' Gets the GameWallpapers headers for a GetWallpaper POST request.
  217.     ''' </summary>
  218.     ''' <value>The GameWallpapers headers for a GetWallpaper POST request.</value>
  219.     Public ReadOnly Property RequestHeadersGetWallpaper As WebHeaderCollection
  220.         Get
  221.  
  222.             Dim headers As New WebHeaderCollection
  223.             With headers
  224.                 .Add("Accept-Language", "en-US,en;q=0.5")
  225.                 .Add("Accept-Encoding", "gzip, deflate")
  226.                 .Add("Keep-Alive", "300")
  227.             End With
  228.             Return headers
  229.  
  230.         End Get
  231.     End Property
  232.  
  233. #End Region
  234.  
  235. #Region " Enumerations "
  236.  
  237.     ''' <summary>
  238.     ''' Specifies a GameWallpapers avaliable resolution.
  239.     ''' </summary>
  240.     <Serializable>
  241.     Public Enum WallpaperResolution
  242.  
  243.         ''' <summary>
  244.         ''' 1280X720, 720p resolution.
  245.         ''' </summary>
  246.         Res1280x720
  247.  
  248.         ''' <summary>
  249.         ''' 1280x768, 15:9 resolution.
  250.         ''' </summary>
  251.         Res1280x768
  252.  
  253.         ''' <summary>
  254.         ''' 1280x800, 16:10 resolution.
  255.         ''' </summary>
  256.         Res1280x800
  257.  
  258.         ''' <summary>
  259.         ''' _1360x768, 16:9 resolution.
  260.         ''' </summary>
  261.         Res1360x768
  262.  
  263.         ''' <summary>
  264.         ''' 1366x768, 16:9 resolution.
  265.         ''' </summary>
  266.         Res1366x768
  267.  
  268.         ''' <summary>
  269.         ''' 1440x900, 16:10 resolution.
  270.         ''' </summary>
  271.         Res1440x900
  272.  
  273.         ''' <summary>
  274.         ''' 1600x900, 16:9 resolution.
  275.         ''' </summary>
  276.         Res1600x900
  277.  
  278.         ''' <summary>
  279.         ''' 1680x945, 16:10 resolution.
  280.         ''' </summary>
  281.         Res1680x945
  282.  
  283.         ''' <summary>
  284.         ''' 1680x1050, 16:10 resolution.
  285.         ''' </summary>
  286.         Res1680x1050
  287.  
  288.         ''' <summary>
  289.         ''' 1920x1080, 1080p resolution.
  290.         ''' </summary>
  291.         Res1920x1080
  292.  
  293.         ''' <summary>
  294.         ''' 1920X1200, 16:10 resolution.
  295.         ''' </summary>
  296.         Res1920x1200
  297.  
  298.         ''' <summary>
  299.         ''' 2560x1440, 16:9 resolution.
  300.         ''' </summary>
  301.         Res2560x1440
  302.  
  303.         ''' <summary>
  304.         ''' 2560x1600, 16:10 resolution.
  305.         ''' </summary>
  306.         Res2560x1600
  307.  
  308.         ''' <summary>
  309.         ''' 3840x2160, 4K resolution.
  310.         ''' </summary>
  311.         Res3840x2160
  312.  
  313.     End Enum
  314.  
  315. #End Region
  316.  
  317. #Region " Types "
  318.  
  319.     ''' <summary>
  320.     ''' Defines a GameWallpaper.
  321.     ''' </summary>
  322.     <Serializable>
  323.     Public NotInheritable Class GameWallpaperInfo : Implements IDisposable
  324.  
  325.  
  326. #Region " Properties "
  327.  
  328.         ''' <summary>
  329.         ''' Gets the wallpaper imageformat.
  330.         ''' </summary>
  331.         ''' <value>The wallpaper imageformat.</value>
  332.         Public ReadOnly Property ImageFormat As Imaging.ImageFormat
  333.             Get
  334.                 Return Imaging.ImageFormat.Jpeg
  335.             End Get
  336.         End Property
  337.  
  338.         ''' <summary>
  339.         ''' Gets the wallpaper Identifier.
  340.         ''' </summary>
  341.         ''' <value>The wallpaper Identifier.</value>
  342.         Public ReadOnly Property Id As Integer
  343.             Get
  344.                 Return Me.id1
  345.             End Get
  346.         End Property
  347.         ''' <summary>
  348.         ''' The wallpaper Identifier.
  349.         ''' </summary>
  350.         Private ReadOnly id1 As Integer
  351.  
  352.         ''' <summary>
  353.         ''' Gets the wallpaper resolution.
  354.         ''' </summary>
  355.         ''' <value>The wallpaper resolution.</value>
  356.         Public ReadOnly Property Resolution As WallpaperResolution
  357.             Get
  358.                 Return Me.resolution1
  359.             End Get
  360.         End Property
  361.         ''' <summary>
  362.         ''' The wallpaper resolution.
  363.         ''' </summary>
  364.         Private ReadOnly resolution1 As WallpaperResolution
  365.  
  366.         ''' <summary>
  367.         ''' Gets the wallpaper filename.
  368.         ''' </summary>
  369.         ''' <value>The wallpaper filename.</value>
  370.         Public ReadOnly Property Filename As String
  371.             Get
  372.                 Return Me.filename1
  373.             End Get
  374.         End Property
  375.         ''' <summary>
  376.         ''' The wallpaper filename.
  377.         ''' </summary>
  378.         Private ReadOnly filename1 As String
  379.  
  380.         ''' <summary>
  381.         ''' Gets the Stream that contains the image.
  382.         ''' </summary>
  383.         ''' <value>The wallpaper filename.</value>
  384.         Public ReadOnly Property ImageStream As Stream
  385.             Get
  386.                 Return Me.imageStream1
  387.             End Get
  388.         End Property
  389.         ''' <summary>
  390.         ''' The Stream that contains the image.
  391.         ''' </summary>
  392.         Private ReadOnly imageStream1 As MemoryStream
  393.  
  394.         ''' <summary>
  395.         ''' Gets the wallpaper url.
  396.         ''' </summary>
  397.         ''' <value>The wallpaper url.</value>
  398.         Public ReadOnly Property Url As String
  399.             Get
  400.                 Return Me.url1
  401.             End Get
  402.         End Property
  403.         ''' <summary>
  404.         ''' The wallpaper url.
  405.         ''' </summary>
  406.         Private ReadOnly url1 As String
  407.  
  408. #End Region
  409.  
  410. #Region " Constructors "
  411.  
  412.         ''' <summary>
  413.         ''' Prevents a default instance of the <see cref="GameWallpaperInfo"/> class from being created.
  414.         ''' </summary>
  415.         Private Sub New()
  416.         End Sub
  417.  
  418.         ''' <summary>
  419.         ''' Initializes a new instance of the <see cref="GameWallpaperInfo"/> class.
  420.         ''' </summary>
  421.         ''' <param name="url">The wallpaper url.</param>
  422.         ''' <param name="id">The wallpaper identifier.</param>
  423.         ''' <param name="resolution">The wallpaper resolution.</param>
  424.         ''' <param name="filename">The wallpaper filename.</param>
  425.         ''' <param name="imageStream">The Stream that contains the image.</param>
  426.         Public Sub New(ByVal url As String,
  427.                        ByVal id As Integer,
  428.                        ByVal resolution As WallpaperResolution,
  429.                        Optional ByVal filename As String = Nothing,
  430.                        Optional ByVal imageStream As MemoryStream = Nothing)
  431.  
  432.             Me.url1 = url
  433.             Me.id1 = id
  434.             Me.resolution1 = resolution
  435.             Me.filename1 = filename
  436.             Me.imageStream1 = imageStream
  437.  
  438.         End Sub
  439.  
  440. #End Region
  441.  
  442. #Region " IDisposable "
  443.  
  444.         ''' <summary>
  445.         ''' To detect redundant calls when disposing.
  446.         ''' </summary>
  447.         Private isDisposed As Boolean = False
  448.  
  449.         ''' <summary>
  450.         ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  451.         ''' </summary>
  452.         Public Sub Dispose() Implements IDisposable.Dispose
  453.             Me.Dispose(isDisposing:=True)
  454.             GC.SuppressFinalize(obj:=Me)
  455.         End Sub
  456.  
  457.         ''' <summary>
  458.         ''' Releases unmanaged and - optionally - managed resources.
  459.         ''' </summary>
  460.         ''' <param name="isDisposing">
  461.         ''' <c>true</c> to release both managed and unmanaged resources;
  462.         ''' <c>false</c> to release only unmanaged resources.
  463.         ''' </param>
  464.         Protected Sub Dispose(ByVal isDisposing As Boolean)
  465.  
  466.             If Not Me.isDisposed Then
  467.  
  468.                 If isDisposing Then
  469.                     If Me.imageStream1 IsNot Nothing Then
  470.                         Me.imageStream1.Flush()
  471.                         Me.imageStream1.Close()
  472.                     End If
  473.                 End If
  474.  
  475.             End If
  476.  
  477.             Me.isDisposed = True
  478.  
  479.         End Sub
  480.  
  481. #End Region
  482.  
  483.     End Class
  484.  
  485.     ''' <summary>
  486.     ''' Defines the query data for a LoginPost request.
  487.     ''' </summary>
  488.     Private NotInheritable Class LoginQueryData
  489.  
  490.         ''' <summary>
  491.         ''' Gets the action field.
  492.         ''' </summary>
  493.         ''' <value>The action field.</value>
  494.         Public ReadOnly Property Action As String
  495.             Get
  496.                 Return "go"
  497.             End Get
  498.         End Property
  499.  
  500.         ''' <summary>
  501.         ''' Gets or sets the email field.
  502.         ''' </summary>
  503.         ''' <value>The email field.</value>
  504.         Public Property Email As String
  505.  
  506.         ''' <summary>
  507.         ''' Gets or sets the wachtwoord field.
  508.         ''' </summary>
  509.         ''' <value>The wachtwoord field.</value>
  510.         Public Property Wachtwoord As String
  511.  
  512.     End Class
  513.  
  514.     ''' <summary>
  515.     ''' Defines the query data for a GetWallpaper request.
  516.     ''' </summary>
  517.     Private NotInheritable Class WallpaperQueryData
  518.  
  519.         ''' <summary>
  520.         ''' Gets or sets the id field.
  521.         ''' </summary>
  522.         ''' <value>The id field.</value>
  523.         Public Property Id As String
  524.  
  525.         ''' <summary>
  526.         ''' Gets or sets res wachtwoord field.
  527.         ''' </summary>
  528.         ''' <value>The res field.</value>
  529.         Public Property Res As String
  530.  
  531.     End Class
  532.  
  533. #End Region
  534.  
  535. #Region " Exceptions "
  536.  
  537.     ''' <summary>
  538.     ''' Exception that is thrown when GameWallpapers server does not sends a response.
  539.     ''' </summary>
  540.     <Serializable>
  541.     Public NotInheritable Class NoResponseReceivedException : Inherits Exception
  542.  
  543.         ''' <summary>
  544.         ''' Initializes a new instance of the <see cref="NoResponseReceivedException"/> class.
  545.         ''' </summary>
  546.         Public Sub New()
  547.             MyBase.New("No response received from server")
  548.         End Sub
  549.  
  550.         ''' <summary>
  551.         ''' Initializes a new instance of the <see cref="NoResponseReceivedException"/> class.
  552.         ''' </summary>
  553.         ''' <param name="message">The message that describes the error.</param>
  554.         Public Sub New(message As String)
  555.             MyBase.New(message)
  556.         End Sub
  557.  
  558.         ''' <summary>
  559.         ''' Initializes a new instance of the <see cref="NoResponseReceivedException"/> class.
  560.         ''' </summary>
  561.         ''' <param name="message">The message that describes the error.</param>
  562.         ''' <param name="inner">The inner exception.</param>
  563.         Public Sub New(message As String, inner As Exception)
  564.             MyBase.New(message, inner)
  565.         End Sub
  566.  
  567.     End Class
  568.  
  569.     ''' <summary>
  570.     ''' Exception that is thrown when cannot login to GameWallpapers.
  571.     ''' </summary>
  572.     <Serializable>
  573.     Public NotInheritable Class LoginRejectedException : Inherits Exception
  574.  
  575.         ''' <summary>
  576.         ''' Initializes a new instance of the <see cref="LoginRejectedException"/> class.
  577.         ''' </summary>
  578.         Public Sub New()
  579.             MyBase.New("Cannot login to server.")
  580.         End Sub
  581.  
  582.         ''' <summary>
  583.         ''' Initializes a new instance of the <see cref="LoginRejectedException"/> class.
  584.         ''' </summary>
  585.         ''' <param name="message">The message that describes the error.</param>
  586.         Public Sub New(message As String)
  587.             MyBase.New(message)
  588.         End Sub
  589.  
  590.         ''' <summary>
  591.         ''' Initializes a new instance of the <see cref="LoginRejectedException"/> class.
  592.         ''' </summary>
  593.         ''' <param name="message">The message that describes the error.</param>
  594.         ''' <param name="inner">The inner exception.</param>
  595.         Public Sub New(message As String, inner As Exception)
  596.             MyBase.New(message, inner)
  597.         End Sub
  598.  
  599.     End Class
  600.  
  601.     ''' <summary>
  602.     ''' Exception that is thrown when a GET request is rejected.
  603.     ''' </summary>
  604.     <Serializable>
  605.     Public NotInheritable Class GetRequestRejectedException : Inherits Exception
  606.  
  607.         ''' <summary>
  608.         ''' Initializes a new instance of the <see cref="GetRequestRejectedException"/> class.
  609.         ''' </summary>
  610.         Public Sub New()
  611.             MyBase.New("Cannot login to server.")
  612.         End Sub
  613.  
  614.         ''' <summary>
  615.         ''' Initializes a new instance of the <see cref="GetRequestRejectedException"/> class.
  616.         ''' </summary>
  617.         ''' <param name="message">The message that describes the error.</param>
  618.         Public Sub New(message As String)
  619.             MyBase.New(message)
  620.         End Sub
  621.  
  622.         ''' <summary>
  623.         ''' Initializes a new instance of the <see cref="GetRequestRejectedException"/> class.
  624.         ''' </summary>
  625.         ''' <param name="message">The message that describes the error.</param>
  626.         ''' <param name="inner">The inner exception.</param>
  627.         Public Sub New(message As String, inner As Exception)
  628.             MyBase.New(message, inner)
  629.         End Sub
  630.  
  631.     End Class
  632.  
  633. #End Region
  634.  
  635. #Region " Constructors "
  636.  
  637.     ''' <summary>
  638.     ''' Prevents a default instance of the <see cref="GameWallpapersApi"/> class from being created.
  639.     ''' </summary>
  640.     Private Sub New()
  641.     End Sub
  642.  
  643.     ''' <summary>
  644.     ''' Initializes a new instance of the <see cref="GameWallpapersApi"/> class.
  645.     ''' </summary>
  646.     ''' <param name="username">The GameWallpapers account username.</param>
  647.     ''' <param name="password">The GameWallpapers account password.</param>
  648.     Public Sub New(ByVal username As String,
  649.                    ByVal password As String)
  650.  
  651.         Me.username1 = username
  652.         Me.password1 = password
  653.         Me.cookieCollection1 = New CookieCollection
  654.  
  655.     End Sub
  656.  
  657. #End Region
  658.  
  659. #Region " Public Methods "
  660.  
  661.     ''' <summary>
  662.     ''' Determines whether the account can log in GameWallpapers.
  663.     ''' </summary>
  664.     ''' <returns><c>true</c> if the account can log in GameWallpapers, <c>false</c> otherwise.</returns>
  665.     Public Function CheckLogin() As Boolean
  666.  
  667.         If Me.GetMethod(Me.UrlMain, Me.cookieCollection1) Then
  668.             Return Me.PostLoginMethod(Me.GetLoginQueryData, Me.cookieCollection1)
  669.  
  670.         Else
  671.             Return False
  672.  
  673.         End If ' Me.GetMethod
  674.  
  675.     End Function
  676.  
  677.     ''' <summary>
  678.     ''' Downloads the specified wallpaper.
  679.     ''' </summary>
  680.     ''' <param name="id">The wallpaper identifier.</param>
  681.     ''' <param name="res">The desired wallpaper resource (maybe could not be avaliable for the specified wallpaper).</param>
  682.     Public Function DownloadWallpaper(ByVal id As Integer,
  683.                                       ByVal res As WallpaperResolution) As GameWallpaperInfo
  684.  
  685.         ' Set the query data.
  686.         Dim wallpaperData As New WallpaperQueryData With
  687.            {
  688.                 .Id = CStr(id),
  689.                 .Res = res.ToString.Remove(0, "res".Length)
  690.             }
  691.  
  692.         If Me.isLogged Then
  693.             Return Me.GetWallpaperMethod(wallpaperData, Me.cookieCollection1)
  694.  
  695.         Else
  696.  
  697.             If Me.GetMethod(Me.UrlMain, Me.cookieCollection1) Then
  698.  
  699.                 If Me.PostLoginMethod(Me.GetLoginQueryData, Me.cookieCollection1) Then
  700.                     Return Me.GetWallpaperMethod(wallpaperData, Me.cookieCollection1)
  701.  
  702.                 Else ' Cannot login the account.
  703.                     Throw New LoginRejectedException
  704.                     Return Nothing
  705.  
  706.                 End If ' Me.PostLoginMethod
  707.  
  708.             Else
  709.                 Throw New GetRequestRejectedException
  710.                 Return Nothing
  711.  
  712.             End If ' Me.GetMethod
  713.  
  714.         End If
  715.  
  716.     End Function
  717.  
  718. #End Region
  719.  
  720. #Region " Private Methods "
  721.  
  722.     ''' <summary>
  723.     ''' Converts cookie string to global cookie collection object.
  724.     ''' </summary>
  725.     ''' <param name="cookie">The cookie string.</param>
  726.     ''' <param name="cookieCollection">The cookie collection.</param>
  727.     Private Sub SaveCookies(ByVal cookie As String,
  728.                             ByRef cookieCollection As CookieCollection)
  729.  
  730.         Dim cookieStrings() As String = cookie.Trim.
  731.                                                Replace("path=/,", String.Empty).
  732.                                                Replace("path=/", String.Empty).
  733.                                                Split({";"c}, StringSplitOptions.RemoveEmptyEntries)
  734.  
  735.         cookieCollection = New CookieCollection
  736.  
  737.         For Each cookieString As String In cookieStrings
  738.  
  739.             If Not String.IsNullOrEmpty(cookieString.Trim) Then
  740.  
  741.                 cookieCollection.Add(New Cookie(name:=cookieString.Trim.Split("="c)(0),
  742.                                                 value:=cookieString.Trim.Split("="c)(1)))
  743.  
  744.             End If
  745.  
  746.         Next cookieString
  747.  
  748.     End Sub
  749.  
  750.     ''' <summary>
  751.     ''' Convert cookie container object to global cookie collection object.
  752.     ''' </summary>
  753.     ''' <param name="cookieContainer">The cookie container.</param>
  754.     ''' <param name="cookieCollection">The cookie collection.</param>
  755.     ''' <param name="url">The url.</param>
  756.     Private Sub SaveCookies(ByVal cookieContainer As CookieContainer,
  757.                             ByRef cookieCollection As CookieCollection,
  758.                             ByVal url As String)
  759.  
  760.         cookieCollection = New CookieCollection
  761.  
  762.         For Each cookie As Cookie In cookieContainer.GetCookies(New Uri(url))
  763.  
  764.             cookieCollection.Add(cookie)
  765.  
  766.         Next cookie
  767.  
  768.     End Sub
  769.  
  770.     ''' <summary>
  771.     ''' Sets the cookie container.
  772.     ''' </summary>
  773.     ''' <param name="url">The url.</param>
  774.     ''' <param name="cookieCollection">The cookie collection.</param>
  775.     ''' <returns>CookieContainer.</returns>
  776.     Private Function SetCookieContainer(ByVal url As String,
  777.                                         ByVal cookieCollection As CookieCollection) As CookieContainer
  778.  
  779.         Dim cookieContainer As New CookieContainer
  780.         Dim refDate As Date
  781.  
  782.         For Each oldCookie As Cookie In cookieCollection
  783.  
  784.             If Not DateTime.TryParse(oldCookie.Value, refDate) Then
  785.  
  786.                 Dim newCookie As New Cookie
  787.                 With newCookie
  788.                     .Name = oldCookie.Name
  789.                     .Value = oldCookie.Value
  790.                     .Domain = New Uri(url).Host
  791.                     .Secure = False
  792.                 End With
  793.  
  794.                 cookieContainer.Add(newCookie)
  795.  
  796.             End If
  797.  
  798.         Next oldCookie
  799.  
  800.         Return cookieContainer
  801.  
  802.     End Function
  803.  
  804.     ''' <summary>
  805.     ''' Gets a new <see cref="LoginQueryData"/> instance using the current account credentials.
  806.     ''' </summary>
  807.     ''' <returns>A new <see cref="LoginQueryData"/> instance using the current account credentials.</returns>
  808.     Private Function GetLoginQueryData() As LoginQueryData
  809.  
  810.         Return New LoginQueryData With
  811.               {
  812.                   .Email = HttpUtility.UrlEncode(Me.username1),
  813.                   .Wachtwoord = HttpUtility.UrlEncode(Me.password1)
  814.               }
  815.  
  816.     End Function
  817.  
  818.     ''' <summary>
  819.     ''' Gets a formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.
  820.     ''' </summary>
  821.     ''' <param name="loginQueryData">The <see cref="LoginQueryData"/> object that contains the login query fields.</param>
  822.     ''' <returns>A formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.</returns>
  823.     Private Function GetLoginQueryString(ByVal loginQueryData As LoginQueryData) As String
  824.  
  825.         Return String.Format(Me.UrlLoginQueryFormat,
  826.                              loginQueryData.Action, loginQueryData.Email, loginQueryData.Wachtwoord)
  827.  
  828.     End Function
  829.  
  830.     ''' <summary>
  831.     ''' Gets a formatted <see cref="String"/> representation of a <see cref="WallpaperQueryData"/> object.
  832.     ''' </summary>
  833.     ''' <param name="wallpaperQueryData">The <see cref="WallpaperQueryData"/> object that contains the 'GetWallpaper' query fields.</param>
  834.     ''' <returns>A formatted <see cref="String"/> representation of a <see cref="LoginQueryData"/> object.</returns>
  835.     Private Function GetWallpaperQueryString(ByVal wallpaperQueryData As WallpaperQueryData) As String
  836.  
  837.         Return String.Format(Me.UrlGetWallpaperQueryFormat,
  838.                              wallpaperQueryData.Id, wallpaperQueryData.Res)
  839.  
  840.     End Function
  841.  
  842.     ''' <summary>
  843.     '''
  844.     ''' </summary>
  845.     ''' <param name="url">The url.</param>
  846.     ''' <param name="cookieCollection">The cookie collection.</param>
  847.     ''' <returns><c>true</c> if successfull, <c>false</c> otherwise.</returns>
  848.     Private Function GetMethod(ByVal url As String,
  849.                                ByRef cookieCollection As CookieCollection) As Boolean
  850.  
  851.         Dim request As HttpWebRequest = Nothing
  852.         Dim response As HttpWebResponse = Nothing
  853.         Dim sr As StreamReader = Nothing
  854.         Dim result As Boolean = False
  855.  
  856.         Try
  857.  
  858. #If DEBUG Then
  859.             Debug.WriteLine("[+] GetMethod method started.")
  860. #End If
  861.  
  862. #If DEBUG Then
  863.             Debug.WriteLine("[+] Attempting to perform a request with:")
  864.             Debug.WriteLine(String.Format("Method: {0}", "GET"))
  865.             Debug.WriteLine(String.Format("UserAgent: {0}", Me.UserAgent))
  866.             Debug.WriteLine(String.Format("Accept: {0}", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"))
  867.             Debug.WriteLine(String.Format("Headers: {0}", String.Join(Environment.NewLine, Me.RequestHeadersGet)))
  868. #End If
  869.  
  870.             request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
  871.             With request
  872.                 .Method = "GET"
  873.                 .Headers = Me.RequestHeadersGet
  874.                 .Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
  875.                 .UserAgent = Me.UserAgent
  876.                 .AllowAutoRedirect = False
  877.                 .KeepAlive = True
  878.             End With
  879.  
  880. #If DEBUG Then
  881.             Debug.WriteLine("[-] Request done.")
  882. #End If
  883.  
  884. #If DEBUG Then
  885.             Debug.WriteLine("[+] Getting server response...")
  886. #End If
  887.  
  888.             ' Get the server response.
  889.             response = DirectCast(request.GetResponse, HttpWebResponse)
  890.  
  891. #If DEBUG Then
  892.             Debug.WriteLine("[-] Getting server response done.")
  893. #End If
  894.  
  895.             If request.HaveResponse Then
  896.  
  897. #If DEBUG Then
  898.                 Debug.WriteLine("[+] Saving cookies...")
  899. #End If
  900.  
  901.                 ' Save the cookie info.
  902.                 Me.SaveCookies(response.Headers("Set-Cookie"), cookieCollection)
  903. #If DEBUG Then
  904.                 Debug.WriteLine("[-] Saving cookies done.")
  905. #End If
  906.  
  907. #If DEBUG Then
  908.                 Debug.WriteLine("[+] Getting server response...")
  909. #End If
  910.                 ' Get the server response.
  911.                 response = DirectCast(request.GetResponse, HttpWebResponse)
  912. #If DEBUG Then
  913.                 Debug.WriteLine("[-] Getting server response done.")
  914. #End If
  915.  
  916. #If DEBUG Then
  917.                 Debug.WriteLine("[+] Reading server response...")
  918. #End If
  919.                 sr = New StreamReader(response.GetResponseStream)
  920.                 Using sr
  921.                     ' Read the response from the server, but we do not save it.
  922.                     sr.ReadToEnd()
  923.                 End Using
  924.                 result = True
  925.  
  926. #If DEBUG Then
  927.                 Debug.WriteLine("[-] Reading server response done.")
  928. #End If
  929.  
  930.             Else ' No response received from server.
  931.                 Throw New NoResponseReceivedException(String.Format("No response received from server with url: {0}", url))
  932.                 result = False
  933.  
  934.             End If
  935.  
  936.         Catch ex As Exception
  937.             Throw
  938.             result = False
  939.  
  940.         Finally
  941.             If sr IsNot Nothing Then
  942.                 sr.Dispose()
  943.             End If
  944.             If response IsNot Nothing Then
  945.                 response.Close()
  946.             End If
  947.  
  948.         End Try
  949.  
  950. #If DEBUG Then
  951.         Debug.WriteLine("[-] GetMethod method finished.")
  952.         Debug.WriteLine("[i] Returning result value...")
  953. #End If
  954.  
  955.         Return result
  956.  
  957.     End Function
  958.  
  959.     ''' <summary>
  960.     '''
  961.     ''' </summary>
  962.     ''' <param name="loginData">The login post data.</param>
  963.     ''' <param name="cookieCollection">The cookie collection.</param>
  964.     ''' <returns><c>true</c> if successfull, <c>false</c> otherwise.</returns>
  965.     Private Function PostLoginMethod(ByVal loginData As LoginQueryData,
  966.                                      ByRef cookieCollection As CookieCollection) As Boolean
  967.  
  968.         Dim request As HttpWebRequest = Nothing
  969.         Dim response As HttpWebResponse = Nothing
  970.         Dim sw As StreamWriter = Nothing
  971.         Dim initialCookieCount As Integer = 0
  972.         Dim postData As String
  973.         Dim result As Boolean = False
  974.  
  975.         Try
  976.  
  977. #If DEBUG Then
  978.             Debug.WriteLine("[+] PostLoginMethod method started.")
  979. #End If
  980.  
  981. #If DEBUG Then
  982.             Debug.WriteLine("[+] Attempting to perform a login request with:")
  983.             Debug.WriteLine(String.Format("Method: {0}", "POST"))
  984.             Debug.WriteLine(String.Format("UserAgent: {0}", Me.UserAgent))
  985.             Debug.WriteLine(String.Format("Referer: {0}", Me.UrlMain))
  986.             Debug.WriteLine(String.Format("Accept: {0}", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"))
  987.             Debug.WriteLine(String.Format("ContentType: {0}", "application/x-www-form-urlencoded"))
  988.             Debug.WriteLine(String.Format("Headers: {0}", String.Join(Environment.NewLine, Me.RequestHeadersPostLogin)))
  989. #End If
  990.  
  991.             request = DirectCast(HttpWebRequest.Create(Me.UrlLogin), HttpWebRequest)
  992.             With request
  993.                 .Method = "POST"
  994.                 .Headers = Me.RequestHeadersPostLogin
  995.                 .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  996.                 .ContentType = "application/x-www-form-urlencoded"
  997.                 .UserAgent = Me.UserAgent
  998.                 .Referer = Me.UrlMain
  999.             End With
  1000.  
  1001. #If DEBUG Then
  1002.             Debug.WriteLine("[-] Request done.")
  1003. #End If
  1004.  
  1005. #If DEBUG Then
  1006.             Debug.WriteLine("[+] Passing request cookie info...")
  1007. #End If
  1008.  
  1009.             If cookieCollection IsNot Nothing Then ' Pass cookie info from the login page.
  1010.                 request.CookieContainer = Me.SetCookieContainer(Me.UrlLogin, cookieCollection)
  1011.             End If
  1012.  
  1013. #If DEBUG Then
  1014.             Debug.WriteLine("[-] Passing request cookie info done.")
  1015. #End If
  1016.  
  1017. #If DEBUG Then
  1018.             Debug.WriteLine("[+] Setting post data with:")
  1019.             Debug.WriteLine(String.Format("Action: {0}", loginData.Action))
  1020.             Debug.WriteLine(String.Format("Email: {0}", loginData.Email))
  1021.             Debug.WriteLine(String.Format("Wachtwoord: {0}", loginData.Wachtwoord))
  1022. #End If
  1023.  
  1024.             ' Set the post data.
  1025.             postData = Me.GetLoginQueryString(loginData)
  1026.  
  1027.             sw = New StreamWriter(request.GetRequestStream)
  1028.             Using sw
  1029.                 sw.Write(postData) ' Post the data to the server.
  1030.             End Using
  1031.  
  1032. #If DEBUG Then
  1033.             Debug.WriteLine("[-] Setting post data done.")
  1034. #End If
  1035.  
  1036. #If DEBUG Then
  1037.             Debug.WriteLine("[+] Getting server response...")
  1038. #End If
  1039.  
  1040.             ' Get the server response.
  1041.             initialCookieCount = request.CookieContainer.Count
  1042.             response = DirectCast(request.GetResponse, HttpWebResponse)
  1043.  
  1044. #If DEBUG Then
  1045.             Debug.WriteLine("[-] Getting server response done.")
  1046. #End If
  1047.  
  1048.             If request.CookieContainer.Count > initialCookieCount Then ' Login successful.
  1049.                 result = True
  1050.  
  1051.             Else ' Login unsuccessful.
  1052.                 result = False
  1053.  
  1054.             End If
  1055.  
  1056. #If DEBUG Then
  1057.             Debug.WriteLine(String.Format("[i] Login response result is: {0}",
  1058.                                           If(result, "Successful (True)",
  1059.                                                      "Unsuccessful (False)")))
  1060. #End If
  1061.  
  1062.             If result Then ' Save new login cookies.
  1063. #If DEBUG Then
  1064.                 Debug.WriteLine("[+] Saving new login cookies...")
  1065. #End If
  1066.                 Me.SaveCookies(request.CookieContainer, cookieCollection, Me.UrlMain)
  1067. #If DEBUG Then
  1068.                 Debug.WriteLine("[-] Saving new login cookies done.")
  1069. #End If
  1070.             End If
  1071.  
  1072.         Catch ex As Exception
  1073.             Throw
  1074.  
  1075.         Finally
  1076.             If sw IsNot Nothing Then
  1077.                 sw.Dispose()
  1078.             End If
  1079.             If response IsNot Nothing Then
  1080.                 response.Close()
  1081.             End If
  1082.  
  1083.         End Try
  1084.  
  1085. #If DEBUG Then
  1086.         Debug.WriteLine("[-] PostLoginMethod method finished.")
  1087.         Debug.WriteLine("[i] Returning result value...")
  1088. #End If
  1089.  
  1090.         Me.isLogged = result
  1091.         Return result
  1092.  
  1093.     End Function
  1094.  
  1095.     ''' <summary>
  1096.     ''' Tries to download the specified wallpaper from GameWallpapers server.
  1097.     ''' </summary>
  1098.     ''' <param name="wallpaperQueryData">The wallpaper query data.</param>
  1099.     ''' <param name="cookieCollection">The cookie collection.</param>
  1100.     ''' <returns>A <see cref="GameWallpaperInfo"/> containing the wallpaper stream and it's info.</returns>
  1101.     Private Function GetWallpaperMethod(ByVal wallpaperQueryData As WallpaperQueryData,
  1102.                                         ByRef cookieCollection As CookieCollection) As GameWallpaperInfo
  1103.  
  1104.         Dim request As HttpWebRequest
  1105.         Dim url As String = Me.UrlGetWallpaper & "?" & Me.GetWallpaperQueryString(wallpaperQueryData)
  1106.         Dim contentDisposition As String
  1107.         Dim webResponse As WebResponse = Nothing
  1108.         Dim responseStream As Stream = Nothing
  1109.         Dim imageStream As MemoryStream = Nothing
  1110.         Dim wallInfo As GameWallpaperInfo = Nothing
  1111.  
  1112. #If DEBUG Then
  1113.         Debug.WriteLine("[+] GetWallpaperMethod method started.")
  1114. #End If
  1115.  
  1116. #If DEBUG Then
  1117.         Debug.WriteLine("[+] Attempting to perform a GetWallpaper request with:")
  1118.         Debug.WriteLine(String.Format("Method: {0}", "GET"))
  1119.         Debug.WriteLine(String.Format("UserAgent: {0}", Me.UserAgent))
  1120.         Debug.WriteLine(String.Format("Accept: {0}", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"))
  1121.         Debug.WriteLine(String.Format("ContentType: {0}", "application/x-www-form-urlencoded"))
  1122.         Debug.WriteLine(String.Format("Headers: {0}", String.Join(Environment.NewLine, Me.RequestHeadersGetWallpaper)))
  1123. #End If
  1124.  
  1125.         Try
  1126.             request = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
  1127.             With request
  1128.                 .Method = "GET"
  1129.                 .Headers = Me.RequestHeadersGetWallpaper
  1130.                 .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  1131.                 .AllowAutoRedirect = False
  1132.                 .UserAgent = Me.UserAgent
  1133.                 .KeepAlive = True
  1134.             End With
  1135.  
  1136. #If DEBUG Then
  1137.             Debug.WriteLine("[-] Request done.")
  1138. #End If
  1139.  
  1140. #If DEBUG Then
  1141.             Debug.WriteLine("[+] Passing request cookie info...")
  1142. #End If
  1143.  
  1144.             If cookieCollection IsNot Nothing Then
  1145.                 ' Pass cookie info so that we remain logged in.
  1146.                 request.CookieContainer = Me.SetCookieContainer(url, cookieCollection)
  1147.             End If
  1148.  
  1149. #If DEBUG Then
  1150.             Debug.WriteLine("[-] Passing request cookie done.")
  1151. #End If
  1152.  
  1153. #If DEBUG Then
  1154.             Debug.WriteLine("[+] Getting response...")
  1155. #End If
  1156.  
  1157.             webResponse = request.GetResponse
  1158.  
  1159. #If DEBUG Then
  1160.             Debug.WriteLine("[-] Getting response done.")
  1161. #End If
  1162.  
  1163.             Using webResponse
  1164.  
  1165.                 contentDisposition = CType(webResponse, HttpWebResponse).Headers("Content-Disposition")
  1166.  
  1167.                 If Not String.IsNullOrEmpty(contentDisposition) Then ' There is an image to download.
  1168.  
  1169.                     Dim filename As String = contentDisposition.Substring(contentDisposition.IndexOf("=") + "=".Length).
  1170.                                              TrimStart(" "c).TrimEnd({" "c, ";"c})
  1171.  
  1172.                     Try
  1173.                         imageStream = New MemoryStream
  1174.                         responseStream = webResponse.GetResponseStream
  1175.  
  1176. #If DEBUG Then
  1177.                         Debug.WriteLine("[+] Reading image stream...")
  1178. #End If
  1179.  
  1180.                         Using responseStream
  1181.  
  1182.                             Dim buffer(2047) As Byte
  1183.                             Dim read As Integer
  1184.  
  1185.                             Do
  1186.                                 read = responseStream.Read(buffer, 0, buffer.Length)
  1187.                                 imageStream.Write(buffer, 0, read)
  1188.                             Loop Until read = 0
  1189.  
  1190.                             responseStream.Close()
  1191.  
  1192.                         End Using
  1193.  
  1194. #If DEBUG Then
  1195.                         Debug.WriteLine("[-] Reading image stream done.")
  1196. #End If
  1197.  
  1198.                     Catch ex As Exception
  1199.                         Throw
  1200.  
  1201.                     End Try
  1202.  
  1203. #If DEBUG Then
  1204.                     Debug.WriteLine("[+] Setting wallpaper info object...")
  1205. #End If
  1206.  
  1207.                     wallInfo = New GameWallpaperInfo(url:=url,
  1208.                                                    id:=CInt(wallpaperQueryData.Id),
  1209.                                                    resolution:=DirectCast([Enum].Parse(GetType(WallpaperResolution),
  1210.                                                                                        "Res" & wallpaperQueryData.Res,
  1211.                                                                                        ignoreCase:=True), WallpaperResolution),
  1212.                                                    filename:=filename,
  1213.                                                    imageStream:=imageStream)
  1214.  
  1215. #If DEBUG Then
  1216.                     Debug.WriteLine("[-] Setting wallpaper info object done.")
  1217. #End If
  1218.  
  1219.                 End If ' String.IsNullOrEmpty(contentDisposition)
  1220.  
  1221.             End Using ' webResponse
  1222.  
  1223.         Catch ex As Exception
  1224.             Throw
  1225.  
  1226.         Finally
  1227.             If webResponse IsNot Nothing Then
  1228.                 webResponse.Close()
  1229.             End If
  1230.             If responseStream IsNot Nothing Then
  1231.                 responseStream.Close()
  1232.             End If
  1233.  
  1234.         End Try
  1235.  
  1236. #If DEBUG Then
  1237.         Debug.WriteLine("[-] GetWallpaperMethod method finished.")
  1238.         Debug.WriteLine("[i] Returning wallInfo object...")
  1239. #End If
  1240.  
  1241.         If wallInfo Is Nothing Then
  1242.  
  1243.             wallInfo = New GameWallpaperInfo(url:=url,
  1244.                                            id:=CInt(wallpaperQueryData.Id),
  1245.                                            resolution:=DirectCast([Enum].Parse(GetType(WallpaperResolution),
  1246.                                                                           "Res" & wallpaperQueryData.Res,
  1247.                                                                           ignoreCase:=True), WallpaperResolution))
  1248.         End If
  1249.  
  1250.         Return wallInfo
  1251.  
  1252.     End Function
  1253.  
  1254. #End Region
  1255.  
  1256. End Class
  1257.  
  1258. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement