def schedule_new_submission(self): # Attempt to schedule a new submission # Check that one has not been completed or in the process of, before submitting # Filters for Things which have been submitted successfully or # that are in the process of (ie attempts counter has not maxed out) recent_submissions = list(db_Thing.select(db_Thing).where((db_Thing.posted_name.is_null(False)) | (db_Thing.text_generation_attempts < 3 & db_Thing.reddit_post_attempts < 1)). where(fn.Lower(db_Thing.subreddit) == self._subreddit.lower()). where(db_Thing.source_name == 't3_new_submission'). where((datetime.timestamp(datetime.utcnow()) - db_Thing.created_utc) < self._new_submission_frequency.total_seconds())) if recent_submissions: # There was a submission recently so we cannot proceed. return logging.info(f"Scheduling a new submission on {self._subreddit}") new_submission_thing = {} new_submission_thing['source_name'] = 't3_new_submission' new_submission_thing['subreddit'] = self._subreddit text_generation_parameters = self._default_text_generation_parameters.copy() text_generation_parameters['prompt'] = self._get_random_new_submission_tag() text_generation_parameters['max_length'] = 1000 new_submission_thing['text_generation_parameters'] = text_generation_parameters return db_Thing.create(**new_submission_thing)
def pending_new_submission_jobs(self): # A list of pending Submission Things from the database that have had text generated, # but not a reddit post attempt return list(db_Thing.select(db_Thing). where(db_Thing.source_name == 't3_new_submission'). where(db_Thing.reddit_post_attempts < 1). where(db_Thing.generated_text.is_null(False)). where(db_Thing.posted_name.is_null()))
def insert_praw_thing_into_database(self, praw_thing, text_generation_parameters=None): record_dict = {} record_dict['source_name'] = praw_thing.name if text_generation_parameters: # If we want to generate a text reply, then include these parameters in the record record_dict['text_generation_parameters'] = text_generation_parameters return db_Thing.create(**record_dict)
def do_POST(self): global store print "POST %s" % self.path bits = urllib.unquote(self.path).split('/') x = None if bits[1] == "people": clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST people: %s" % j x = Person.of_dict(j) elif bits[1] == 'service': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST service: %s" % j x = Service.of_dict(j) elif bits[1] == 'att': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) mime, pdict = cgi.parse_header(self.headers.getheader('content-type')) c = self.rfile.read(int(clen)) print "POST att: %s" % bits[1] x = Att.insert(unicode(bits[2]), c, unicode(mime)) elif bits[1] == 'thing': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST thing: %s" % j x = Thing.of_dict(j) elif bits[1] == 'credential': clen, pdict = cgi.parse_header(self.headers.getheader('content-length')) c = self.rfile.read(int(clen)) j = simplejson.loads(unicode(c)) print "POST credential: %s" % j x = Credential.of_dict(j) try: store.commit() except: store = get_store() store.commit() if x: self.send_response(200) else: self.send_response(500) self.end_headers()
def top_pending_jobs(self): """ Get a list of text that need text to be generated, by treating each database Thing record as a 'job'. Three attempts at text generation are allowed. """ query = db_Thing.select(db_Thing).\ where(db_Thing.text_generation_parameters.is_null(False)).\ where(db_Thing.generated_text.is_null()).\ where(db_Thing.text_generation_attempts < 3).\ order_by(db_Thing.created_utc) return list(query)
def do_GET(self): bits = urllib.unquote(self.path).split('/') try: x = None if bits[1] == "ping": self.send_response(200) self.end_headers() self.wfile.write("pong") elif bits[1] == "people": self.output_json(Person.retrieve(bits[2])) elif bits[1] == "service": self.output_json(Service.retrieve(bits[2],bits[3])) elif bits[1] == "thing": self.output_json(Thing.retrieve(bits[2])) elif bits[1] == "att": x = Att.retrieve(bits[2]) if x: self.send_response(200) self.send_header('Content-type', x.mime) self.send_header('Content-length', x.size) self.end_headers() self.wfile.write(x.body) else: self.send_response(404) self.end_headers() self.wfile.write('404 Not Found') elif bits[1] == "credential": self.output_json(Credential.retrieve(bits[2])) except IndexError: print "GET error! self:%s\n%s" % (self.path, self.headers)
def is_praw_thing_in_database(self, praw_thing): # Note that this is using the prefixed reddit id, ie t3_, t1_ # do not mix it with the unprefixed version which is called id! record = db_Thing.get_or_none(db_Thing.source_name == praw_thing.name) return record