def unsubscribe(email_address): try: unsub_email = Email.query.filter_by(email_address=email_address).first() db.session.delete(unsub_email) db.session.commit() return 'Succesfully unsubscribed ' + strip_html(email_address) except Exception as e: app.logger.error(e) return 'Something went wrong trying to unsubscribe ' + strip_html(email_address) \ + '. Try again later.'
def send_email(sender, instance, **kwargs): comment = instance if settings.ENABLE_EMAIL and comment.visible == True: t = loader.get_template("blog/coolblue/email.tpl") type = 0 if comment.content_type.model == 'article' else 1 c = Context({ 'type': type, 'comment': comment, 'site': settings.SITE }) html = t.render(c) plain_text = strip_html(html) subject = '' to_email = '' if not comment.reply_to_comment: to_email = [settings.ADMINS[0][1], ] if type == 0: subject = u'【残阳似血的博客】上的文章刚刚被%s评论了' % comment.username else: subject = u'【残阳似血的博客】刚刚收到%s的留言' % comment.username else: to_email = [comment.reply_to_comment.email_address, ] if type == 0: subject = u'您在【残阳似血的博客】上的评论刚刚被%s回复了' % comment.username else: subject = u'您在【残阳似血的博客】上的留言刚刚被%s回复了' % comment.username from_email = settings.EMAIL_HOST_USER send_mail(subject, plain_text, from_email, to_email, html=html)
def send_subscribers_email(sender, instance, **kwargs): article = instance if settings.ENABLE_EMAIL: subject = u'您订阅的【残阳似血的博客】上的文章有了更新' from_email = settings.EMAIL_HOST_USER t = loader.get_template("blog/coolblue/subscriberemail.tpl") for subscriber in Subscriber.enabled_objects.all(): if ArticleSubscriber.objects.filter(article=article, subscriber=subscriber).count() == 0: article_subscriber = ArticleSubscriber(article=article, subscriber=subscriber) article_subscriber.save() to_email = [subscriber.email_address, ] c = Context({ 'article': article, 'subscriber': subscriber, 'parameter': urllib.urlencode({ 'email': to_email[0], 'hv': hashlib.md5("%s%s"%(to_email[0], str(subscriber.subscribe_time))).hexdigest() }), 'site': settings.SITE }) html = t.render(c) plain_text = strip_html(html) send_mail(subject, plain_text, from_email, to_email, html=html)
def get_sync_content(article, client, social_type): # Title title = article.title title_count = sina_client.count_words(title) # Short url url = '%s%s' % (settings.SITE, article.get_absolute_url()) if hasattr(client, 'get_short_url'): short_url = client.get_short_url(url) else: short_url = url short_url_count = sina_client.count_words(short_url) # Base content base = u"发表了文章《》 - ... " base_count = sina_client.count_words(base) rest_count = MAX_WORDS - title_count - short_url_count - base_count rest_content = '' for word in iter(strip_html(article.content)): rest_count -= sina_client.count_words(word) threshold = 0 if not social_type==2 else 3 if rest_count < threshold: break rest_content += word content = u"发表了文章《%s》 - %s... %s" % (title, rest_content, short_url) return content
def spiel(self, id, code=None): msg = self.spiels[id] if code != None and True: # make this configurable msg = '%s (%s)' % (msg, code) if not request.is_jquery: msg = utils.strip_html(msg) return msg.replace('#URL', 'https://%s' % (request.source['portal_ip']))
def post(self): method = self.request.get("_method") key = utils.url_to_entity_key(self.request.uri) if key: playlist = db.get(db.Key(key)) if playlist: current_user = utils.get_current_user() #Get corresponding link library_item = playlist.library_item_for_user(current_user) if method == "PUT": need_version_control = False playlist_changed = False if(self.request.get('position')): #Rights: Can always update this current_user.re_sort_playlists(library_item, int(self.request.get('position'))) if (playlist.collaborative or library_item.is_owner): #Rights: Owner or collaborators can update this if playlist.smart: utils.parse_smart_filters(playlist, self.request) playlist_changed = True if self.request.get('tracks'): playlist.tracks = self.request.get('tracks') playlist_changed = True need_version_control = playlist.collaborative if library_item.is_owner: #Rights: Only owner can update this if(self.request.get('name') and len(self.request.get('name')) > 0): playlist.name = utils.strip_html(self.request.get('name')) playlist_changed = True if(self.request.get('collaborative')): playlist.collaborative = utils.convert_javascript_bool_to_python(self.request.get('collaborative')) playlist_changed = True if playlist_changed: if need_version_control: if self.request.get('version') and not int(self.request.get('version')) < playlist.version: playlist.version += 1 playlist.put() self.response.out.write(utils.status_code_json(200)) else: self.response.out.write(library_item.serialize()) else: playlist.put() self.response.out.write(utils.status_code_json(200)) else: self.response.out.write(utils.status_code_json(200)) elif method == "DELETE": library_item.delete() current_user.re_index_playlists() if not playlist.has_user(): playlist.delete() self.response.out.write(utils.status_code_json(200))
def _update_ippsec(self): self.ipsc = { "url": "https://ippsec.rocks/dataset.json", "count": 0, "entries": {} } entries = utils.download_json(self.ipsc["url"]) for entry in entries: if not entry.get("videoId", None): continue name = utils.strip_html(entry["machine"]) video_url = "https://www.youtube.com/watch?v=%s&t=0" % (entry["videoId"]) hours = int(entry["timestamp"]["minutes"] // 60); minutes = entry["timestamp"]["minutes"] - (hours*60) timestamp = "%02d:%02d:%02d" % (hours, minutes, entry["timestamp"]["seconds"]) totalseconds = (entry["timestamp"]["minutes"]*60) + entry["timestamp"]["seconds"] tsurl = "https://www.youtube.com/watch?v=%s&t=%ds" % (entry["videoId"], totalseconds) desc = "%s - %s" % (timestamp, utils.strip_html(entry["line"])) if name not in self.ipsc["entries"]: self.ipsc["count"] += 1 self.ipsc["entries"][name] = { "video_url": video_url, "description": { desc: tsurl } } else: self.ipsc["entries"][name]["description"][desc] = tsurl if len(self.ipsc["entries"]): for machine in self.stats["machines"]: if self.ipsc and self.ipsc.get("entries") and self.ipsc["entries"]: for entry in self.ipsc["entries"]: if machine["infrastructure"] in entry.lower().strip() and machine["name"].lower().strip() in entry.lower().strip(): machine["writeups"] = { "ippsec": { "name": entry, "video_url": self.ipsc["entries"][entry]["video_url"], "description": self.ipsc["entries"][entry]["description"], } } utils.info("[update.ippsec] added %d machine writeup descriptions from ippsec" % (self.ipsc["count"]))
def check_req(self, username): from main import API_BASE_URL # Connects to the API and checks if everything is good to go try: res = requests.get(API_BASE_URL + "check/", params={"user": username}) except ConnectionError as e: return False, f"Connection error: {e}", "Connection error." if res.status_code == 200: data = res.json() return data['linked'], "", "" content = strip_html(res.content.decode("utf-8")) return False, f"HTTP error {res.status_code} - {content}", "Connection error."
def start_req(self, link_code): from main import API_BASE_URL try: res = requests.get(API_BASE_URL + "start/", params={"link_code": link_code}) except ConnectionError as e: return False, f"Connection error: {e}", "Connection error." if res.status_code == 200: data = res.json() if data.get("error", False): return False, data['msg'], data['short_msg'] else: return True, "", "" content = strip_html(res.content.decode("utf-8")) return False, f"HTTP error {res.status_code} - {content}", "Connection error."
def get(self): entries = query.get_fresh_entries(self.db, 3) new = self.get_arguments("new", None) for entry in entries: entry["summary"] = utils.strip_html( markdown.markdown(entry["markdown"]) ) #show abstract only if len(entry["summary"]) > 300: entry["summary"] = entry["summary"][:300] entry["date"] = entry["published"] user = self.get_current_user() args = locals() args.pop('self') self.render("index.html", **args)
def renren_share(sender, instance, **kwargs): article = instance if settings.ENABLE_RENREN_ACCOUNT \ and article.status == 2 \ and article.share \ and article.shares.filter(type=3).count() == 0: content = u'残阳似血的博客发表了文章《%s》 - %s' % (article.title, strip_html(article.content[:100]+"...")) content = content.encode('utf-8') renren_share = renren_client('share.share', url='%s%s' % (settings.SITE, article.get_absolute_url()), comment=content, type=6) if 'error_code' in renren_share and renren_share['error_code'] != 0: return share_id = renren_share['id'] # 6 in renren api means this is a url content_type = ContentType.objects.get(model='article') share = SocialItem(share_id=share_id, content_type=content_type, type=3, object_id=article.id) share.save()
def _parse_machine_page(self, url): if not url: return match = re.search(r'/entry/(.+),(\d+)/', url) if not match: return shortname, mid, url = match.groups()[0], int( match.groups()[1]), "https://www.vulnhub.com/entry/%s,%s/" % ( match.groups()[0], match.groups()[1]) machinestats = { "avatar_thumb": None, "id": mid, "maker": { "id": None, "name": None, "url": None, }, "series": { "id": None, "name": None, "url": None, }, "name": None, "shortname": shortname, "os": None, "points": None, "difficulty": None, "description": None, "release": None, "url": url, } res = utils.get_http_res(url) page = res.text soup = BeautifulSoup(page, 'lxml') container = soup.find('div', id="description", class_="panel") if not container: return machinestats machinestats["description"] = "\n".join([ para.text for para in container.find_all('p', recursive=True) ]).strip() # <li><b>Name</b>: Seppuku: 1</li> match = re.search(r'<li><b>Name</b>:\s*(.+)</li>', page, re.I) machinestats["name"] = utils.strip_html( match.groups()[0]) if match else None # <a href="/media/img/entry/ match = re.search(r'<a href="/media/img/entry/([^"]+)', page, re.I) machinestats[ "avatar_thumb"] = "https://www.vulnhub.com/media/img/entry/%s" % ( utils.strip_html(match.groups()[0])) if match else None # <li><b>Date release</b>: 13 May 2020</li> match = re.search(r'<li><b>Date release</b>:\s*(.+)</li>', page, re.I) machinestats["release"] = utils.strip_html( match.groups()[0]) if match else None # <li><b>Author</b>: <a href="/author/suncsr-team,696/">SunCSR Team</a></li> match = re.search( r'<li><b>Author</b>:\s*<a href="/author/(.+),(\d+)/">(.+)</a></li>', page, re.I) if match: machinestats["maker"]["name"] = utils.strip_html(match.groups()[2]) machinestats["maker"]["id"] = int( utils.strip_html(match.groups()[1])) machinestats["maker"][ "url"] = "https://www.vulnhub.com/author/%s,%s/" % ( utils.strip_html(match.groups()[0]), utils.strip_html(match.groups()[1])) # <li><b>Series</b>: <a href="/series/seppuku,318/">Seppuku</a></li> match = re.search( r'<li><b>Series</b>:\s*<a href="/series/(.+),(\d+)/">(.+)</a></li>', page, re.I) if match: machinestats["series"]["name"] = utils.strip_html( match.groups()[2]) machinestats["series"]["id"] = int( utils.strip_html(match.groups()[1])) machinestats["series"][ "url"] = "https://www.vulnhub.com/series/%s,%s/" % ( utils.strip_html(match.groups()[0]), utils.strip_html(match.groups()[1])) # <li><b>Operating System</b>: Linux</li> match = re.search(r'<li><b>Operating System</b>:\s*(.+)</li>', page, re.I) machinestats["os"] = utils.strip_html( match.groups()[0]) if match else None if machinestats["url"] in self.difficulty["easy"]: machinestats["difficulty"] = "easy" elif machinestats["url"] in self.difficulty["medium"]: machinestats["difficulty"] = "medium" elif machinestats["url"] in self.difficulty["hard"]: machinestats["difficulty"] = "hard" elif machinestats["url"] in self.difficulty["insane"]: machinestats["difficulty"] = "insane" if not machinestats["difficulty"]: # <li>Difficulty: Intermediate to Hard</li> # <li>Difficulty: <strong>Beginner to Intermediate</strong></li> match = re.search( r'<li>Difficulty\s*:\s*(<strong>)?(.+)(</strong>)?</li>', page, re.I) machinestats["difficulty"] = utils.strip_html( match.groups()[1]) if match else None if not machinestats["difficulty"]: # <p>Difficulty is Medium</p> # <p>Difficulty: Intermediate # <p>Difficulty : beginner/intermediate</p> # <p>Level : beginner for user flag and intermediate for root flag.</p> match = re.search(r'<p>(Difficulty|Level)\s*(is|:)\s*(.+)</p>', page, re.I) machinestats["difficulty"] = utils.strip_html( match.groups()[2]) if match else None if not machinestats["difficulty"]: # An easy to intermediate boot2root match = re.search(r'easy\s+(to|/)\s+intermediate', page, re.I) machinestats["difficulty"] = "Easy/Intermediate" if match else None if not machinestats["difficulty"]: # beginner boot2root match = re.search( r'(beginner\s+boot2root|A piece of cake machine)', page, re.I) machinestats["difficulty"] = "Easy" if match else None if not machinestats["difficulty"]: # medium level difficulty match = re.search(r'(medium\s+level\s+difficulty)', page, re.I) machinestats["difficulty"] = "Medium" if match else None if not machinestats["difficulty"]: # <p>[BEGINNER - INTERMEDIATE]</p> # beginner to intermediate boot2root match = re.search(r'BEGINNER\s*(-|to)?\s*INTERMEDIATE', page, re.I) machinestats[ "difficulty"] = "Beginner/Intermediate" if match else None if machinestats["difficulty"]: mapping = { "insane": ["insane"], "hard": ["hard", "difficult", "challenging", "intermediate++"], "medium": ["medium", "intermediate", "moderate", "beginner++"], "easy": ["easy", "beginner", "low"], } newdiff = None for normdiff in mapping["insane"]: if normdiff in machinestats["difficulty"].lower().strip(): newdiff = "insane" break if not newdiff: for normdiff in mapping["hard"]: if normdiff in machinestats["difficulty"].lower().strip(): newdiff = "hard" break if not newdiff: for normdiff in mapping["medium"]: if normdiff in machinestats["difficulty"].lower().strip(): newdiff = "medium" break if not newdiff: for normdiff in mapping["easy"]: if normdiff in machinestats["difficulty"].lower().strip(): newdiff = "easy" break machinestats["difficulty"] = newdiff if newdiff else None machinestats["points"] = None return machinestats
def load(self, id, ext_kw = True): article = self.storage.load(id) if not article: return None if ext_kw: self._extend_keywords(article) article.text_content = utils.strip_html(article.content) if article.content else '' return article
def get(self): """ /api/ -- all items all data ?st= -- State Name ?city= -- city """ results = [] state = self.request.get('st', default_value=None) city = self.request.get('city', default_value=None) if state is not None or city is not None: # lets look up addresses first then grab relevant entries query = Address.all(keys_only=True) if state is not None: query.filter('state =', cgi.escape(state)) if city is not None: query.filter('city =', cgi.escape(city)) # at this point we have our addresses for addrs in query: e_query = Entry.all().filter('address =', addrs) for result in e_query: entry = {} entry['address'] = cgi.escape(strip_html(get_as_str(result.address))) if result.address else None entry['time'] = cgi.escape(strip_html(str(result.time))) if result.time else None entry['lat'] = result.address.geo_pt.lat entry['lng'] = result.address.geo_pt.lon entry['power_status'] = cgi.escape(strip_html(result.power_status)) if result.power_status else None entry['landline_status'] = cgi.escape(strip_html(result.landline_status)) if result.landline_status else None entry['water_status'] = cgi.escape(strip_html(result.water_status)) if result.water_status else None entry['road_status'] = cgi.escape(strip_html(result.road_status)) if result.road_status else None entry['rain_fall'] = cgi.escape(strip_html(result.rain_fall)) if result.rain_fall else None entry['visual_report'] = cgi.escape(strip_html(result.visual_report)) if result.visual_report else None results.append(entry) else: query = Entry.all() for result in query: entry = {} entry['address'] = cgi.escape(strip_html(get_as_str(result.address))) if result.address else None entry['time'] = cgi.escape(strip_html(str(result.time))) if result.time else None entry['lat'] = result.address.geo_pt.lat entry['lng'] = result.address.geo_pt.lon entry['power_status'] = cgi.escape(strip_html(result.power_status)) if result.power_status else None entry['landline_status'] = cgi.escape(strip_html(result.landline_status)) if result.landline_status else None entry['water_status'] = cgi.escape(strip_html(result.water_status)) if result.water_status else None entry['road_status'] = cgi.escape(strip_html(result.road_status)) if result.road_status else None entry['rain_fall'] = cgi.escape(strip_html(result.rain_fall)) if result.rain_fall else None entry['visual_report'] = cgi.escape(strip_html(result.visual_report)) if result.visual_report else None results.append(entry) self.response.headers['content-type'] = 'application/json' self.response.write(simplejson.dumps(results))
def post(self): method = self.request.get("_method") key = utils.url_to_entity_key(self.request.uri) if key: playlist = db.get(db.Key(key)) if playlist: current_user = utils.get_current_user() #Get corresponding link library_item = playlist.library_item_for_user(current_user) if method == "PUT": need_version_control = False playlist_changed = False if (self.request.get('position') ): #Rights: Can always update this current_user.re_sort_playlists( library_item, int(self.request.get('position'))) if (playlist.collaborative or library_item.is_owner ): #Rights: Owner or collaborators can update this if playlist.smart: utils.parse_smart_filters(playlist, self.request) playlist_changed = True if self.request.get('tracks'): playlist.tracks = self.request.get('tracks') playlist_changed = True need_version_control = playlist.collaborative if library_item.is_owner: #Rights: Only owner can update this if (self.request.get('name') and len(self.request.get('name')) > 0): playlist.name = utils.strip_html( self.request.get('name')) playlist_changed = True if (self.request.get('collaborative')): playlist.collaborative = utils.convert_javascript_bool_to_python( self.request.get('collaborative')) playlist_changed = True if playlist_changed: if need_version_control: if self.request.get('version') and not int( self.request.get( 'version')) < playlist.version: playlist.version += 1 playlist.put() self.response.out.write( utils.status_code_json(200)) else: self.response.out.write( library_item.serialize()) else: playlist.put() self.response.out.write( utils.status_code_json(200)) else: self.response.out.write(utils.status_code_json(200)) elif method == "DELETE": library_item.delete() current_user.re_index_playlists() if not playlist.has_user(): playlist.delete() self.response.out.write(utils.status_code_json(200))
def test_strip_html(): """ test suppression des balises HTML """ assert utils.strip_html("<div><br>Hello</div>") == "Hello"
def spiel(self, id): msg = self.spiels[id] if not request.is_jquery: msg = utils.strip_html(msg) return msg.replace('#URL', 'https://%s' % (request.source['portal_ip']))