示例#1
0
def generate_captcha(request_header):
    print('================================= GETTING CAPTCHA ==================================================')
    resp = requests.post(CAPTCHA_URL, headers=request_header)
    print(f'Captcha Response Code: {resp.status_code}')

    if resp.status_code == 200:
        return captcha_builder(resp.json())
示例#2
0
def generate_captcha(request_header, captcha_automation, api_key):
    print(
        "================================= GETTING CAPTCHA =================================================="
    )
    resp = requests.post(CAPTCHA_URL, headers=request_header)
    print(f'Captcha Response Code: {resp.status_code}')

    if resp.status_code == 200 and captcha_automation == "no":
        return captcha_builder(resp.json())
    elif resp.status_code == 200 and captcha_automation == "yes":
        return captcha_builder_auto(resp.json(), api_key)
示例#3
0
def main():
    booked_flag = False
    status_code = check_session()
    # Get Benificiary lists
    beneficiary_flag = False
    while beneficiary_flag == False:
        beneficiaries = requests.get(BENEFICIARIES_URL, headers=request_header)
        if beneficiaries.status_code == 200:
            beneficiaries = beneficiaries.json()["beneficiaries"]
            #print(beneficiaries)
            beneficiary_flag = True
        else:
            print("Unable to get benificiary.")
            time.sleep(2)
            
    refined_beneficiaries = []
    for beneficiary in beneficiaries:
        # To only pull those names that are not booked
        if beneficiary["vaccination_status"] == "Not Vaccinated" and beneficiary["appointments"] == []:
            beneficiary["age"] = datetime.datetime.today().year - int(beneficiary["birth_year"])
            tmp = {
                "bref_id": beneficiary["beneficiary_reference_id"], # Can be hardcoded too 
                "name": beneficiary["name"],
                "vaccine": beneficiary["vaccine"],
                "age": beneficiary["age"],
                "status": beneficiary["vaccination_status"],
            }
            refined_beneficiaries.append(tmp)        
    beneficiary_dtls = copy.deepcopy(refined_beneficiaries)
    
    if isinstance(start_date, int) and start_date == 2:
        start_date_datetime = (datetime.datetime.today() + datetime.timedelta(days=1)).strftime("%d-%m-%Y")
    elif isinstance(start_date, int) and start_date == 1:
        start_date_datetime = datetime.datetime.today().strftime("%d-%m-%Y")
    
    #Get district name and id
    #reqd_districts = [{
    #                    "district_id": 294,
    #                    "district_name": "BBMP"
    #                },
    #                {
    #                    "district_id": 265,
    #                    "district_name": "Bangalore Urban"
    #                }]
    #location_search_choice = int(input("Do you want to search by Pincode (enter 1) or District (enter 2) : ").strip())
    print(">>>>Searching by District<<<<")
    location_search_choice = 2 # For Testing 
    if location_search_choice == 2:
        reqd_districts = set_location_preference_by_district()
        location_dtls = copy.deepcopy(reqd_districts)
        base_url = CALENDAR_URL_DISTRICT
    else:
        reqd_pincodes = set_location_preference_by_pincode()
        location_dtls = copy.deepcopy(reqd_pincodes)
        base_url = CALENDAR_URL_PINCODE
    if vaccine_type:
        base_url += f"&vaccine={vaccine_type}"

    count_trial_booking = 1
    while booked_flag is not True:
        print(f"Trying to book no: {count_trial_booking}")
        #To find the list of available centers 
        resp_centers_available = []
        start = datetime.datetime.now()
        while len(resp_centers_available) < 1:
            #resp_vaccine_centers_all = get_vaccine_centers_in_district(base_url, start_date_datetime, location_dtls)
            if location_search_choice == 2:
                resp_centers_available = get_vaccine_centers_in_district(base_url, start_date_datetime, location_dtls)
            else:
                resp_centers_available = get_vaccine_centers_in_pincode(base_url, start_date_datetime, location_dtls)
            end = datetime.datetime.now()
            diff = end-start
            if diff.seconds > 250:
                status_code = check_session()
                if status_code != 200:
                    start = datetime.datetime.now()
        #print(resp_centers_available)
        winsound.Beep(650, 1000)
        winsound.Beep(750, 1000)
        options = copy.deepcopy(resp_centers_available)
        
        #Enter option to choose Center
        if len(options) > 1:
            header = ['#Center Number'] + ['Name - Pincode'] + ['Number of slots available'] + ['date']
            center_values = [[iter_num+1] + list([center_details['name'] + " - " + str(center_details['pincode'])]) + list([center_details['available']]) + list([center_details['date']]) for iter_num, center_details in enumerate(options)]
            print(tabulate.tabulate(center_values, header, tablefmt="grid"))
            center_choice_input = input("Enter the Center number from the table above where you would like to book : ")
            center_choice_num = int(center_choice_input)-1
        else:
            center_choice_num = 0
            
            
        
        #Count number of benificiary
        max_avaliable = options[center_choice_num]['available']
        can_be_given = 0
        if max_avaliable == 1:
            can_be_given = 1
        elif max_avaliable == 2:
            can_be_given = 2
        elif max_avaliable == 3:
            can_be_given = 3
        elif max_avaliable >= 4:
            can_be_given = 4
        
        ben_available = len(beneficiary_dtls)
        
        if ben_available <= can_be_given:
            can_be_given = ben_available
        
        ben_id =[]
        for each_ben_num in range(can_be_given):
            ben_id.append(beneficiary_dtls[each_ben_num]["bref_id"])
        new_req = {
            "beneficiaries": ben_id,
            "dose": dose_num,
            "center_id": options[center_choice_num]["center_id"],
            "session_id": options[center_choice_num]["session_id"],
            "slot": options[center_choice_num]["slots"][0],
        }
        
        # Write the captcha
        t1 = threading.Thread(target=make_alert_sound)
        t1.start()
        t1.join()
        valid_captcha = True
        #########################Testing#######################
        #break ###### remove
        while valid_captcha:
            resp = requests.post(CAPTCHA_URL, headers=request_header)
            if resp.status_code == 200:
                captcha = captcha_builder(resp.json())
            else:
                print("No Captcha generating. Check issue")
                print(resp)
            new_req["captcha"] = captcha
            
            resp = requests.post(BOOKING_URL, headers=request_header, json=new_req)
            #print(resp.status_code)
            print("\n")
            if resp.status_code == 401:
                print("TOKEN INVALID")
                #_ = get_vaccine_centers_in_district(base_url, start_date_datetime)
                #if len(_) < 1:
                    #break
            elif resp.status_code == 200:
                print("BOOKED! Check Cowin Portal.")
                print("Booked for " + str(can_be_given) + " person.")
                booked_flag = True
                valid_captcha = False
            elif resp.status_code == 400:
                print(f"Response: {resp.status_code} : {resp.text}")
                pass
            elif resp.status_code == 409: #Center is booked
                print(f"Center's slots got filled! Try Again. Response: {resp.status_code} : {resp.text}")
                valid_captcha = False
            else:
                print(f"Response: {resp.status_code} : {resp.text}")
                user_captcha_ans = input("Regenerate Captcha? y/n: ")
                if user_captcha_ans.lower() == "n":
                    break
                else:
                    continue 
                    
        count_trial_booking += 1
        #break ###### remove
        print(f"Booking Response Code: {resp.status_code}")