4

Hello I'm making a Console app in VS15 using C#.

How can I decode torrent files? To get the Name, Size and Date of the torrent file? I want to donwload a torrent file from a server and then decode it to see the name, size and date. So far i can download a file using WebCLient, but i have search and search for how to decode a torrent file, but without luck.

I have tried this library and did this:

using (var fs = File.OpenRead("Ubuntu.torrent"))
{
    BDictionary bdictionary = Bencode.DecodeDictionary(fs);
}

But i don't quite understand what bdictionary gives me? I want to output the torrents information in the console.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70
  • 2
    At the very same page you linked you have plenty of samples of how to use this. Just *look* at the data you get, and it should be obvious how to do what you're trying to do. – Luaan Aug 18 '15 at 08:36
  • The problem is I don't know how to look at the data i get. the bdictionary object contains some list but that doens't contain the right information? @Luaan – Loc Dai Le Aug 18 '15 at 08:47
  • 1
    If it doesn't contain the right information, the information isn't there. Going by the protocol specification, `Metainfo files (also known as .torrent files) are bencoded dictionaries with the following keys: announce (The URL of the tracker) info (This maps to a dictionary, with keys described below)`. So you need to further decode the `info` part of the top-level dictionary. However, I still don't understand what reading a torrent file has to do with "name, size and date" of the torrent file - that's what `FileInfo` is for. If you mean the files *described in* the torrent, there's no date. – Luaan Aug 18 '15 at 08:52
  • @Luaan. I want to get the torrent information. Like how big the size of all the files in the current torrent are. Like this site can give me: http://i-tools.org/torrent/exec – Loc Dai Le Aug 18 '15 at 09:00
  • That site tells you exactly how to get the data - so what's your problem? – Luaan Aug 18 '15 at 09:04
  • @Luaan- Sorry if i'm not being clear. I'm making a .NET application and from my application I can get the data/information from a .torrent file I download down. – Loc Dai Le Aug 18 '15 at 09:08
  • Did you follow the instructions for decoding the file? They are quite plain. They tell you exactly what can be in your `bdictionary`, and what to do to get the rest of the information. If you're getting something *not* described by the specification, ask about that *specifically*. – Luaan Aug 18 '15 at 09:11
  • which instructions @Luaan ? – Loc Dai Le Aug 18 '15 at 09:13
  • The instructions on the site you've linked. Decode the top-level dictionary. You'll get `info`. `info` can be either a dictionary of `name, length, md5sum`, or a dictionary of `name, files`, where you have to further decode the `files` (and of course, each of the individual files). – Luaan Aug 18 '15 at 09:20
  • I understand what you mean @Luaan, but i don't see where they tell how to do it. – Loc Dai Le Aug 18 '15 at 09:26
  • 2
    The specification *is* the "how". What else do you need? You have a library that can decode the data, and you have a specification of what the data structure is. You're not going to find a step-by-step guide for every problem you have as a programmer, you know (and this is fairly close to being a step-by-step guide). – Luaan Aug 18 '15 at 09:29

1 Answers1

3

I have recently added functionality to work specifically with torrent files. So far it is very basic and just have properties for easy access to some of the info.

You should be able to extract the name and size of files like this:

TorrentFile torrent = Bencode.DecodeTorrentFile("Ubuntu.torrent");

// Calculate info hash (e.g. "B415C913643E5FF49FE37D304BBB5E6E11AD5101")
string infoHash = torrent.CalculateInfoHash();

// Get name and size of each file in 'files' list of 'info' dictionary ("multi-file mode")
BList files = (BList)torrent.Info["files"];
foreach (BDictionary file in files)
{
    // File size in bytes (BNumber has implicit conversion to int and long)
    int size = (BNumber) file["length"];

    // List of all parts of the file path. 'dir1/dir2/file.ext' => dir1, dir2 and file.ext
    BList path = (BList) file["path"];

    // Last element is the file name
    BString fileName = (BString) path.Last();

    // Converts fileName (BString = bytes) to a string
    string fileNameString = fileName.ToString(Encoding.UTF8);
}

For more information on the data stored in a .torrent have a look at the BitTorrentSpecification.

Søren Kruse
  • 1,160
  • 10
  • 12