public void UploadImage(string name, ImageType imageType, byte[] file)
 {
     var request = Reddit.CreatePost(UploadImageUrl);
     var formData = new MultipartFormBuilder(request);
     formData.AddDynamic(new
         {
             name,
             uh = Reddit.User.Modhash,
             r = Subreddit.Name,
             formid = "image-upload",
             img_type = imageType == ImageType.PNG ? "png" : "jpg",
             upload = ""
         });
     formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg");
     formData.Finish();
     try
     {
         var response = request.GetResponse();
         var data = Reddit.GetResponseString(response.GetResponseStream());
     }
     catch (WebException ex)
     {
         var data = Reddit.GetResponseString(ex.Response.GetResponseStream());
         throw;
     }
 }
Ejemplo n.º 2
0
        public void UploadImage(string name, ImageType imageType, byte[] file)
        {
            var request  = WebAgent.CreatePost(UploadImageUrl);
            var formData = new MultipartFormBuilder(request);

            formData.AddDynamic(new
            {
                name,
                uh       = Reddit.User.Modhash,
                r        = Subreddit.Name,
                formid   = "image-upload",
                img_type = imageType == ImageType.PNG ? "png" : "jpg",
                upload   = ""
            });
            formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg");
            formData.Finish();
            WebAgent.ExecuteRequest(request);
        }
Ejemplo n.º 3
0
        public void UploadImage(string name, ImageType imageType, byte[] file)
        {
            var request  = Reddit.CreatePost(UploadImageUrl);
            var formData = new MultipartFormBuilder(request);

            formData.AddDynamic(new
            {
                name,
                uh       = Reddit.User.Modhash,
                r        = Subreddit.Name,
                formid   = "image-upload",
                img_type = imageType == ImageType.PNG ? "png" : "jpg",
                upload   = ""
            });
            formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg");
            formData.Finish();
            var response = request.GetResponse();
            var data     = Reddit.GetResponseString(response.GetResponseStream());
            // TODO: Detect errors
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Upload an image to reddit.
        /// </summary>
        /// <param name="name">name of image.</param>
        /// <param name="imageType"><see cref="ImageType"/> of image</param>
        /// <param name="file">image buffer</param>
        public async Task UploadImageAsync(string name, ImageType imageType, byte[] file)
        {
            var request  = WebAgent.CreateRequest(UploadImageUrl, "POST");
            var formData = new MultipartFormBuilder(request);

            formData.AddDynamic(new
            {
                name,
                r        = Subreddit.Name,
                formid   = "image-upload",
                img_type = imageType == ImageType.PNG ? "png" : "jpg",
                upload   = ""
            });
            formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg");
            formData.Finish();
            var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);

            var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            // TODO: Detect errors
        }
