diff options
Diffstat (limited to 'app/utils/util.py')
-rw-r--r-- | app/utils/util.py | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/app/utils/util.py b/app/utils/util.py new file mode 100644 index 0000000..dc04b0b --- /dev/null +++ b/app/utils/util.py @@ -0,0 +1,147 @@ +import re +from django.contrib.gis.geos import GEOSGeometry +from django.apps import apps +from django.template.loader import render_to_string +from django.conf import settings +from bs4 import BeautifulSoup +import markdown + + +def markdown_to_html(txt): + md = markdown.Markdown( + extensions=[ + 'markdown.extensions.fenced_code', + 'markdown.extensions.codehilite', + 'markdown.extensions.attr_list', + 'footnotes', + 'extra' + ], + extension_configs = { + 'markdown.extensions.codehilite': { + 'css_class': 'highlight', + 'linenums': False + }, + }, + output_format='html5', + safe_mode=False + ) + return md.convert(txt) + + +def convertll(lat, lon): + pnt = GEOSGeometry('POINT({0} {1})'.format(lon, lat), srid=4326) + pnt.transform(3857) + return pnt.y, pnt.x + + +def get_latlon(): + loc = apps.get_model('locations', 'LuxCheckIn').objects.latest() + lat_converted, lon_converted = convertll(loc.lat, loc.lon) + return lat_converted, lon_converted + + +def extract_main_image(markdown): + soup = BeautifulSoup(markdown, 'html.parser') + try: + image = soup.find_all('img')[0]['id'] + img_pk = image.split('image-')[1] + return apps.get_model('photos', 'LuxImage').objects.get(pk=img_pk) + except IndexError: + return None + + +def parse_products(s): + soup = BeautifulSoup(s.group(), "lxml") + for div in soup.find_all('div'): + try: + p = apps.get_model('products', 'Product').objects.get(pk=int(div['id'].split("product-")[1])) + return render_to_string("products/snippet.html", {'object': p}) + except KeyError: + return str(s) + + +def parse_image(s): + soup = BeautifulSoup(s.group(), "lxml") + for img in soup.find_all('img'): + try: + cl = img['class'] + #if cl[0] == 'postpic' or cl[0] == 'postpicright': + replacer = "[[base_url]]" + if replacer in str(img): + s = str(img).replace('[[base_url]]', settings.IMAGES_URL) + #print(s) + return s + else: + try: + image_id = img['id'].split("image-")[1] + i = apps.get_model('photos', 'LuxImage').objects.get(pk=image_id) + caption = False + exif = False + cluster_class = None + is_cluster = False + extra = None + if cl[0] == 'cluster': + css_class = cl[0] + is_cluster = True + cluster_class = cl[1] + try: + if cl[2] == 'caption': + caption = True + elif cl[2] == 'exif': + exif = True + else: + extra = cl[2] + + if len(cl) > 3: + if cl[3] == 'exif': + exif = True + except: + pass + elif cl[0] != 'cluster' and len(cl) > 1: + css_class = cl[0] + if cl[1] == 'caption': + caption = True + if cl[1] == 'exif': + exif = True + elif cl[0] != 'cluster' and len(cl) > 2: + css_class = cl[0] + if cl[1] == 'caption': + caption = True + if cl[2] == 'exif': + exif = True + print('caption'+str(caption)) + else: + css_class = cl[0] + return render_to_string("lib/img_%s.html" % css_class, {'image': i, 'caption': caption, 'exif': exif, 'is_cluster': is_cluster, 'cluster_class': cluster_class, 'extra': extra}) + except KeyError: + ''' regular inline image, not a luximage ''' + return str(img) + except KeyError: + ''' regular inline image, not a luximage ''' + return str(img) + + +def render_images(s): + s = re.sub('<img(.*)/>', parse_image, s) + return s + +def render_products(s): + s = re.sub('<div(.*)</div>', parse_products, s) + return s + +def parse_video(s): + soup = BeautifulSoup(s, "lxml") + if soup.find('video'): + return True + return False + +def parse_reg_bio_page(): + content = requests.get("https://www.theregister.co.uk/Author/Scott-Gilbertson/") + soup = BeautifulSoup(content, 'html.parser') + try: + image = soup.find_all('img')[0]['id'] + img_pk = image.split('image-')[1] + return apps.get_model('photos', 'LuxImage').objects.get(pk=img_pk) + except IndexError: + return None + |