-
Notifications
You must be signed in to change notification settings - Fork 5
/
torrent.m
162 lines (135 loc) · 6.03 KB
/
torrent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#import "torrent.h"
#import "BEncoding.h"
NSString *stringFromFileSize(NSInteger theSize)
{
/*
From http://snippets.dzone.com/posts/show/3038
*/
float floatSize = theSize;
if (theSize<1023)
return([NSString stringWithFormat:@"%i bytes",theSize]);
floatSize = floatSize / 1024;
if (floatSize<1023)
return([NSString stringWithFormat:@"%1.1f KB",floatSize]);
floatSize = floatSize / 1024;
if (floatSize<1023)
return([NSString stringWithFormat:@"%1.1f MB",floatSize]);
floatSize = floatSize / 1024;
return([NSString stringWithFormat:@"%1.1f GB",floatSize]);
}
NSString *stringFromData(NSData *torrent, NSString *key)
{
NSData *rawkey = [torrent valueForKey:key];
NSString *strdata = [NSString stringWithUTF8String:[rawkey bytes]];
return strdata;
}
void replacer(NSMutableString *html, NSString *replaceThis, NSString *withThis, NSString *defaultString) {
if(withThis == nil) withThis = defaultString;
[html replaceOccurrencesOfString:replaceThis
withString:withThis
options:NSLiteralSearch
range:NSMakeRange(0, [html length])];
}
NSDictionary *getTorrentInfo(NSURL *url)
{
// Read raw file, and de-bencode
NSData *rawdata = [NSData dataWithContentsOfURL:url];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
NSData *infoData = [torrent valueForKey:@"info"];
// Retrive interesting data
NSString *announce = stringFromData(torrent, @"announce");
NSString *torrentName = stringFromData(infoData, @"name");
NSString *length = [infoData valueForKey:@"length"];
NSNumber *isPrivate;
if([[infoData valueForKey:@"private"] isNotEqualTo:NULL]){
isPrivate = [NSNumber numberWithBool:YES];
}else{
isPrivate = [NSNumber numberWithBool:NO];
}
// Get filenames/sizes
NSArray *filesData = [infoData valueForKey:@"files"];
NSInteger totalSize = 0;
NSMutableArray *allFiles = [NSMutableArray array];
for (int i = 0; i < [filesData count]; i++) {
NSData *currentFileData = [filesData objectAtIndex:i];
NSString *currentSize = [currentFileData valueForKey:@"length"];
NSMutableDictionary *currentFile = [NSMutableDictionary dictionaryWithObject:currentSize forKey:@"length"];
totalSize = totalSize + [currentSize integerValue];
NSMutableString *currentFilePath = [NSMutableString string];
// Looping over path segments"
for(int path_i = 0; path_i < [[currentFileData valueForKey:@"path"] count] ; path_i++) {
NSData *currentSegmentData = [[currentFileData valueForKey:@"path"] objectAtIndex:path_i];
NSString *currentPathSegment = [NSString stringWithUTF8String:[currentSegmentData bytes]];
[currentFilePath appendFormat:@"/%@", currentPathSegment];
}
[currentFile setObject:currentFilePath forKey:@"filename"];
[allFiles addObject:currentFile];
}
// Store interesting data in dictionary, and return it
NSMutableDictionary *ret = [NSMutableDictionary dictionary];
if(length != NULL) [ret setObject:length forKey:@"length"];
if(announce != NULL) [ret setObject:announce forKey:@"announce"];
if(torrentName != NULL) [ret setObject:torrentName forKey:@"torrentName"];
if(isPrivate != NULL) [ret setObject:isPrivate forKey:@"isPrivate"];
if(allFiles != NULL) [ret setObject:allFiles forKey:@"files"];
[ret setObject:[NSNumber numberWithInteger:totalSize] forKey:@"totalSize"];
return ret;
}
NSData *getTorrentPreview(NSURL *url)
{
// Load template HTML
NSString *templateFile = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier: @"uk.co.dbrweb.qltorrent"]
pathForResource:@"torrentpreview" ofType:@"html"]];
NSDictionary *torrentInfo = getTorrentInfo(url);
NSMutableString *html = [NSMutableString stringWithString:templateFile];
// Replace torrentName
replacer(html,
@"{TORRENT_NAME}",
[torrentInfo objectForKey:@"torrentName"],
@"[Unknown]");
// Replace torrent size with length or totalSize
NSNumber *size;
if([torrentInfo objectForKey:@"length"] != NULL){
// For single-file torrents
size = [torrentInfo objectForKey:@"length"];
}else{
// Multi-file torrents don't have length, so use the total file size
size = [torrentInfo objectForKey:@"totalSize"];
}
NSString *torrentInfoString = [NSString stringWithFormat:@"<ul><li>Size: %@</li></ul>",
stringFromFileSize([size integerValue])];
replacer(html,
@"{TORRENT_INFO}",
torrentInfoString,
@"[Unknown]");
// Replace files
NSMutableArray *files = [torrentInfo objectForKey:@"files"];
if(files != NULL)
{
NSMutableString *torrentFileString = [NSMutableString string];
for(int i = 0; i < [files count]; i++)
{
NSMutableDictionary *currentFile = [files objectAtIndex:i];
NSString *currentName = [currentFile objectForKey:@"filename"];
NSString *currentSizeData = [currentFile objectForKey:@"length"];
NSString *currentSize;
if(currentSizeData == NULL){
currentSize = [NSString stringWithString:@"N/a"];
}
else
{
currentSize = [NSString stringWithString:stringFromFileSize([currentSizeData integerValue])];
}
[torrentFileString appendString:[NSString stringWithFormat: @"<tr><td>%@</td><td>%@</td></tr>\n",
currentName,
currentSize]
];
}
replacer(html,
@"{TORRENT_FILES}",
torrentFileString,
@"<tr><td>[Cannot list files]</td><td>N/a</td></tr>"
);
}
return [html dataUsingEncoding:NSUTF8StringEncoding];
}