def handler(event, context): update_html_earilier_than = str(datetime.utcnow() - timedelta( seconds=int(config.HTML_UPDATE_INTERVAL_IN_SECONDS.get_value()))) result_iterator = Startup.scan( Startup.html_download_at < update_html_earilier_than, limit=10, attributes_to_get=[ "id", "html_download_at", "details_update_at", ], ) sqs_entries = list() for startup in result_iterator: queue_item = QueueItem( type=QueueItem.Types.download_project_html, data=startup.attribute_values, ) entry = { "Id": fingerprint.of_text(startup.id), "MessageBody": json.dumps(queue_item.to_dict()), } sqs_entries.append(entry) sqs_client.send_message_batch( QueueUrl=config.SQS_QUEUE_URL.get_value(), Entries=sqs_entries, )
def mindwaveArray(self): output = { "delta": deque(maxlen=30), "theta": deque(maxlen=30), "lowAlpha": deque(maxlen=30), "highAlpha": deque(maxlen=30), "lowBeta": deque(maxlen=30), "highBeta": deque(maxlen=30), "lowGamma": deque(maxlen=30), "midGamma": deque(maxlen=30), } while not thread_stop_event.isSet(): mindwaveDataPointReader = MindwaveDataPointReader() mindwaveDataPointReader.start() if mindwaveDataPointReader.isConnected(): while True: dataPoint = mindwaveDataPointReader.readNextDataPoint() if dataPoint.__class__ is EEGPowersDataPoint: newData = dataPoint.__dict__ for k, v in newData.items(): if k in output.keys(): output[k].appendleft(v) prepData = copy.deepcopy(output) for k, v in prepData.items(): prepData[k] = list(v) socketio.emit( "newnumber", {"output": json.dumps(prepData)}, namespace="/test", ) else: output = "Could not connect to the Mindwave Mobile device, retrying ..." socketio.emit("newnumber", {"output": output}, namespace="/test") sleep(self.delay)
def update_json_bin(new, json_bin_id, json_bin_key): """Updates JSON storeed at `json_bin_id`""" url = f"https://api.jsonbin.io/b/{json_bin_id}" headers = {"secret-key": json_bin_key, "Content-Type": "application/json"} resp = requests.put(url, headers=headers, data=json.dumps(new)) if resp.status_code != 200: LOGGER.warning( "Failed to update tweet DB. <%s>: %s", resp.status_code, resp.text ) LOGGER.info("Successfully updated JSONBIN: %s", resp.text)
def process_download_project_html(self, sqs_client, record, config): receipt_handle = record["receiptHandle"] startup = Startup.get( hash_key=self.data["id"], attributes_to_get=[ "id", "html_download_at", "details_update_at", ] ) dt_now = datetime.utcnow() dt_html_download_at = parse(startup.html_download_at) if (dt_now - dt_html_download_at).total_seconds() > int(config.HTML_UPDATE_INTERVAL_IN_SECONDS.get_value()): url = startup.url content = requests.get(url).content compressed_raw_html = gzip.compress(content) html_download_at = str(datetime.utcnow()) startup.update( actions=[ Startup.compressed_raw_html.set(compressed_raw_html), Startup.html_download_at.set(html_download_at), ] ) sqs_client.delete_message( QueueUrl=config.SQS_QUEUE_URL.get_value(), ReceiptHandle=receipt_handle, ) # send parse scheduled job back to queue queue_item = QueueItem( type=QueueItem.Types.parse_project_html, data=startup.attribute_values, ) sqs_client.send_message( QueueUrl=config.SQS_QUEUE_URL.get_value(), MessageBody=json.dumps(queue_item.to_dict()), )
def jprint(data): print(json.dumps(data, indent=4, sort_keys=True))
def send_json(socket: zmq.Socket, data: dict, **kw) -> None: """ Send a dictionary. """ s_data = json.dumps(data) socket.send_string(s_data, **kw)