Ejemplo n.º 5
0
 public static void DoUpdate()
 {
     Console.WriteLine("Running update...");
     try
     {
         var messages = Reddit.User.GetUnreadMessages();
         foreach (var message in messages)
         {
             var comment = message as Comment;
             if (comment == null)
             {
                 if (message is PrivateMessage)
                     (message as PrivateMessage).SetAsRead();
                 continue;
             }
             comment.SetAsRead();
             if (!comment.Body.Contains("/u/MediaCrusher"))
                 continue;
             Console.WriteLine("Handling {0}", comment.FullName);
             var post = Reddit.GetThingByFullname("t3_" + comment.LinkId) as Post;
             if (post.Domain == "mediacru.sh")
                 comment.Reply("This post is already on mediacru.sh, silly!");
             else
             {
                 var uri = new Uri(post.Url);
                 if (uri.Host == "imgur.com" && !uri.LocalPath.StartsWith("/a/"))
                 {
                     // Swap out for i.imgur.com
                     uri = new Uri("http://i.imgur.com/" + uri.LocalPath + ".png"); // Note: extension doesn't need to be correct
                 }
                 var task = Task.Factory.StartNew(() =>
                 {
                     var request = (HttpWebRequest)WebRequest.Create(uri);
                     request.Method = "HEAD";
                     var response = request.GetResponse() as HttpWebResponse;
                     if (!SupportedContentTypes.Any(c => c == response.ContentType))
                     {
                         comment.Reply("This post isn't a supported media type. :(");
                         response.Close();
                         return;
                     }
                     else if (response.StatusCode != HttpStatusCode.OK)
                     {
                         comment.Reply("There was an error fetching this file. :(");
                         response.Close();
                         return;
                     }
                     else
                     {
                         response.Close();
                         // Let's do this
                         var client = new WebClient();
                         var file = client.DownloadData(uri);
                         request = (HttpWebRequest)WebRequest.Create("https://mediacru.sh/upload/");
                         request.Method = "POST";
                         var builder = new MultipartFormBuilder(request);
                         var ext = response.ContentType.Split('/')[1];
                         builder.AddFile("file", "foobar." + ext, file, response.ContentType);
                         builder.Finish();
                         try
                         {
                             response = request.GetResponse() as HttpWebResponse;
                         }
                         catch (WebException e)
                         {
                             try
                             {
                                 response = e.Response as HttpWebResponse;
                                 if (response.StatusCode != HttpStatusCode.Conflict)
                                 {
                                     comment.Reply("MediaCrush didn't like this for some reason. Sorry :(");
                                     return;
                                 }
                             }
                             catch
                             {
                                 comment.Reply("MediaCrush didn't like this for some reason. Sorry :(");
                                 return;
                             }
                         }
                         string hash;
                         using (var stream = new StreamReader(response.GetResponseStream()))
                             hash = stream.ReadToEnd();
                         while (true)
                         {
                             request = (HttpWebRequest)WebRequest.Create("https://mediacru.sh/upload/status/" + hash);
                             request.Method = "GET";
                             response = request.GetResponse() as HttpWebResponse;
                             string text;
                             using (var stream = new StreamReader(response.GetResponseStream()))
                                 text = stream.ReadToEnd();
                             response.Close();
                             try
                             {
                                 if (text == "done")
                                 {
                                     request = (HttpWebRequest)WebRequest.Create("https://mediacru.sh/" + hash + ".json");
                                     request.Method = "GET";
                                     response = request.GetResponse() as HttpWebResponse;
                                     var json = JObject.Parse(new StreamReader(response.GetResponseStream()).ReadToEnd());
                                     response.Close();
                                     var compliment = GetCompliment();
                                     var compression = (int)(json["compression"].Value<double>() * 100);
                                     if (compression >= 100)
                                     {
                                         comment.Reply(string.Format("Done! It loads **{0}% faster** now. https://mediacru.sh/{1}\n\n*{2}* " +
                                             "^^[faq](http://www.reddit.com/r/MediaCrush/wiki/mediacrusher) " +
                                             "^^- ^^[upload](https://mediacru.sh)", compression, hash, compliment));
                                     }
                                     else
                                     {
                                         comment.Reply(string.Format("Done! https://mediacru.sh/{0}\n\n*{1}* " +
                                             "^^[faq](http://www.reddit.com/r/MediaCrush/wiki/mediacrusher) " +
                                             "^^- ^^[upload](https://mediacru.sh)", hash, compliment));
                                     }
                                     Console.WriteLine("https://mediacru.sh/" + hash);
                                     return;
                                 }
                                 else if (text == "timeout")
                                 {
                                     comment.Reply("This took too long for us to process :(");
                                     return;
                                 }
                                 else if (text == "error")
                                 {
                                     comment.Reply("Something went wrong :(");
                                     return;
                                 }
                             }
                             catch (RateLimitException e)
                             {
                                 Console.WriteLine("Rate limited - waiting {0} minutes", e.TimeToReset.TotalMinutes);
                                 Thread.Sleep(e.TimeToReset);
                             }
                         }
                     }
                 });
             }
         }
         Console.WriteLine("Done.");
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception occured during update:");
         Console.WriteLine(e.ToString());
     }
     Timer.Change(30000, Timeout.Infinite);
 }
Ejemplo n.º 6
0
 public void UploadHeaderImage(string name, ImageType imageType, byte[] file)
 {
     var request = WebAgent.CreatePost(UploadImageUrl);
     var formData = new MultipartFormBuilder(request);
     formData.AddDynamic(new
     {
         name,
         uh = Reddit.User.Modhash,
         r = Name,
         formid = "image-upload",
         img_type = imageType == ImageType.PNG ? "png" : "jpg",
         upload = "",
         header = 1
     });
     formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg");
     formData.Finish();
     var response = request.GetResponse();
     var data = WebAgent.GetResponseString(response.GetResponseStream());
     // TODO: Detect errors
 }
Ejemplo n.º 7
0
 public void UploadImage(string name, ImageType imageType, byte[] file)
 {
     var request = WebAgent.CreatePost(UploadImageUrl);
     var formData = new MultipartFormBuilder(request);
     formData.AddDynamic(new
         {
             name,
             uh = Reddit.User.Modhash,
             r = Subreddit.Name,
             formid = "image-upload",
             img_type = imageType == ImageType.PNG ? "png" : "jpg",
             upload = ""
         });
     formData.AddFile("file", "foo.png", file, imageType == ImageType.PNG ? "image/png" : "image/jpeg");
     formData.Finish();
     WebAgent.ExecuteRequest(request);
 }