Posts tagged code

Torrent Automation

One of the main reasons for buying a server was for the ability to leave a torrent client running 24/7, now I can. With uTorrent ( and all of its advanced options ) I can now download torrents all of the time, one of the drawbacks to this is the drain of bandwidth on the rest of the network. So I’ve set uTorrent to only download at full speed during the night, and then very limited during the daytime, this was I shouldn’t notice any impact on my network.

uTorrent Schedule

Another nice feature of uTorrent is the auto download of torrents when placed in a specific directory, so this means I can quickly add a torrent file to my NAS drive and within a few seconds the torrent is downloading. This and with addition of the RSS feed feature allows me to quickly download TV and films without much effort.

File are then automatically moved to the correct directory, based on the torrents labels, so that my Boxee Box will pick up the media without me intervening.

Another idea I had (well @Bonxy suggested) was to get twitter updates when downloads have finished. I had already dealt with twitter messages when creating my home automation system for my dissertation. So it was just a quick job of copy a bit of code and creating a simple console app that took the tweet params.

Twitter Code

I then edited my uTorrent options to ‘run a program’ when a download completes, I just simply pass the twitter message with the torrent name, and within a few seconds I get a tweet (plus email and free SMS) with my download details. I then added the same call to the status changed option in uTorrent.

tweet Image

Obviously I cancelled the above download as downloading content that isn’t freely available isn’t legal. :)

Resize An Image

For the imgUp tool I had to resize an image to various different sizes, after searching on the web for a while I came accross this nice little function.

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            // Save resized picture
            NewImage.Save(NewFile);
        }

The method take several parameters, first the file location of the original file, then the location for the altered image to be saved. Then the width and height of the new image, finally you can set whether the image will be resized only if the width is larger than the resized size, this prevents the image from being stretched.

ResizeImage("c:\img1.jpg", "c:\img1-altered.jpg" , 640, 480, true);

I found this snippet here and here.