PyNesca/modules/address_generation/GDocsAddressGenerator.py

44 lines
1.8 KiB
Python

from core.prototypes.AbstractAddressGenerator import AbstractAddressGenerator
class GDocsAddressGenerator(AbstractAddressGenerator):
def set_parsed_fields(self, prefix:"gdocs_prefix",
ranges:"gdocs_hash_ranges") -> None:
self.alphabet = list(
'-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
)
self.revsymbols = {symb:i for i, symb in enumerate(self.alphabet)}
self.prefix = prefix
self.ranges = ranges
self.hashlen = len(ranges[0][0])
self.currange = self.ranges.pop(0)
def hash2int(self, gdhash):
alen = len(self.alphabet)
res = 0
for symb in gdhash:
res *= alen
res += self.revsymbols[symb]
return res
def int2hash(self, hint):
alen = len(self.alphabet)
reshash = [self.alphabet[0]]*self.hashlen
for i in range(-1, -self.hashlen-1, -1):
hint, rest = divmod(hint, alen)
reshash[i] = self.alphabet[rest]
return "".join(reshash)
def get_next_address(self, prev_hash:'gdoc_hash') -> {"gdoc_prefix", "gdoc_hash"}:
if not prev_hash:
return {'gdoc_prefix':self.prefix, "gdoc_hash":self.currange[0]}
#prev_hash = prev_url[prev_url.rfind('/') + 1:]
if self.hash2int(self.currange[1]) <= self.hash2int(prev_hash):
if not self.ranges: return None
self.currange = self.ranges.pop(0)
return {'gdoc_prefix' : self.prefix, 'gdoc_hash':self.currange[0]}
return {'gdoc_prefix' : self.prefix, 'gdoc_hash':self.int2hash(self.hash2int(prev_hash) +
1)}
def get_all_addresses(self) -> {'gdocs_prefix', 'gdocs_hash_ranges'}:
return {'gdocs_prefix':self.prefix, 'gdocs_hash_ranges': self.ranges}