def main(): parser = commandline_parser( 'Get ofgem certificates for a given month & year') parser.add_argument('--generator', action='store', help='Generator ID to search for') args = parser.parse_args() setup_logging(args.debug, request_logging=args.request_debug) print("Contacting Ofgem and preparing to search.\n") ocs = CertificateSearch() ocs.start() print("Filtering search:") if args.station: print(" - cannot filter results based on station name") if args.scheme: if ocs.filter_scheme(args.scheme): print(" - scheme {}".format(args.scheme)) else: print("\nFailed to filter for scheme.") sys.exit(0) if args.generator: if ocs.filter_generator_id(args.generator.upper()): print(" - generator id {}".format(args.generator.upper())) else: print("\nFailed to filter by generator") sys.exit(0) if args.period: if ocs.set_period(args.period): print(' - period should be {}'.format(args.period)) else: print("\nFailed to set period") sys.exit(0) if ocs.get_data() is False: print("No data was returned from the Ofgem server") sys.exit(0) print("Total of %d records returned" % len(ocs)) fmt = StdoutFormatter("10s", "8s", "35s", "6s", "10s", "12d") print( fmt.titles("Issue Date", "Period", "Station Name", "Scheme", "Status", "Certificates")) for cert in ocs.certificates(): print( fmt.row(cert.issue_dt.strftime("%Y-%m-%d"), cert.period, cert.name, cert.scheme, cert.status, cert.certificates))
# if scheme == 'RO': # ws.write_merge(PERIOD_START - 2, # PERIOD_START - 2, # col, # col + 4, # 'RO: ' + station.accreditation + ' [' + station.commission_dt.strftime("%d %b %Y") + ']', # title_style) # elif scheme == 'REGO': # ws.write_merge(PERIOD_START - 3, # PERIOD_START - 3, # col, # col + 4, # 'REGO: ' + station.accreditation + ' [' + station.commission_dt.strftime("%d %b %Y") + ']', # title_style) cs = CertificateSearch() cs.set_start_month(start_month) cs.set_start_year(start_year) cs.set_finish_month(end_month) cs.set_finish_year(end_year) # print dir(cs) # print dir(cs.form) # print cs.form.fields cs.form.set_text_value('accreditation', station.accreditation) if not cs.get_data(): print "Unable to get any certificate data :-(" continue # print cs.certificates # continue
def main(): """ Function that actually does the work :-) """ parser = commandline_parser( 'Download bulk information from Ofgem to produce an Excel spreadsheet') parser.add_argument('start', type=int, help='Period to start (YYYYMM)') parser.add_argument('end', type=int, help='Period to finish (YYYYMM)') parser.add_argument('--filename', default='certificates.xls', help='Filename to export to') parser.add_argument('--stations', nargs='*', help='Stations to search for') args = parser.parse_args() print(args) if not args.filename.endswith('.xls'): args.filename += '.xls' periods = [] start_dt = _convert_type(args.start, 'period') end_dt = _convert_type(args.end, 'period') for yyy in range(start_dt.year, end_dt.year + 1): mmm = start_dt.month if start_dt.year == yyy else 1 mm2 = end_dt.month if end_dt.year == yyy else 12 for mon in range(mmm, mm2 + 1): periods.append(date(yyy, mon, 1)) print("Period covered will be {} to {}. A total of {} periods".format( start_dt.strftime("%b-%Y"), end_dt.strftime("%b-%Y"), len(periods))) stations = [] station_names = args.stations or [] if args.input is not None: with open(args.input) as fh: for line in fh.readlines(): station = line.strip() if '#' in station: (station, dummy_junk) = station.split('#', 1) station_names.append(station) if len(station_names) > 0: print("Station names to be searched for:") for stat in station_names: print(" - {}".format(stat)) while True: station = raw_input("Enter a station name (or blank to finish)") if station.strip() == '': break if ',' in station: for s in station.strip().split(','): station_names.append(s) else: station_names.append(station) if len(station_names) == 0: print("No stations to process. Exiting...") sys.exit(0) print("\nSearching for stations...") for name in station_names: print(" - {}".format(name)) sss = StationSearch() sss.start() if sss.filter_name(name) and sss.get_data(): stations.extend(sss.stations) print(" found") else: print(" no stations found") print("A total of {} stations will be recorded".format(len(stations))) wbb = Workbook() add_station_sheet(wbb, stations) print("\nGetting certificate data (this is quicker)...") certificates = {} for station in stations: print(" - {}".format(station.name)) ocs = CertificateSearch() ocs.start() if ocs.filter_generator_id(station.generator_id) and \ ocs.set_start_month(start_dt.month) and \ ocs.set_start_year(start_dt.year) and \ ocs.set_finish_month(end_dt.month) and \ ocs.set_finish_year(end_dt.year) and \ ocs.get_data(): certificates[station.name] = ocs.cert_list add_certificate_sheet(wbb, station, ocs.certificates) print(" added to spreadsheet") else: print(" nothing to add") wbb.save(args.filename) print("\nData saved to {}".format(args.filename))