Guest User

Imgur Upload Class

a guest
Jun 5th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports imgur_upload.Utility.Http
  2. Imports System.IO
  3. Imports System.Collections.Specialized
  4.  
  5. '''Coded by loyalty
  6. '''Uses HTTP Class by idb
  7. '''HTTP CLASS: http://s.olution.cc/utility-http/
  8. '''Dated: June,03,2012
  9. '''Credits to idb for the HTTP Class
  10.  
  11. Namespace Services
  12.     Public Class Imgur
  13.         Implements IDisposable
  14. #Region "Declerations"
  15.  
  16.         Private http As Utility.Http = Nothing
  17.  
  18. #End Region
  19.  
  20. #Region "Structures"
  21.  
  22.         Public Structure Result
  23.             Dim Success As Boolean
  24.             Dim ErrMsg As String
  25.             Dim iLink As String
  26.             Dim DirectLink As String
  27.         End Structure
  28.  
  29. #End Region
  30.  
  31. #Region "Properties"
  32.  
  33.         Private _filepath As String = String.Empty
  34.         Public Property FilePath As String
  35.             Get
  36.                 Return _filepath
  37.             End Get
  38.             Set(ByVal value As String)
  39.                 _filepath = value
  40.             End Set
  41.         End Property
  42.  
  43.         Public ReadOnly Property FileName As String
  44.             Get
  45.                 Return New FileInfo(FilePath).Name
  46.             End Get
  47.         End Property
  48.  
  49.         Private ReadOnly Property FileExtension As String
  50.             Get
  51.                 Return New FileInfo(FilePath).Extension
  52.             End Get
  53.         End Property
  54.  
  55.         Private ReadOnly Property Headers As NameValueCollection
  56.             Get
  57.                 Dim _Headers As New NameValueCollection
  58.                 _Headers.Add("X-Requested-With", "XMLHttpRequest")
  59.                 _Headers.Add("Pragma", "no-cache")
  60.                 _Headers.Add("Cache-Control", "no-cache")
  61.                 Return _Headers
  62.             End Get
  63.         End Property
  64.  
  65.         Private ReadOnly Property Fields As NameValueCollection
  66.             Get
  67.                 Dim _fields As New NameValueCollection
  68.                 _fields.Add("album_title", "Optional Album Title")
  69.                 _fields.Add("layout", "b")
  70.                 Return _fields
  71.             End Get
  72.         End Property
  73.  
  74.         Private ReadOnly Property UploadArgs As UploadData
  75.             Get
  76.                 Return New UploadData(File.ReadAllBytes(FilePath), FileName, "file")
  77.             End Get
  78.         End Property
  79.  
  80. #End Region
  81.  
  82. #Region "Constructor"
  83.  
  84.         Public Sub New()
  85.             Me.NewSession()
  86.         End Sub
  87.  
  88.         Public Sub New(ByVal imagepath As String)
  89.             Me.NewSession()
  90.             FilePath = imagepath
  91.         End Sub
  92.  
  93.         Public Sub New(ByVal imagepath As String, ByRef result As Result)
  94.             Me.NewSession()
  95.             FilePath = imagepath
  96.             result = Upload()
  97.         End Sub
  98.  
  99. #End Region
  100.  
  101. #Region "Private Methods"
  102.  
  103.         Private Sub NewSession()
  104.             If Not IsNothing(http) Then http.Dispose()
  105.             http = New Utility.Http
  106.         End Sub
  107.  
  108.         Private Function GetHash() As String
  109.             Dim res As HttpResponse = http.GetResponse(Verb._GET, "http://imgur.com/")
  110.             If Not IsNothing(res.RequestError) Then Throw New Exception(res.RequestError.Message)
  111.             Return http.ParseBetween(res.Html, "var sid_hash   = '", "';", "var sid_hash   = '".Length)
  112.         End Function
  113.  
  114. #End Region
  115.  
  116. #Region "upload"
  117.  
  118.         Public Function Upload() As Result
  119.  
  120.             If Not IsNothing(http) Then http.Dispose()
  121.             Me.NewSession()
  122.  
  123.             With http
  124.  
  125.                 Dim Result As New Result
  126.                 Try
  127.                     .TimeOut = -1
  128.                     .Accept = "application/json, text/javascript, */*; q=0.01"
  129.                     .Referer = "http://imgur.com/"
  130.                     Dim res As HttpResponse = .GetResponse(Verb._POST, String.Format("http://imgur.com/upload?sid_hash={0}", GetHash()), Headers, Fields, UploadArgs)
  131.                     If Not IsNothing(res.RequestError) Then
  132.                         Result.ErrMsg = res.RequestError.Message
  133.                     Else
  134.                         Dim postfix As String = .ParseBetween(res.Html, """hash"":""", """", """hash"":""".Length)
  135.                         Result.Success = True
  136.                         Result.iLink = "http://imgur.com/" & postfix
  137.                         Result.DirectLink = "http://imgur.com/" & postfix & FileExtension
  138.                     End If
  139.                 Catch ex As Exception
  140.                     Result.ErrMsg= ex.Message
  141.                 End Try
  142.                 Return Result
  143.  
  144.             End With
  145.  
  146.         End Function
  147.  
  148. #End Region
  149.  
  150. #Region "Destructor"
  151.  
  152.         Private disposedValue As Boolean '
  153.        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
  154.             If Not Me.disposedValue Then
  155.                 If disposing Then
  156.                     If Not IsNothing(http) Then http.Dispose()
  157.                 End If
  158.             End If
  159.             Me.disposedValue = True
  160.         End Sub
  161.  
  162.         Public Sub Dispose() Implements IDisposable.Dispose
  163.             Dispose(True)
  164.             GC.SuppressFinalize(Me)
  165.         End Sub
  166.  
  167. #End Region
  168.  
  169.     End Class
  170.  
  171. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment