1: param(
2: [System.IO.FileInfo]$file = $null,
3: [string]$twitteruser = $null,
4: [string]$twitterpass = $null,
5: [string]$message = $null
6: );
7:
8: $CODEPAGE = "iso-8859-1";
9:
10: $TWITPIC_API_UPLOAD = "http://twitpic.com/api/upload";
11: $TWITPIC_API_UPLOADANDPOST = "http://twitpic.com/api/uploadAndPost";
12:
13: #--------------------------------------------------------------------------------------------
14: # function Get-EncodedDataFromFile
15: #--------------------------------------------------------------------------------------------
16: function Get-EncodedDataFromFile()
17: {
18: param(
19: [System.IO.FileInfo]$file = $null,
20: [string]$codePageName = $CODEPAGE
21: );
22:
23: $data = $null;
24:
25: if ( $file -and [System.IO.File]::Exists($file.FullName) )
26: {
27: $bytes = [System.IO.File]::ReadAllBytes($file.FullName);
28: if ( $bytes )
29: {
30: $enc = [System.Text.Encoding]::GetEncoding($codePageName);
31: $data = $enc.GetString($bytes);
32: }
33: }
34: else
35: {
36: Write-Host "ERROR; File '$file' does not exist";
37: }
38: $data;
39: }
40:
41: #--------------------------------------------------------------------------------------------
42: # function Get-ImageContentType
43: #--------------------------------------------------------------------------------------------
44: function Get-ImageContentType()
45: {
46: param([System.IO.FileInfo]$file = $null);
47: $contentType = $null;
48: $contentTypeMap = @{
49: ".jpg" = "image/jpeg";
50: ".jpeg" = "image/jpeg";
51: ".gif" = "image/gif";
52: ".png" = "image/png";
53: ".tiff" = "image/tiff";
54: }
55:
56: if ( $file )
57: {
58: $contentType = $contentTypeMap[$file.Extension.ToLower()];
59: }
60: $contentType;
61: }
62:
63: #----------------------------------------------------------------------------
64: # function Execute-HTTPPostCommand
65: #----------------------------------------------------------------------------
66: function Execute-HTTPPostCommand()
67: {
68: param(
69: [string] $url = $null,
70: [string] $data = $null,
71: [System.Net.NetworkCredential]$credentials = $null,
72: [string] $contentType = "application/x-www-form-urlencoded",
73: [string] $codePageName = "UTF-8",
74: [string] $userAgent = $null
75: );
76:
77: if ( $url -and $data )
78: {
79: [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url);
80: $webRequest.ServicePoint.Expect100Continue = $false;
81: if ( $credentials )
82: {
83: $webRequest.Credentials = $credentials;
84: $webRequest.PreAuthenticate = $true;
85: }
86: $webRequest.ContentType = $contentType;
87: $webRequest.Method = "POST";
88: if ( $userAgent )
89: {
90: $webRequest.UserAgent = $userAgent;
91: }
92:
93: $enc = [System.Text.Encoding]::GetEncoding($codePageName);
94: [byte[]]$bytes = $enc.GetBytes($data);
95: $webRequest.ContentLength = $bytes.Length;
96: [System.IO.Stream]$reqStream = $webRequest.GetRequestStream();
97: $reqStream.Write($bytes, 0, $bytes.Length);
98: $reqStream.Flush();
99:
100: $resp = $webRequest.GetResponse();
101: $rs = $resp.GetResponseStream();
102: [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
103: $sr.ReadToEnd();
104: }
105: }
106:
107: #--------------------------------------------------------------------------------------------
108: # function Post-TwitPic
109: #--------------------------------------------------------------------------------------------
110: function Post-TwitPic()
111: {
112: param
113: (
114: [System.IO.FileInfo]$file = $null,
115: [string]$twitteruser = $null,
116: [string]$twitterpass = $null,
117: [string]$message = $null
118: );
119:
120: if ( $file -and $twitteruser -and $twitterpass )
121: {
122: $boundary = [System.Guid]::NewGuid().ToString();
123: $header = "--{0}" -f $boundary;
124: $footer = "--{0}--" -f $boundary;
125:
126: [System.Text.StringBuilder]$contents = New-Object System.Text.StringBuilder;
127: [void]$contents.AppendLine($header);
128:
129: $filedata = Get-EncodedDataFromFile -file $file -codePageName $CODEPAGE;
130: if ( $filedata )
131: {
132: $fileContentType = Get-ImageContentType -file $file;
133: $fileHeader = "Content-Disposition: file; name=""{0}""; filename=""{1}""" -f "media", $file.Name;
134:
135: [void]$contents.AppendLine($fileHeader);
136: [void]$contents.AppendLine("Content-Type: {0}" -f $fileContentType);
137: [void]$contents.AppendLine();
138: [void]$contents.AppendLine($fileData);
139:
140: [void]$contents.AppendLine($header);
141: [void]$contents.AppendLine("Content-Disposition: form-data; name=""username""");
142: [void]$contents.AppendLine();
143: [void]$contents.AppendLine($twitteruser);
144:
145: [void]$contents.AppendLine($header);
146: [void]$contents.AppendLine("Content-Disposition: form-data; name=""password""");
147: [void]$contents.AppendLine();
148: [void]$contents.AppendLine($twitterpass);
149:
150: $url = $TWITPIC_API_UPLOAD;
151: if ( $message )
152: {
153: [void]$contents.AppendLine($header);
154: [void]$contents.AppendLine("Content-Disposition: form-data; name=""message""");
155: [void]$contents.AppendLine();
156: [void]$contents.AppendLine($message);
157:
158: $url = $TWITPIC_API_UPLOADANDPOST;
159: }
160:
161: [void]$contents.AppendLine($footer);
162:
163: $contents.ToString() > ".\out.txt";
164:
165: $postContentType = "multipart/form-data; boundary={0}" -f $boundary;
166:
167: [xml]$resp = Execute-HTTPPostcommand -url $url -data $contents.ToString() `
168: -contentType $postContentType -codePageName $CODEPAGE -userAgent "PoshTwitPic";
169:
170: if ( $resp )
171: {
172: switch ($resp.rsp.stat)
173: {
174: "ok" {
175: $obj = 1 | select mediaid, mediaurl;
176: $obj.mediaid = $resp.rsp.mediaid;
177: $obj.mediaurl = $resp.rsp.mediaurl;
178: $obj;
179: }
180: "fail" {
181: $errcode = $resp.rsp.err.code;
182: $errmsg = $resp.rsp.err.msg;
183: Write-Host "Post Error: Code = '$errcode'; Message = '$errmsg'";
184: }
185: }
186: }
187: }
188: }
189: else
190: {
191: Write-Host "USAGE: Post-TwitPic.ps1 -file file -twitteruser user -twitterpass pass";
192: }
193: }
194:
195: if ( $file -and $twitteruser -and $twitterpass )
196: {
197: Post-TwitPic -file $file -twitteruser $twitteruser -twitterpass $twitterpass -message $message;
198: }
199: else
200: {
201: Write-Host "USAGE: Post-TwitPic.ps1 -file file -twitteruser user -twitterpass pass";
202: }