Example #1
0
    def __init__(self, persist_cookies='cookies.json'):
        'Load the headers and cookies from external files.'
        with open(module_dir('tickets.json')) as f:
            params = json.load(f)
        super().__init__(params, params.pop('headers'))

        self.persist_cookies = persist_cookies
        if persist_cookies:
            if os.path.exists(persist_cookies):
                with open(persist_cookies) as f:
                    cj = requests.utils.cookiejar_from_dict(json.load(f))
                self.session.cookies = cj

        self.init_query_path()
Example #2
0
def main(src, dest):
    'Append train models to the existing JSON file.'
    with open(src) as f:
        lst = group(f)
    print('\n'.join(sorted(lst.keys())))
    print(len(lst), 'models found.')

    if os.path.isfile(dest):
        with open(dest) as f:
            existing = json.load(f)
            pprint(existing)
            lst.update(existing)

    with open(dest, 'w') as f:
        json.dump(lst, f)
Example #3
0
    def __init__(self, params='tickets.json', persist='cookies.json'):
        'Initialize the session.'
        # load the headers
        self.session = requests.Session()
        with open(module_dir(params)) as f:
            self.params = json.load(f)
        self.session.headers = self.params.pop('headers')

        # load the cookies
        self.persist = persist and module_dir(persist)
        if not persist or not os.path.exists(self.persist):
            return
        with open(self.persist) as f:
            cj = requests.utils.cookiejar_from_dict(json.load(f))
            self.session.cookies = cj
Example #4
0
 def save_cookies(self):
     'Save the cookies in a JSON file.'
     if not self.persist:
         return
     with open(self.persist, 'w') as f:
         cj = requests.utils.dict_from_cookiejar(self.session.cookies)
         json.dump(cj, f)
Example #5
0
def show_image(file: BinaryIO, img_path='captcha.jpg'):
    'Save the image to a file if Pillow is not installed.'
    try:
        from PIL import Image
    except ImportError:
        with open(img_path, 'wb') as f:
            f.write(file.read())
        print('Open the image "%s" to solve the CAPTCHA.' % img_path)
    else:
        Image.open(file).show()
Example #6
0
                          for index, s in enumerate(stations)}
                         for field in (1, -2))
    if not initials:
        initials = {name[0] for name in names}.union({s[-1] for s in stations})
    for initial in initials:
        progress()
        for name, tmis_code in tmis.dfs(initial).items():
            # append as a new station
            if name not in names and tmis_code not in tmis_codes:
                yield ['', name, '', tmis_code, '']

            # replace in-place
            elif name in names:
                old = stations[names[name]]
                if not old[-2]:
                    old[-2] = tmis_code
                elif old[-2] != tmis_code:
                    conflict = 'TMIS code conflict: %s' % old
                    shell(dict(vars(), s=stations), '\n%s' % conflict)


if __name__ == '__main__':
    stations = list(combine_stations())
    stations.extend(heuristic_search(stations))

    shell(dict(vars(), s=stations), 'Well done.')

    with open(path, 'w') as f:
        print(dump_stations(stations), file=f)
        print('Dumped %d stations to "%s".' % (len(stations), path))
Example #7
0
    data = load_trains(file.read())
    codes = set(emu_codes(data))
    codes = sorted(codes, key=lambda code: ord(code[0]) * 1e4 + int(code[1:]))
    print('Ready, %d trains to be checked.' % len(codes))
    return codes


def batch_query(me: Automation, codes: Iterable, img_dir: str, models: TextIO):
    'Save screenshots and train models for all the given trains.'
    for code in codes:
        try:
            me.query(code)
        except LookupError:
            print(code, 'not found?')
        else:
            img_path = os.path.join(img_dir, '%s.png' % code)
            me.get_shot().save(img_path)
            print(code, me.get_text(), file=models)


if __name__ == '__main__':
    me = Automation()
    with open(path) as f:
        codes = unique_trains(f)
    img_dir = argv(3) or 'img'
    mkdir(img_dir)
    time.sleep(5)

    with open(argv(2) or 'models.txt', 'w') as f:
        batch_query(me, codes, img_dir, f)
Example #8
0
 def save_cookies(self):
     'Save the cookies in a JSON file.'
     if self.persist_cookies:
         cj = requests.utils.dict_from_cookiejar(self.session.cookies)
         with open(self.persist_cookies, 'w') as f:
             json.dump(cj, f)
Example #9
0
             '-': '|',
             ')': ''
         }.items()}).split('|')


def load_trains(script: str) -> dict:
    'Deserialize the jsonp script.'
    # https://kyfw.12306.cn/otn/resources/js/query/train_list.js
    json_text = script.partition('=')[2]
    return json.loads(json_text)


def parse_trains(data: dict) -> Iterable[Tuple[str, str, str, str]]:
    'Flatten the train list and return all items in it.'
    for day, trains in data.items():
        for train_type in trains.values():
            for train in train_type:
                train = train['train_no'], train['station_train_code']
                yield tuple([train[0]] + decompose(train[1]))


if __name__ == '__main__':
    print('Loading...')
    with open(path) as f:
        data = load_trains(f.read())
        t = list(set(parse_trains(data)))
    print('Ready.')

    for interpreter in shell, sql_shell:
        interpreter({'t': t}, 'len(t) == %d.' % len(t))
Example #10
0
        if not match:
            return print(page.name, 'X')

        for code, abbr, province in self.provinces:
            if province in match.group(1):
                station[-1] = abbr
                return print(station[1], '->', abbr)

    def convert(self, **kwargs) -> str:
        'Convert between language variants, such as zh-CN and zh-TW.'
        return self.site.get('parse', **kwargs)['parse']['displaytitle']


def load_provices(file: TextIO) -> Iterable[List[str]]:
    'Load the province list from a text file.'
    for line in file:
        if line.strip():
            yield line.split()


if __name__ == '__main__':
    with open(path) as f:
        stations = list(load_stations(f.read()))
    with open(argv(2) or 'provinces.txt') as f:
        provinces = list(load_provices(f))

    stations = Wikipedia(stations, provinces).fill_missing_provinces()

    with open(path, 'w') as f:
        print(dump_stations(stations), file=f)