async def handler(ans: Message, match) -> str: url = get_image_url(ans) if not url: return "Не удалось найти картинку для поиска в Вашем сообщении!" try: text = await search(url) except Exception: return "Ой, произошла ошибка, попробуйте еще раз." return text
def browse(self): posts = utils.get_thread(self.board, self.threadId)["posts"] for p in posts: if ('ext' not in p) or ("filename" not in p): continue if p["ext"] in utils.image_ext: thumb = utils.get_thumb_url(self.board, p["tim"]) img_url = utils.get_image_url(self.board, p["tim"], p["ext"]) utils.add_image(p['filename'], thumb, img_url) else: thumb = utils.get_thumb_url(self.board, p["tim"]) vid_url = utils.get_image_url(self.board, p["tim"], p["ext"]) utils.add_video(p['filename'], thumb, vid_url) control.directory_end() return
def pretty_message(message): dt = datetime.fromtimestamp(message['time']) dt_str = dt.strftime('%H:%M:%S') print(message['name'], dt_str) print(message['text']) print() if message['text'] == 'бот пришли пёсика': url = get_image_url() webbrowser.open(url)
def pretty_message(self, message): dt = datetime.fromtimestamp(message['time']) dt_str = dt.strftime('%H:%M:%S') self.messages.append(message['name'] + ' ' + dt_str) self.messages.append(message['text']) self.messages.append('') if message['text'] == 'бот пришли пёсика': self.messages.append('*открываю браузер*') self.messages.append('') url = get_image_url() webbrowser_open(url) self.messages.repaint()
def get_image(html, query, current): # extract image url img_list = get_img_list(html) num = 0 if img_list: count = 0 # extract title title_list = get_title_list(html) title_list = remove_label(title_list) # extract size width_list = get_width_list(html) height_list = get_height_list(html) for img_url in img_list: # extract url img_url = get_image_url(img_url) # crawl image on the page try: # detect the image(find the bigest face in the image) result = api.detection.detect(url=img_url, mode='oneface') if result['face']: image_path = '%s%s.jpg' % (IMAGE_DIR, current+num) # download image filename, msg = urllib.urlretrieve(img_url, image_path) # if download successfully if msg['Content-Length']: # append tag to image append_tags(image_path, get_image_size(width_list[count]), get_image_size(height_list[count]), get_image_title(title_list[count]), img_url, query, result['face'][0]['attribute']) # output info print "%s: %s" % (current+num, img_url) num += 1 if num >= EACH: break except Exception as err: print(err) count += 1 else: print "%s didn't have .jpg format picture" % query return current+num
def info_image_url(image_uri): return redirect(get_image_url(str(image_uri)))
def csvimport(dbo, csvdata, encoding = "utf8", user = "", createmissinglookups = False, cleartables = False, checkduplicates = False): """ Imports the csvdata. createmissinglookups: If a lookup value is given that's not in our data, add it cleartables: Clear down the animal, owner and adoption tables before import """ # Convert line endings to standard unix lf to prevent # the Python CSV importer barfing. csvdata = csvdata.replace("\r\n", "\n") csvdata = csvdata.replace("\r", "\n") if user == "": user = "******" else: user = "******" % user reader = None if encoding == "utf8": reader = utils.UnicodeCSVReader(StringIO(csvdata)) else: reader = utils.UnicodeCSVReader(StringIO(csvdata), encoding=encoding) # Make sure we have a valid header cols = None for row in reader: cols = row break if cols is None: asynctask.set_last_error(dbo, "Your CSV file is empty") return onevalid = False hasanimal = False hasanimalname = False hasmed = False hasvacc = False hasperson = False haspersonlastname = False haspersonname = False haslicence = False haslicencenumber = False hasmovement = False hasmovementdate = False hasdonation = False hasdonationamount = False hasoriginalowner = False hasoriginalownerlastname = False for col in cols: if col in VALID_FIELDS: onevalid = True if col.startswith("ANIMAL"): hasanimal = True if col == "ANIMALNAME": hasanimalname = True if col.startswith("ORIGINALOWNER"): hasoriginalowner = True if col.startswith("VACCINATION"): hasvacc = True if col.startswith("MEDICAL"): hasmed = True if col.startswith("LICENSE"): haslicence = True if col == "LICENSENUMBER": haslicencenumber = True if col == "ORIGINALOWNERLASTNAME": hasoriginalownerlastname = True if col.startswith("PERSON"): hasperson = True if col == "PERSONLASTNAME": haspersonlastname = True if col == "PERSONNAME": haspersonname = True if col.startswith("MOVEMENT"): hasmovement = True if col == "MOVEMENTDATE": hasmovementdate = True if col.startswith("DONATION"): hasdonation = True if col == "DONATIONAMOUNT": hasdonationamount = True # Any valid fields? if not onevalid: asynctask.set_last_error(dbo, "Your CSV file did not contain any fields that ASM recognises") return # If we have any animal fields, make sure at least ANIMALNAME is supplied if hasanimal and not hasanimalname: asynctask.set_last_error(dbo, "Your CSV file has animal fields, but no ANIMALNAME column") return # If we have any person fields, make sure at least PERSONLASTNAME or PERSONNAME is supplied if hasperson and not haspersonlastname and not haspersonname: asynctask.set_last_error(dbo, "Your CSV file has person fields, but no PERSONNAME or PERSONLASTNAME column") return # If we have any original owner fields, make sure at least ORIGINALOWNERLASTNAME is supplied if hasoriginalowner and not hasoriginalownerlastname: asynctask.set_last_error(dbo, "Your CSV file has original owner fields, but no ORIGINALOWNERLASTNAME column") return # If we have any movement fields, make sure MOVEMENTDATE is supplied if hasmovement and not hasmovementdate: asynctask.set_last_error(dbo, "Your CSV file has movement fields, but no MOVEMENTDATE column") return # If we have any donation fields, we need an amount if hasdonation and not hasdonationamount: asynctask.set_last_error(dbo, "Your CSV file has donation fields, but no DONATIONAMOUNT column") return # We also need a valid person if hasdonation and not (haspersonlastname or haspersonname): asynctask.set_last_error(dbo, "Your CSV file has donation fields, but no person to apply the donation to") return # If we have any med fields, we need an animal if hasmed and not hasanimal: asynctask.set_last_error(dbo, "Your CSV file has medical fields, but no animal to apply them to") return # If we have any vacc fields, we need an animal if hasvacc and not hasanimal: asynctask.set_last_error(dbo, "Your CSV file has vaccination fields, but no animal to apply them to") return # If we have licence fields, we need a number if haslicence and not haslicencenumber: asynctask.set_last_error(dbo, "Your CSV file has license fields, but no LICENSENUMBER column") return # We also need a valid person if haslicence and not (haspersonlastname or haspersonname): asynctask.set_last_error(dbo, "Your CSV file has license fields, but no person to apply the license to") # Read the whole CSV file into a list of maps. Note, the # reader has a cursor at the second row already because # we read the header in the first row above data = [] for row in reader: currow = {} for i, col in enumerate(row): if i >= len(cols): continue # skip if we run out of cols currow[cols[i]] = col data.append(currow) al.debug("reading CSV data, found %d rows" % len(data), "csvimport.csvimport", dbo) # If we're clearing down tables first, do it now if cleartables: al.warn("Resetting the database by removing all non-lookup data", "csvimport.csvimport", dbo) dbupdate.reset_db(dbo) # Now that we've read them in, go through all the rows # and start importing. errors = [] rowno = 1 asynctask.set_progress_max(dbo, len(data)) for row in data: al.debug("import csv: row %d of %d" % (rowno, len(data)), "csvimport.csvimport", dbo) asynctask.increment_progress_value(dbo) # Should we stop? if asynctask.get_cancel(dbo): break # Do we have animal data to read? animalid = 0 if hasanimal and gks(row, "ANIMALNAME") != "": a = {} a["animalname"] = gks(row, "ANIMALNAME") a["sheltercode"] = gks(row, "ANIMALCODE") a["shortcode"] = gks(row, "ANIMALCODE") if gks(row, "ANIMALSEX") == "": a["sex"] = "2" # Default unknown if not set else: a["sex"] = gks(row, "ANIMALSEX").lower().startswith("m") and "1" or "0" a["basecolour"] = gkl(dbo, row, "ANIMALCOLOR", "basecolour", "BaseColour", createmissinglookups) if a["basecolour"] == "0": a["basecolour"] = str(configuration.default_colour(dbo)) a["species"] = gkl(dbo, row, "ANIMALSPECIES", "species", "SpeciesName", createmissinglookups) if a["species"] == "0": a["species"] = str(configuration.default_species(dbo)) a["animaltype"] = gkl(dbo, row, "ANIMALTYPE", "animaltype", "AnimalType", createmissinglookups) if a["animaltype"] == "0": a["animaltype"] = str(configuration.default_type(dbo)) a["breed1"] = gkbr(dbo, row, "ANIMALBREED1", a["species"], createmissinglookups) if a["breed1"] == "0": a["breed1"] = str(configuration.default_breed(dbo)) a["breed2"] = gkbr(dbo, row, "ANIMALBREED2", a["species"], createmissinglookups) if a["breed2"] != "0" and a["breed2"] != a["breed1"]: a["crossbreed"] = "on" a["size"] = gkl(dbo, row, "ANIMALSIZE", "lksize", "Size", False) if gks(row, "ANIMALSIZE") == "": a["size"] = str(configuration.default_size(dbo)) a["internallocation"] = gkl(dbo, row, "ANIMALLOCATION", "internallocation", "LocationName", createmissinglookups) if a["internallocation"] == "0": a["internallocation"] = str(configuration.default_location(dbo)) a["unit"] = gks(row, "ANIMALUNIT") a["comments"] = gks(row, "ANIMALCOMMENTS") a["markings"] = gks(row, "ANIMALMARKINGS") a["hiddenanimaldetails"] = gks(row, "ANIMALHIDDENDETAILS") a["healthproblems"] = gks(row, "ANIMALHEALTHPROBLEMS") a["notforadoption"] = gkbi(row, "ANIMALNOTFORADOPTION") a["nonshelter"] = gkbi(row, "ANIMALNONSHELTER") a["housetrained"] = gkynu(row, "ANIMALHOUSETRAINED") a["goodwithcats"] = gkynu(row, "ANIMALGOODWITHCATS") a["goodwithdogs"] = gkynu(row, "ANIMALGOODWITHDOGS") a["goodwithkids"] = gkynu(row, "ANIMALGOODWITHKIDS") a["reasonforentry"] = gks(row, "ANIMALREASONFORENTRY") a["estimatedage"] = gks(row, "ANIMALAGE") a["dateofbirth"] = gkd(dbo, row, "ANIMALDOB", True) if gks(row, "ANIMALDOB") == "" and a["estimatedage"] != "": a["dateofbirth"] = "" # if we had an age and dob was blank, prefer the age a["datebroughtin"] = gkd(dbo, row, "ANIMALENTRYDATE", True) a["deceaseddate"] = gkd(dbo, row, "ANIMALDECEASEDDATE") a["neutered"] = gkbc(row, "ANIMALNEUTERED") a["neutereddate"] = gkd(dbo, row, "ANIMALNEUTEREDDATE") if a["neutereddate"] != "": a["neutered"] = "on" a["microchipnumber"] = gks(row, "ANIMALMICROCHIP") if a["microchipnumber"] != "": a["microchipped"] = "on" a["microchipdate"] = gkd(dbo, row, "ANIMALMICROCHIPDATE") # image data if any was supplied imagedata = gks(row, "ANIMALIMAGE") if imagedata.startswith("http"): # It's a URL, get the image from the remote server r = utils.get_image_url(imagedata, timeout=5000) if r["status"] == 200: al.debug("retrieved image from %s (%s bytes)" % (imagedata, len(r["response"])), "csvimport.csvimport", dbo) imagedata = "data:image/jpeg;base64,%s" % base64.b64encode(r["response"]) else: row_error(errors, "animal", rowno, row, "error reading image from '%s': %s" % (imagedata, r), dbo, sys.exc_info()) continue elif imagedata.startswith("data:image"): # It's a base64 encoded data URI - do nothing as attach_file requires it pass else: # We don't know what it is, don't try and do anything with it imagedata = "" # If an original owner is specified, create a person record # for them and attach it to the animal as original owner if gks(row, "ORIGINALOWNERLASTNAME") != "": p = {} p["title"] = gks(row, "ORIGINALOWNERTITLE") p["initials"] = gks(row, "ORIGINALOWNERINITIALS") p["forenames"] = gks(row, "ORIGINALOWNERFIRSTNAME") p["surname"] = gks(row, "ORIGINALOWNERLASTNAME") p["address"] = gks(row, "ORIGINALOWNERADDRESS") p["town"] = gks(row, "ORIGINALOWNERCITY") p["county"] = gks(row, "ORIGINALOWNERSTATE") p["postcode"] = gks(row, "ORIGINALOWNERZIPCODE") p["jurisdiction"] = gkl(dbo, row, "ORIGINALOWNERJURISDICTION", "jurisdiction", "JurisdictionName", createmissinglookups) p["hometelephone"] = gks(row, "ORIGINALOWNERHOMEPHONE") p["worktelephone"] = gks(row, "ORIGINALOWNERWORKPHONE") p["mobiletelephone"] = gks(row, "ORIGINALOWNERCELLPHONE") p["emailaddress"] = gks(row, "ORIGINALOWNEREMAIL") try: if checkduplicates: dups = person.get_person_similar(dbo, p["emailaddress"], p["surname"], p["forenames"], p["address"]) if len(dups) > 0: a["originalowner"] = str(dups[0]["ID"]) if "originalowner" not in a: ooid = person.insert_person_from_form(dbo, utils.PostedData(p, dbo.locale), user, geocode=False) a["originalowner"] = str(ooid) # Identify an ORIGINALOWNERADDITIONAL additional fields and create them create_additional_fields(dbo, row, errors, rowno, "ORIGINALOWNERADDITIONAL", "person", ooid) except Exception as e: row_error(errors, "originalowner", rowno, row, e, dbo, sys.exc_info()) try: if checkduplicates: dup = animal.get_animal_sheltercode(dbo, a["sheltercode"]) if dup is not None: animalid = dup["ID"] if animalid == 0: animalid, newcode = animal.insert_animal_from_form(dbo, utils.PostedData(a, dbo.locale), user) # Identify any ANIMALADDITIONAL additional fields and create them create_additional_fields(dbo, row, errors, rowno, "ANIMALADDITIONAL", "animal", animalid) # If we have some image data, add it to the animal if len(imagedata) > 0: imagepost = utils.PostedData({ "filename": "image.jpg", "filetype": "image/jpeg", "filedata": imagedata }, dbo.locale) media.attach_file_from_form(dbo, user, media.ANIMAL, animalid, imagepost) except Exception as e: row_error(errors, "animal", rowno, row, e, dbo, sys.exc_info()) # Person data? personid = 0 if hasperson and (gks(row, "PERSONLASTNAME") != "" or gks(row, "PERSONNAME") != ""): p = {} p["ownertype"] = gks(row, "PERSONCLASS") if p["ownertype"] != "1" and p["ownertype"] != "2": p["ownertype"] = "1" p["title"] = gks(row, "PERSONTITLE") p["initials"] = gks(row, "PERSONINITIALS") p["forenames"] = gks(row, "PERSONFIRSTNAME") p["surname"] = gks(row, "PERSONLASTNAME") # If we have a person name, all upto the last space is first names, # everything after the last name if gks(row, "PERSONNAME") != "": pname = gks(row, "PERSONNAME") if pname.find(" ") != -1: p["forenames"] = pname[0:pname.rfind(" ")] p["surname"] = pname[pname.rfind(" ")+1:] else: p["surname"] = pname p["address"] = gks(row, "PERSONADDRESS") p["town"] = gks(row, "PERSONCITY") p["county"] = gks(row, "PERSONSTATE") p["postcode"] = gks(row, "PERSONZIPCODE") p["jurisdiction"] = gkl(dbo, row, "PERSONJURISDICTION", "jurisdiction", "JurisdictionName", createmissinglookups) p["hometelephone"] = gks(row, "PERSONHOMEPHONE") p["worktelephone"] = gks(row, "PERSONWORKPHONE") p["mobiletelephone"] = gks(row, "PERSONCELLPHONE") p["emailaddress"] = gks(row, "PERSONEMAIL") p["gdprcontactoptin"] = gks(row, "PERSONGDPRCONTACTOPTIN") flags = gks(row, "PERSONFLAGS") if gkb(row, "PERSONFOSTERER"): flags += ",fosterer" if gkb(row, "PERSONMEMBER"): flags += ",member" if gkb(row, "PERSONDONOR"): flags += ",donor" p["flags"] = flags p["comments"] = gks(row, "PERSONCOMMENTS") p["membershipnumber"] = gks(row, "PERSONMEMBERSHIPNUMBER") p["membershipexpires"] = gkd(dbo, row, "PERSONMEMBERSHIPEXPIRY") p["matchactive"] = gkbi(row, "PERSONMATCHACTIVE") if p["matchactive"] == "1": if "PERSONMATCHADDED" in cols: p["matchadded"] = gkd(dbo, row, "PERSONMATCHADDED") if "PERSONMATCHEXPIRES" in cols: p["matchexpires"] = gkd(dbo, row, "PERSONMATCHEXPIRES") if "PERSONMATCHSEX" in cols: p["matchsex"] = gks(row, "PERSONMATCHSEX").lower().startswith("m") and "1" or "0" if "PERSONMATCHSIZE" in cols: p["matchsize"] = gkl(dbo, row, "PERSONMATCHSIZE", "lksize", "Size", False) if "PERSONMATCHCOLOR" in cols: p["matchcolour"] = gkl(dbo, row, "PERSONMATCHCOLOR", "basecolour", "BaseColour", createmissinglookups) if "PERSONMATCHAGEFROM" in cols: p["matchagefrom"] = gks(row, "PERSONMATCHAGEFROM") if "PERSONMATCHAGETO" in cols: p["matchageto"] = gks(row, "PERSONMATCHAGETO") if "PERSONMATCHTYPE" in cols: p["matchanimaltype"] = gkl(dbo, row, "PERSONMATCHTYPE", "animaltype", "AnimalType", createmissinglookups) if "PERSONMATCHSPECIES" in cols: p["matchspecies"] = gkl(dbo, row, "PERSONMATCHSPECIES", "species", "SpeciesName", createmissinglookups) if "PERSONMATCHBREED1" in cols: p["matchbreed"] = gkbr(dbo, row, "PERSONMATCHBREED1", p["matchspecies"], createmissinglookups) if "PERSONMATCHBREED2" in cols: p["matchbreed2"] = gkbr(dbo, row, "PERSONMATCHBREED2", p["matchspecies"], createmissinglookups) if "PERSONMATCHGOODWITHCATS" in cols: p["matchgoodwithcats"] = gkynu(row, "PERSONMATCHGOODWITHCATS") if "PERSONMATCHGOODWITHDOGS" in cols: p["matchgoodwithdogs"] = gkynu(row, "PERSONMATCHGOODWITHDOGS") if "PERSONMATCHGOODWITHCHILDREN" in cols: p["matchgoodwithchildren"] = gkynu(row, "PERSONMATCHGOODWITHCHILDREN") if "PERSONMATCHHOUSETRAINED" in cols: p["matchhousetrained"] = gkynu(row, "PERSONMATCHHOUSETRAINED") if "PERSONMATCHCOMMENTSCONTAIN" in cols: p["matchcommentscontain"] = gks(row, "PERSONMATCHCOMMENTSCONTAIN") try: if checkduplicates: dups = person.get_person_similar(dbo, p["emailaddress"], p["surname"], p["forenames"], p["address"]) if len(dups) > 0: personid = dups[0].ID # Merge flags and any extra details person.merge_flags(dbo, user, personid, flags) person.merge_gdpr_flags(dbo, user, personid, p["gdprcontactoptin"]) # If we deduplicated on the email address, and address details are # present, assume that they are newer than the ones we had and update them # (we do this by setting force=True parameter to merge_person_details, # otherwise we do a regular merge which only fills in any blanks) person.merge_person_details(dbo, user, personid, p, force=dups[0].EMAILADDRESS == p["emailaddress"]) if personid == 0: personid = person.insert_person_from_form(dbo, utils.PostedData(p, dbo.locale), user, geocode=False) # Identify any PERSONADDITIONAL additional fields and create them create_additional_fields(dbo, row, errors, rowno, "PERSONADDITIONAL", "person", personid) except Exception as e: row_error(errors, "person", rowno, row, e, dbo, sys.exc_info()) # Movement to tie animal/person together? movementid = 0 if hasmovement and personid != 0 and animalid != 0 and gks(row, "MOVEMENTDATE") != "": m = {} m["person"] = str(personid) m["animal"] = str(animalid) movetype = gks(row, "MOVEMENTTYPE") if movetype == "": movetype = "1" # Default to adoption if not supplied m["type"] = str(movetype) m["movementdate"] = gkd(dbo, row, "MOVEMENTDATE", True) m["returndate"] = gkd(dbo, row, "MOVEMENTRETURNDATE") m["comments"] = gks(row, "MOVEMENTCOMMENTS") m["returncategory"] = str(configuration.default_entry_reason(dbo)) try: movementid = movement.insert_movement_from_form(dbo, user, utils.PostedData(m, dbo.locale)) except Exception as e: row_error(errors, "movement", rowno, row, e, dbo, sys.exc_info()) # Donation? if hasdonation and personid != 0 and gkc(row, "DONATIONAMOUNT") != 0: d = {} d["person"] = str(personid) d["animal"] = str(animalid) d["movement"] = str(movementid) d["amount"] = str(gkc(row, "DONATIONAMOUNT")) d["fee"] = str(gkc(row, "DONATIONFEE")) d["comments"] = gks(row, "DONATIONCOMMENTS") d["received"] = gkd(dbo, row, "DONATIONDATE", True) d["chequenumber"] = gks(row, "DONATIONCHECKNUMBER") d["type"] = gkl(dbo, row, "DONATIONTYPE", "donationtype", "DonationName", createmissinglookups) if d["type"] == "0": d["type"] = str(configuration.default_donation_type(dbo)) d["payment"] = gkl(dbo, row, "DONATIONPAYMENT", "donationpayment", "PaymentName", createmissinglookups) if d["payment"] == "0": d["payment"] = "1" try: financial.insert_donation_from_form(dbo, user, utils.PostedData(d, dbo.locale)) except Exception as e: row_error(errors, "payment", rowno, row, e, dbo, sys.exc_info()) if movementid != 0: movement.update_movement_donation(dbo, movementid) # Vaccination? if hasvacc and animalid != 0 and gks(row, "VACCINATIONDUEDATE") != "": v = {} v["animal"] = str(animalid) v["type"] = gkl(dbo, row, "VACCINATIONTYPE", "vaccinationtype", "VaccinationType", createmissinglookups) if v["type"] == "0": v["type"] = str(configuration.default_vaccination_type(dbo)) v["required"] = gkd(dbo, row, "VACCINATIONDUEDATE", True) v["given"] = gkd(dbo, row, "VACCINATIONGIVENDATE") v["expires"] = gkd(dbo, row, "VACCINATIONEXPIRESDATE") v["batchnumber"] = gks(row, "VACCINATIONBATCHNUMBER") v["manufacturer"] = gks(row, "VACCINATIONMANUFACTURER") v["comments"] = gks(row, "VACCINATIONCOMMENTS") try: medical.insert_vaccination_from_form(dbo, user, utils.PostedData(v, dbo.locale)) except Exception as e: row_error(errors, "vaccination", rowno, row, e, dbo, sys.exc_info()) # Medical? if hasmed and animalid != 0 and gks(row, "MEDICALGIVENDATE") != "" and gks(row, "MEDICALNAME") != "": m = {} m["animal"] = str(animalid) m["treatmentname"] = gks(row, "MEDICALNAME") m["dosage"] = gks(row, "MEDICALDOSAGE") m["startdate"] = gkd(dbo, row, "MEDICALGIVENDATE") m["comments"] = gks(row, "MEDICALCOMMENTS") m["singlemulti"] = "0" # single treatment m["status"] = "2" # completed try: medical.insert_regimen_from_form(dbo, user, utils.PostedData(m, dbo.locale)) except Exception as e: row_error(errors, "medical", rowno, row, e, dbo, sys.exc_info()) # License? if haslicence and personid != 0 and gks(row, "LICENSENUMBER") != "": l = {} l["person"] = str(personid) l["animal"] = str(animalid) l["type"] = gkl(dbo, row, "LICENSETYPE", "licencetype", "LicenceTypeName", createmissinglookups) if l["type"] == "0": l["type"] = 1 l["number"] = gks(row, "LICENSENUMBER") l["fee"] = str(gkc(row, "LICENSEFEE")) l["issuedate"] = gkd(dbo, row, "LICENSEISSUEDATE") l["expirydate"] = gkd(dbo, row, "LICENSEEXPIRESDATE") l["comments"] = gks(row, "LICENSECOMMENTS") try: financial.insert_licence_from_form(dbo, user, utils.PostedData(l, dbo.locale)) except Exception as e: row_error(errors, "license", rowno, row, e, dbo, sys.exc_info()) rowno += 1 h = [ "<p>%d success, %d errors</p><table>" % (len(data) - len(errors), len(errors)) ] for rowno, row, err in errors: h.append("<tr><td>%s</td><td>%s</td><td>%s</td></tr>" % (rowno, row, err)) h.append("</table>") return "".join(h)
def Home(): """Home page""" st.subheader('Have a question about veganism:question:') user_q = st.text_input(label = 'Ask me below and I\'ll try my best to answer!') ans = st.empty() inf = st.empty() st.markdown('\n\n') try: img = utils.get_image_url() caption = 'Photo by [' + img[1] + ']('+ img[2] + \ ''') on [Unsplash](https://www.unsplash.com) - randomly selected from the vegan collection''' st.image(img[0], use_column_width=True) st.markdown(caption) except: st.markdown(':confused: unsplash hourly API limit reached.' 'Come back later for more beautiful pictures!') df = utils.load_data() model = utils.load_model() st.markdown( """ *** # About ## What is VIRA? **VIRA** ('Vegan Information Retrieval Assistant') is a bot that tries to answer your *veganism related* questions! ## How does VIRA work? VIRA tries to match your question to its stored knowledge bank - and then return the most relevant response. VIRA is a 'closed domain' QA bot, and thus if the answer is not found in one of VIRA's corpora, she will have a hard time answering the question! ## Tell me more about VIRA's knowledge bank VIRA's knowledge bank was curated from various sources such as: reddit, wikipedia, bbc news, & vegan related websites. The set of questions & answers is quality controlled to ensure the responses accurately represent veganism! Currently VIRA's knowledge bank is small, and thus she won't always be able to assist. It is however being continually updated! ## Tell me more about VIRA's information retrieval algorithm VIRA utilises a 'Universal Sentence Encoder' algorithm. This approach was publicised by google in [this](https://arxiv.org/abs/1803.11175) paper. Compared to traditional methods such as word2vec or glove - which create word embeddings, Universal Sentence Encoder creates 512 dimensional embeddings at the sentence level. VIRA will compare the embeddings of your question, with those of the questions in her knowledge bank - and then if there is a close enough match - retrive the answer! Stay tuned for a full article on this approach, including what could be improved, and why this algorithm was used over others! *** [![github][1]][2] [![email][3]][4] ![version][5] [1]: https://img.shields.io/static/v1?label=github&message=VIRA&color=informational&logo=github [2]: https://www.github.com/ME-64/VIRA [3]: https://img.shields.io/static/v1?label=contact&[email protected]&color=green&logo=gmail [4]: mailto:[email protected] [5]: https://img.shields.io/static/v1?label=version&message=Protoype&color=red """ ) suggested_questions = ['Does it actually make a difference if I go vegan?', 'Do plants feel pain?', 'What do vegans think of palm oil?', 'Don\'t zoos help protect endangered species?', 'If you were stuck on a desert island with just meat, would you eat it?', 'What\'s wrong with milking cows?', 'Can fish really feel pain like other animals?', 'I\'m an athlete, can i go vegan?', 'Would you eat road kill?', 'Is tattoo ink vegan?', 'Is animal testing bad if it benefits humans?'] rand_questions = utils.select_random_questions(suggested_questions) if user_q == '': ans.info('Here\'s some questions to get you started!' + '\n - ' + \ rand_questions[0] + '\n- '+ \ rand_questions[1] + '\n- '+ \ rand_questions[2] + '\n- '+ \ rand_questions[3] ) elif len(user_q.split()) < 3: ans.info(':broken_heart: Your question must be more than 3 words' + \ '\n\n Maybe try this question - ' + random.choice(suggested_questions)) else: with st.spinner('Loading Answer'): prediction = utils.comp_q(user_q, df, model) if prediction[0] >= 0.5: know_bank = '\n\n *Associated question in my knowledge bank: *\n\n > ' + \ prediction[1] ans.success( ':mag: \n**Answer:**\n' + prediction[2].reset_index(drop=True)[0]) if prediction[0] <= 0.70: inf.info('I think this might answer your question! :neutral_face:' + \ know_bank) elif prediction[0] <= 0.85: inf.info('I\'m fairly confident this answers your question' ':grinning: :heavy_check_mark:' + know_bank) else: inf.info('This looks like it answers your question perfectly!' ':white_check_mark: :tada:' + know_bank) else: ans.warning(':disappointed: Sorry I\'m not confident I can answer' 'that correctly, please try another question')
def post(self, post_id_str, ip_num_str): post_id = int(post_id_str) main_post_key = models.Post.get_key(post_id) main_post = main_post_key.get() if main_post and main_post.parent_thread is None: errors = self._validate_form() if len(errors) > 0: delete_rpc = self._delete_files_async() child_posts_rpc = models.Post.get_child_posts_async(main_post_key) self.render_response("post_view.html", post_errors=errors, main_post=main_post, posts=child_posts_rpc.get_result()) delete_rpc.wait() else: ip_num = int(ip_num_str) ip = ipaddr.IPAddress(ip_num) ban_rpc = self.user_is_banned_async(ip) if not ban_rpc.get_result()[0]: uploads = self.get_uploads() if len(uploads) > 0: image_info = self.get_uploads()[0] image_key = image_info.key() delete_rpc = self._delete_files_async(image_info) if image_info.size > config.MAX_FILE_SIZE_BYTES: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) blobstore.delete(image_key) self.render_response("post_view.html", post_errors=['The supplied file was too large.'], main_post=main_post, posts=child_posts_rpc.get_result()) logging.info("File was too large") delete_rpc.wait() return data = blobstore.fetch_data(image_key, 0, 50000) try: image_data = images.Image(image_data=data) _ = image_data.width _ = image_data.height except images.NotImageError: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) blobstore.delete(image_key) self.render_response("post_view.html", post_errors=['The supplied file was not an image.'], main_post=main_post, posts=child_posts_rpc.get_result()) logging.info("Not an image", exc_info=True) delete_rpc.wait() return except images.Error: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) blobstore.delete(image_key) self.render_response("post_view.html", post_errors=['An unknown error occurred when we attempted to process the file you supplied.'], main_post=main_post, posts=child_posts_rpc.get_result()) logging.warn("Unknown error when processing image", exc_info=True) delete_rpc.wait() return else: image_data = None image_info = None image_key = None delete_rpc = None main_post_changed = False post_id_rpc = models.Post.get_next_id_async() current_user = users.get_current_user() current_user_admin = users.is_current_user_admin() post_id = post_id_rpc.get_result() post_key = models.Post.get_key(post_id) post = models.Post(key=post_key, ip=ip_num) post.parent_thread = main_post_key post.thread_bumped = None if image_key and image_data and image_info: post.image = image_key post.image_width = image_data.width post.image_height = image_data.height post.image_url = utils.get_image_url(image_info) post_as = self.request.POST.get('post-as', None) if current_user: post.user = current_user if post_as == 'anonymous': post.username = '******' else: if current_user_admin and post_as == 'admin': post.capcode = "Admin" else: post.capcode = "Logged in" post.username = current_user.nickname() if 'post-include-email' in self.request.POST: post.email = current_user.email() else: poster_name = self.request.POST.get('poster-name', '').strip() if len(poster_name) > 0: post.username = poster_name else: post.username = '******' if 'poster-email' in self.request.POST: post.email = self.request.POST['poster-email'] if 'dont-bump' not in self.request.POST: main_post.thread_bumped = datetime.now() main_post_changed = True if 'post-subject' in self.request.POST: post.subject = cgi.escape(self.request.POST['post-subject']) if 'post-comment' in self.request.POST: post.comment = utils.format_comment(self.request.POST['post-comment']) if main_post_changed: ndb.put_multi([main_post, post]) else: post.put() track_rpc = self.mixpanel_track_async("New thread", {"Post number": post_id, "mp_note": "User made post #%s." % post_id}) self.redirect('/post/%s#%s' % (main_post_key.id(), post_id)) if delete_rpc: ndb.Future.wait_all([delete_rpc, track_rpc]) else: track_rpc.wait() else: child_posts_rpc = models.Post.get_child_posts_async(main_post_key) delete_rpc = self._delete_files_async() self.render_response("post_view.html", main_post=main_post, posts=child_posts_rpc.get_result()) delete_rpc.wait() else: delete_rpc = self._delete_files_async() self.abort(404) delete_rpc.wait()
def post(self, ip_num_str): errors = self._validate_form() if len(errors) > 0: index_threads_rpc = models.Post.get_index_page_async() delete_rpc = self._delete_files_async() self.render_response("index.html", post_errors=errors, threads=index_threads_rpc.get_result()) delete_rpc.wait() else: ip_num = int(ip_num_str) ip = ipaddr.IPAddress(ip_num) ban_rpc = self.user_is_banned_async(ip) if not ban_rpc.get_result()[0]: image_info = self.get_uploads()[0] image_key = image_info.key() delete_rpc = self._delete_files_async(image_info) if image_info.size > config.MAX_FILE_SIZE_BYTES: index_threads_rpc = models.Post.get_index_page_async() blobstore.delete(image_key) delete_rpc.wait() self.render_response("index.html", post_errors=['The supplied file was too large.'], threads=index_threads_rpc.get_result()) logging.info("File was too large") return data = blobstore.fetch_data(image_key, 0, 50000) try: image_data = images.Image(image_data=data) _ = image_data.width _ = image_data.height except images.NotImageError: index_threads_rpc = models.Post.get_index_page_async() blobstore.delete(image_key) delete_rpc.wait() self.render_response("index.html", post_errors=['The supplied file was not an image.'], threads=index_threads_rpc.get_result()) logging.info("Not an image", exc_info=True) return except images.Error: index_threads_rpc = models.Post.get_index_page_async() blobstore.delete(image_key) delete_rpc.wait() self.render_response("index.html", post_errors=['An unknown error occurred when we attempted to process the file you supplied.'], threads=index_threads_rpc.get_result()) logging.warn("Unknown error when processing image", exc_info=True) return post_id_rpc = models.Post.get_next_id_async() current_user = users.get_current_user() current_user_admin = users.is_current_user_admin() post_id = post_id_rpc.get_result() post_key = models.Post.get_key(post_id) post = models.Post(key=post_key, ip=ip_num) post.image = image_key post.image_width = image_data.width post.image_height = image_data.height post.image_url = utils.get_image_url(image_info) post_as = self.request.POST.get('post-as', None) if current_user: post.user = current_user if post_as == 'anonymous': post.username = '******' else: if current_user_admin and post_as == 'admin': post.capcode = "Admin" else: post.capcode = "Logged in" post.username = current_user.nickname() if 'post-include-email' in self.request.POST: post.email = current_user.email() else: poster_name = self.request.POST.get('poster-name', '').strip() if len(poster_name) > 0: post.username = self.request.POST['poster-name'] else: post.username = '******' if 'poster-email' in self.request.POST: post.email = self.request.POST['poster-email'] if 'post-subject' in self.request.POST: post.subject = self.request.POST['post-subject'] if 'post-comment' in self.request.POST: post.comment = utils.format_comment(self.request.POST['post-comment']) post.put() track_rpc = self.mixpanel_track_async("New thread", {"Post number": post_id, "mp_note": "User made thread #%s." % post_id}) self.redirect('/post/%s#%s' % (post_id, post_id)) ndb.Future.wait_all([delete_rpc, track_rpc]) else: index_threads_rpc = models.Post.get_index_page_async() delete_rpc = self._delete_files_async() logging.info("Banned user attempted to post") self.render_response("index.html", threads=index_threads_rpc.get_result()) delete_rpc.wait()
print("English Hashtags are extracted") hashtags = utils.generate_hashtags(english_hashtags, path=hashtags_db_path) print("All hashtags are generated") # 3. Pick Sentences (picking 2 captions by default) sentences = utils.pick_sentences(sentences_db_path) print("Sentences in caption are generated") # 4. Generate caption caption = utils.generate_caption(author_name, sentences, hashtags, caption_template_path) print("Caption is generated") with open("Mydebug.txt", "a") as f: f.write(caption + "\n" + utils.get_image_url(df.loc[row_num, "image_id"])) # Login bot = instabot.Bot() bot.login(username=my_account_name, password=my_account_password) print("Successfully log in") # Pause a while time.sleep(random.randint(5, 15)) # Update Image DB: Change Image Availability utils.update_image_availability(cnxn, image_id) print(f"Id: Availability of Image ID: {row_num} is updated ") # Post bot.upload_photo(downloaded_photo_path, caption)