def like(self, photo_id): captcha = False captcha_result = '' captcha_sid = '' while True: owner_id, item_id = photo_id.split('_') if captcha == False: url = self.getUrl.likes.add(type='photo', owner_id=owner_id, item_id=item_id) else: url = self.getUrl.likes.add(type='photo', owner_id=owner_id, item_id=item_id, captcha_sid=captcha_sid, captcha_key=captcha_result) self.requester(url, self.session) response = self.requester.request() for r in response: j = json.loads(r[0].text) try: if 'likes' in j['response']: with open('files/completed', 'a') as file: file.write(str(owner_id) + '\n') print('+', owner_id) return except Exception as e: print(e) print(r[0].text) if 'error' in j: if j['error']['error_code'] == 14: print("Решаем капчу") captcha = Captcha() captcha_result = captcha.solve(self.session, j['error']['captcha_img']) captcha_sid = j['error']['captcha_sid'] if captcha_result == 'error': self.interval = 2000 return else: captcha = True print(self.name, 'Captcha', captcha_result) if j['error']['error_code'] == 15: with open('files/completed', 'a') as file: file.write(str(owner_id) + '\n') print('+', owner_id) return if j['error']['error_code'] == 17: self.interval = 99999 return
class TestCaptcha(TestCase): def setUp(self): self.c = Captcha() def test_solver_all_equal(self): result = self.c.solve('1111') self.assertEqual(4, result) def test_solver_all_different(self): result = self.c.solve('1234') self.assertEqual(0, result) def test_solver_simple_sums(self): result = self.c.solve('1122') self.assertEqual(3, result) def test_loop_sum(self): result = self.c.solve('91212129') self.assertEqual(9, result) def test_multiple_sums(self): result = self.c.solve('911229') self.assertEqual(12, result) def test_empty_input(self): result = self.c.solve('') self.assertEqual(0, result)