def create_posts(jurisdiction_id, settings): from opencivicdata.core.models import Organization, Jurisdiction division_id = Jurisdiction.objects.get(pk=jurisdiction_id).division_id districts = get_districts(settings) # create remaining orgs & add posts for chamber in districts: org = Organization.objects.get(jurisdiction_id=jurisdiction_id, classification=chamber) if chamber != "legislature": # check for name overrides title = settings.get(chamber + "_title") if not title: title = "Senator" if chamber == "upper" else "Representative" else: title = settings["legislature_title"] # add posts to org posts = [{ "label": label, "role": title, "division_id": get_division_id_for_role(settings, division_id, chamber, label), "maximum_memberships": maximum, } for label, maximum in districts[chamber].items()] updated = update_subobjects(org, "posts", posts) if updated: click.secho(f"updated {org} posts", fg="yellow")
def create_posts(jurisdiction_id, settings): from opencivicdata.core.models import Organization, Jurisdiction division_id = Jurisdiction.objects.get(pk=jurisdiction_id).division_id districts = get_districts(settings) # create remaining orgs & add posts for chamber in districts: org = Organization.objects.get(jurisdiction_id=jurisdiction_id, classification=chamber) if chamber != 'legislature': # check for name overrides title = settings.get(chamber + '_title') if not title: title = 'Senator' if chamber == 'upper' else 'Representative' else: title = settings['legislature_title'] # add posts to org posts = [{ 'label': label, 'role': title, 'division_id': get_division_id_for_role(settings, division_id, chamber, label), 'maximum_memberships': maximum, } for label, maximum in districts[chamber].items()] updated = update_subobjects(org, 'posts', posts) if updated: click.secho(f'updated {org} posts', fg='yellow')
def get_expected_districts(settings): expected = get_districts(settings) # remove vacancies vacancies = settings.get('vacancies', []) if vacancies: click.secho(f'Processing {len(vacancies)} vacancies:') for vacancy in settings.get('vacancies', []): if datetime.date.today() < vacancy['vacant_until']: expected[vacancy['chamber']][str(vacancy['district'])] -= 1 click.secho('\t{chamber}-{district} (until {vacant_until})'.format(**vacancy), fg='yellow') else: click.secho('\t{chamber}-{district} expired {vacant_until} remove & re-run'.format( **vacancy), fg='red') raise BadVacancy() return expected
def get_expected_districts(settings): expected = get_districts(settings) # remove vacancies vacancies = settings.get("vacancies", []) if vacancies: click.secho(f"Processing {len(vacancies)} vacancies:") for vacancy in settings.get("vacancies", []): if datetime.date.today() < vacancy["vacant_until"]: expected[vacancy["chamber"]][str(vacancy["district"])] -= 1 click.secho( "\t{chamber}-{district} (until {vacant_until})".format(**vacancy), fg="yellow" ) else: click.secho( "\t{chamber}-{district} expired {vacant_until} remove & re-run".format(**vacancy), fg="red", ) raise BadVacancy() return expected
def main(): parser = argparse.ArgumentParser() parser.add_argument('--token', help='Pass token directly') args = parser.parse_args() mobile = None try: if args.token: token = args.token else: mobile = input("Enter the registered mobile number: ") token = generate_token_OTP_pin(mobile) request_header = {"Authorization": f"Bearer {token}"} # Get Beneficiaries print("Fetching registered beneficiaries.. ") beneficiary_dtls = get_beneficiaries_pin(request_header) if len(beneficiary_dtls) == 0: print("There should be at least one beneficiary. Exiting.") os.system("pause") sys.exit(1) # Make sure all beneficiaries have the same type of vaccine vaccine_types = [ beneficiary['vaccine'] for beneficiary in beneficiary_dtls ] vaccines = Counter(vaccine_types) if len(vaccines.keys()) != 1: print( f"All beneficiaries in one attempt should have the same vaccine type. Found {len(vaccines.keys())}" ) os.system("pause") sys.exit(1) # Collect vaccination center preferance mode = int( input(""" ########################################## SELECT MODE OF BOOKING 1. Enter 0 for District wise search 2. Enter 1 for Pincode wise search ########################################## """)) if mode == 0: district_dtls = get_districts() # Set filter condition minimum_slots = int( input('Filter out centers with availability less than: ')) minimum_slots = minimum_slots if minimum_slots > len( beneficiary_dtls) else len(beneficiary_dtls) token_valid = True while token_valid: request_header = {"Authorization": f"Bearer {token}"} # call function to check and book slots token_valid = check_and_book(request_header, beneficiary_dtls, district_dtls, minimum_slots) # check if token is still valid beneficiaries_list = requests.get(BENEFICIARIES_URL, headers=request_header) if beneficiaries_list.status_code == 200: token_valid = True else: # if token invalid, regenerate OTP and new token beep(WARNING_BEEP_DURATION[0], WARNING_BEEP_DURATION[1]) print('Token is INVALID.') token_valid = False tryOTP = input('Try for a new Token? (y/n): ') if tryOTP.lower() == 'y': if mobile: tryOTP = input( f"Try for OTP with mobile number {mobile}? (y/n) : " ) if tryOTP.lower() == 'y': token = generate_token_OTP(mobile) token_valid = True else: token_valid = False print("Exiting") else: mobile = input( f"Enter 10 digit mobile number for new OTP generation? : " ) token = generate_token_OTP(mobile) token_valid = True else: print("Exiting") os.system("pause") else: pincode = int(input("Enter Pincode: ")) # Set filter condition minimum_slots = int( input('Filter out centers with availability less than: ')) minimum_slots = minimum_slots if minimum_slots > len( beneficiary_dtls) else len(beneficiary_dtls) token_valid = True while token_valid: request_header = {"Authorization": f"Bearer {token}"} # call function to check and book slots token_valid = check_and_book_pin(request_header, beneficiary_dtls, pincode, minimum_slots) # check if token is still valid beneficiaries_list = requests.get(BENEFICIARIES_URL, headers=request_header) if beneficiaries_list.status_code == 200: token_valid = True else: # if token invalid, regenerate OTP and new token beep_pin(WARNING_BEEP_DURATION[0], WARNING_BEEP_DURATION[1]) print('Token is INVALID.') token_valid = False tryOTP = input('Try for a new Token? (y/n): ') if tryOTP.lower() == 'y': if mobile: tryOTP = input( f"Try for OTP with mobile number {mobile}? (y/n) : " ) if tryOTP.lower() == 'y': token = generate_token_OTP_pin(mobile) token_valid = True else: token_valid = False print("Exiting") else: mobile = input( f"Enter 10 digit mobile number for new OTP generation? : " ) token = generate_token_OTP_pin(mobile) token_valid = True else: print("Exiting") os.system("pause") except Exception as e: print(str(e)) print('Exiting Script') os.system("pause")
def main(): parser = argparse.ArgumentParser() parser.add_argument('--token', help='Pass token directly') args = parser.parse_args() mobile = None try: base_request_header = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' } if args.token: token = args.token else: mobile = input("Enter the registered mobile number: ") token = generate_token_OTP(mobile, base_request_header) request_header = copy.deepcopy(base_request_header) request_header["Authorization"] = f"Bearer {token}" # Get Beneficiaries print("Fetching registered beneficiaries.. ") beneficiary_dtls = get_beneficiaries(request_header) if len(beneficiary_dtls) == 0: print("There should be at least one beneficiary. Exiting.") os.system("pause") sys.exit(1) # Make sure all beneficiaries have the same type of vaccine vaccine_types = [ beneficiary['vaccine'] for beneficiary in beneficiary_dtls ] vaccines = Counter(vaccine_types) if len(vaccines.keys()) != 1: print( f"All beneficiaries in one attempt should have the same vaccine type. Found {len(vaccines.keys())}" ) os.system("pause") sys.exit(1) print( "\n================================= Location Info =================================\n" ) # get search method to use search_option = input( """Search by Pincode? Or by State/District? \nEnter 1 for Pincode or 2 for State/District. (Default 2) : """ ) search_option = int(search_option) if int(search_option) in [1, 2 ] else 2 if search_option == 2: # Collect vaccination center preferance location_dtls = get_districts(request_header) else: # Collect vaccination center preferance location_dtls = get_pincodes() print( "\n================================= Additional Info =================================\n" ) # Set filter condition minimum_slots = input( f'Filter out centers with availability less than ? Minimum {len(beneficiary_dtls)} : ' ) if minimum_slots: minimum_slots = int(minimum_slots) if int(minimum_slots) >= len( beneficiary_dtls) else len(beneficiary_dtls) else: minimum_slots = len(beneficiary_dtls) # Get refresh frequency refresh_freq = input( 'How often do you want to refresh the calendar (in seconds)? Default 15. Minimum 5. : ' ) refresh_freq = int( refresh_freq) if refresh_freq and int(refresh_freq) >= 5 else 15 # Get search start date start_date = input( 'Search for next seven day starting from when?\nUse 1 for today, 2 for tomorrow, or provide a date in the format yyyy-mm-dd. Default 2: ' ) if not start_date: start_date = 2 elif start_date in ['1', '2']: start_date = int(start_date) else: try: datetime.datetime.strptime(start_date, '%Y-%m-%d') except ValueError: start_date = 2 print( "\n=========== CAUTION! =========== CAUTION! CAUTION! =============== CAUTION! =======\n" ) print( " ==== BE CAREFUL WITH THIS OPTION! AUTO-BOOKING WILL BOOK THE FIRST AVAILABLE CENTRE, DATE, AND SLOT! ==== " ) auto_book = input( "Do you want to enable auto-booking? (yes-please or no): ") token_valid = True while token_valid: request_header = copy.deepcopy(base_request_header) request_header["Authorization"] = f"Bearer {token}" # call function to check and book slots token_valid = check_and_book(request_header, beneficiary_dtls, location_dtls, search_option, min_slots=minimum_slots, ref_freq=refresh_freq, auto_book=auto_book, start_date=start_date) # check if token is still valid beneficiaries_list = requests.get(BENEFICIARIES_URL, headers=request_header) if beneficiaries_list.status_code == 200: token_valid = True else: # if token invalid, regenerate OTP and new token beep(WARNING_BEEP_DURATION[0], WARNING_BEEP_DURATION[1]) print('Token is INVALID.') token_valid = False tryOTP = input('Try for a new Token? (y/n): ') if tryOTP.lower() == 'y': if mobile: tryOTP = input( f"Try for OTP with mobile number {mobile}? (y/n) : " ) if tryOTP.lower() == 'y': token = generate_token_OTP(mobile, base_request_header) token_valid = True else: token_valid = False print("Exiting") else: mobile = input( f"Enter 10 digit mobile number for new OTP generation? : " ) token = generate_token_OTP(mobile, base_request_header) token_valid = True else: print("Exiting") os.system("pause") except Exception as e: print(str(e)) print('Exiting Script') os.system("pause")
def main(): parser = argparse.ArgumentParser() parser.add_argument('--token', help='Pass token directly') args = parser.parse_args() mobile = None try: if args.token: token = args.token else: mobile = input("Enter the registered mobile number: ") token = generate_token_OTP(mobile) request_header = {"Authorization": f"Bearer {token}"} # Get Beneficiaries print("Fetching registered beneficiaries.. ") beneficiary_dtls = get_beneficiaries(request_header) if len(beneficiary_dtls) == 0: print("There should be at least one beneficiary. Exiting.") os.system("pause") sys.exit(1) # Make sure all beneficiaries have the same type of vaccine vaccine_types = [ beneficiary['vaccine'] for beneficiary in beneficiary_dtls ] vaccines = Counter(vaccine_types) if len(vaccines.keys()) != 1: print( f"All beneficiaries in one attempt should have the same vaccine type. Found {len(vaccines.keys())}" ) os.system("pause") sys.exit(1) # Collect vaccination center preferance district_dtls = get_districts() print( "================================= Additional Info =================================" ) # Set filter condition minimum_slots = int( input( f'Filter out centers with availability less than ? Minimum {len(beneficiary_dtls)} : ' )) minimum_slots = minimum_slots if minimum_slots >= len( beneficiary_dtls) else len(beneficiary_dtls) # Get refresh frequency refresh_freq = input( 'How often do you want to refresh the calendar (in seconds)? Default 15. Minimum 5. : ' ) refresh_freq = int( refresh_freq) if refresh_freq and int(refresh_freq) >= 5 else 15 token_valid = True while token_valid: request_header = {"Authorization": f"Bearer {token}"} # call function to check and book slots token_valid = check_and_book(request_header, beneficiary_dtls, district_dtls, min_slots=minimum_slots, ref_freq=refresh_freq) # check if token is still valid beneficiaries_list = requests.get(BENEFICIARIES_URL, headers=request_header) if beneficiaries_list.status_code == 200: token_valid = True else: # if token invalid, regenerate OTP and new token beep(WARNING_BEEP_DURATION[0], WARNING_BEEP_DURATION[1]) print('Token is INVALID.') token_valid = False tryOTP = input('Try for a new Token? (y/n): ') if tryOTP.lower() == 'y': if mobile: tryOTP = input( f"Try for OTP with mobile number {mobile}? (y/n) : " ) if tryOTP.lower() == 'y': token = generate_token_OTP(mobile) token_valid = True else: token_valid = False print("Exiting") else: mobile = input( f"Enter 10 digit mobile number for new OTP generation? : " ) token = generate_token_OTP(mobile) token_valid = True else: print("Exiting") os.system("pause") except Exception as e: print(str(e)) print('Exiting Script') os.system("pause")
def expected_people(state): expected = 0 settings = get_settings()[state] for v in get_districts(settings).values(): expected += sum(v.values()) return expected