diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2023-02-21 15:18:10 +0100 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2023-02-21 15:18:10 +0100 |
commit | bc053a9b414722990e9d62cce9f32963cfe43096 (patch) | |
tree | fb9c76e79c518ab47bb5b399ab64c53fdcf0e8e9 /analiza | |
parent | pie -> barh (diff) | |
download | travnik-bc053a9b414722990e9d62cce9f32963cfe43096.tar travnik-bc053a9b414722990e9d62cce9f32963cfe43096.tar.gz travnik-bc053a9b414722990e9d62cce9f32963cfe43096.tar.bz2 travnik-bc053a9b414722990e9d62cce9f32963cfe43096.tar.lz travnik-bc053a9b414722990e9d62cce9f32963cfe43096.tar.xz travnik-bc053a9b414722990e9d62cce9f32963cfe43096.tar.zst travnik-bc053a9b414722990e9d62cce9f32963cfe43096.zip |
Diffstat (limited to 'analiza')
-rw-r--r-- | analiza/zvezek.ipynb | 94 |
1 files changed, 4 insertions, 90 deletions
diff --git a/analiza/zvezek.ipynb b/analiza/zvezek.ipynb index 1b832e2..b88677b 100644 --- a/analiza/zvezek.ipynb +++ b/analiza/zvezek.ipynb @@ -15,101 +15,15 @@ "......\n", "fixed bad single file torrent 4f269d8aefd647ee270842d53ec98aebd23a4afe\n", "fixed bad single file torrent 7b09ae0b612dafc1744562dccbbe4becf4d633c3\n", - "37769 @ 434.7589800900314 s\n" + "38021 @ 413.0262871221639 s\n" ] } ], "source": [ - "from bencodepy import decode\n", - "from enum import Enum\n", - "from hashlib import sha1, sha256\n", - "from os import scandir\n", "from time import monotonic\n", - "class Type(Enum):\n", - " UNDEF = 0,\n", - " V1 = 1,\n", - " V2 = 2,\n", - " HYBRID = 3\n", - "class Torrent():\n", - " def __init__(self):\n", - " self.sha1 = b''\n", - " self.files = {}\n", - " self.type = Type.UNDEF\n", - " def file(self, f):\n", - " self.parse(open(f, \"rb\").read())\n", - " def parse(self, b):\n", - " infodict = b[b.find(b'4:info')+6:b.rfind(b'6:sourced2:ip')]\n", - " self.sha1 = sha1(infodict).digest()\n", - " self.sha256 = sha256(infodict).digest()\n", - " self.dict = decode(b)\n", - " if b'pieces' in self.dict.get(b'info'):\n", - " self.dict.get(b'info').pop(b'pieces')\n", - " if b'files' in self.dict.get(b'info').keys():\n", - " self.type = Type.V1\n", - " for file in self.dict.get(b'info').get(b'files'):\n", - " if file.get(b'attr') is not None and b'p' in file.get(b'attr') or b'padding.file' in b'/'.join(file.get(b'path')) or b'.pad' in file.get(b'path'):\n", - " continue\n", - " def insert_file(d, path, length, self):\n", - " name = path.pop()\n", - " if not len(path):\n", - " d[name] = length\n", - " return\n", - " if name not in d.keys():\n", - " d[name] = {}\n", - " insert_file(d[name], path, length, self)\n", - " file.get(b'path').reverse()\n", - " insert_file(self.files, file.get(b'path'), file.get(b'length'), self)\n", - " self.dict.get(b'info').pop(b'files')\n", - " if b'file tree' in self.dict.get(b'info').keys(): # some torrents have broken file trees so we use files first\n", - " if self.type is Type.V1:\n", - " self.type = Type.HYBRID\n", - " else:\n", - " def filetree(names):\n", - " r = {}\n", - " for key in names.keys():\n", - " if key == b'':\n", - " return names.get(key).get(b'length')\n", - " r[key] = filetree(names.get(key))\n", - " return r\n", - " self.files = filetree(self.dict.get(b'info').get(b'file tree'))\n", - " self.dict.get(b'info').pop(b'file tree')\n", - " if not len(self.files):\n", - " self.type = Type.V1\n", - " self.files[self.dict.get(b'info').get(b'name')] = self.dict.get(b'info').get(b'length')\n", - " first_filename = [i for i in self.files.keys()][0]\n", - " if len(self.files) == 1 and self.files[first_filename] == {}:\n", - " print(\"fixed bad single file torrent\", self.sha1.hex())\n", - " self.files[first_filename] = self.dict.get(b'info').get(b'length')\n", - " def paths(self):\n", - " def paths_r(d, path=None):\n", - " if path is None:\n", - " path = []\n", - " for f in d.keys():\n", - " if type(d[f]) is int:\n", - " z = path.copy()\n", - " z.append(f)\n", - " yield z, d[f]\n", - " else:\n", - " z = path.copy()\n", - " z.append(f)\n", - " for z, v in paths_r(d[f], z):\n", - " yield z, v\n", - " for z, v in paths_r(self.files):\n", - " yield z, v\n", - " def __repr__(self):\n", - " return str(self.__dict__)\n", - " def __hash__(self):\n", - " if len(self.sha1):\n", - " return int.from_bytes(self.sha1, byteorder=\"big\")\n", - " return id(self)\n", - "def glob(d):\n", - " r = {}\n", - " for f in scandir(d):\n", - " if f.name.endswith(\".torrent\") and f.is_file():\n", - " t = Torrent()\n", - " t.file(f.path)\n", - " r[t.sha1] = t\n", - " return r\n", + "from sys import path\n", + "path.append(\"/root/projects/travnik\")\n", + "from travnik import glob\n", "print(\"......\")\n", "start = monotonic()\n", "torrents = glob(\"/root/projects/travnik\")\n", |