Browse Source

init

master
Robin Thoni 8 years ago
commit
3b48f011f0
4 changed files with 135 additions and 0 deletions
  1. 61
    0
      .gitignore
  2. 11
    0
      fake-tracker.py
  3. 20
    0
      torrententry.py
  4. 43
    0
      torrentfile.py

+ 61
- 0
.gitignore View File

@@ -0,0 +1,61 @@
1
+# Byte-compiled / optimized / DLL files
2
+__pycache__/
3
+*.py[cod]
4
+*$py.class
5
+
6
+# C extensions
7
+*.so
8
+
9
+# Distribution / packaging
10
+.Python
11
+env/
12
+build/
13
+develop-eggs/
14
+dist/
15
+downloads/
16
+eggs/
17
+.eggs/
18
+lib/
19
+lib64/
20
+parts/
21
+sdist/
22
+var/
23
+*.egg-info/
24
+.installed.cfg
25
+*.egg
26
+
27
+# PyInstaller
28
+#  Usually these files are written by a python script from a template
29
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
30
+*.manifest
31
+*.spec
32
+
33
+# Installer logs
34
+pip-log.txt
35
+pip-delete-this-directory.txt
36
+
37
+# Unit test / coverage reports
38
+htmlcov/
39
+.tox/
40
+.coverage
41
+.coverage.*
42
+.cache
43
+nosetests.xml
44
+coverage.xml
45
+*,cover
46
+
47
+# Translations
48
+*.mo
49
+*.pot
50
+
51
+# Django stuff:
52
+*.log
53
+
54
+# Sphinx documentation
55
+docs/_build/
56
+
57
+# PyBuilder
58
+target/
59
+
60
+*.swp
61
+*.torrent

+ 11
- 0
fake-tracker.py View File

@@ -0,0 +1,11 @@
1
+#! /usr/bin/env python
2
+
3
+import torrentfile
4
+import torrententry
5
+
6
+for file in ('torrent.torrent', 'torrent2.torrent'):
7
+    torrent = torrentfile.TorrentFile()
8
+    torrent.readFile(file)
9
+    print torrent.getSize()
10
+    print torrent.getTracker()
11
+    print torrent.getHash()

+ 20
- 0
torrententry.py View File

@@ -0,0 +1,20 @@
1
+#! /usr/bin/env python
2
+
3
+class TorrentEntry:
4
+    name = ""
5
+    size = 0
6
+    def __init__(self, name, size):
7
+        self.name = str(name)
8
+        self.size = int(size)
9
+
10
+    def __str__(self):
11
+        return self.name + " " + str(self.size)
12
+
13
+    def __repr__(self):
14
+        return self.__str__()
15
+
16
+    def getName(self):
17
+        return self.name
18
+
19
+    def getSize(self):
20
+        return self.size

+ 43
- 0
torrentfile.py View File

@@ -0,0 +1,43 @@
1
+#! /usr/bin/env python
2
+
3
+import os
4
+import hashlib
5
+import bencode
6
+import torrententry
7
+
8
+class TorrentFile:
9
+    data = ()
10
+    info = ()
11
+    def readFile(self, path):
12
+        with open(path, 'rb') as f:
13
+            bencoded = f.read()
14
+        self.readBencoded(bencoded)
15
+
16
+    def readBencoded(self, data):
17
+        self.data = bencode.bdecode(data)
18
+        self.info = self.data['info']
19
+
20
+    def getName(self):
21
+        return self.data['name']
22
+
23
+    def getFiles(self):
24
+        if 'files' in self.info:
25
+            files = []
26
+            for file in self.info['files']:
27
+                files.append(torrententry.TorrentEntry(os.sep.join(file['path']), file['length']))
28
+            return files
29
+        else:
30
+            return [torrententry.TorrentEntry(self.info['name'], self.info['length'])]
31
+
32
+    def getSize(self):
33
+        size = 0
34
+        for file in self.getFiles():
35
+            size += file.getSize()
36
+        return size
37
+
38
+    def getTracker(self):
39
+        return self.data['announce']
40
+
41
+    def getHash(self):
42
+        bencoded = bencode.bencode(self.info)
43
+        return hashlib.sha1(bencoded).hexdigest()

Loading…
Cancel
Save