def get_youtube_id(inputString): try: result = getYouTubeIdIfPresent(inputString) if result is False: return "" else: return result except: return ""
def linkCategorizer(citeURL, citeMIME): from enterlink.media_functions import getYouTubeIdIfPresent # Get the file extension name, extension = os.path.splitext(citeURL) extension = extension.lower() # Determine the category if citeMIME == "" or citeMIME is None: citeCategory = "NONE" if citeMIME == 'image/gif': citeCategory = "GIF" elif 'image' in citeMIME: citeCategory = "PICTURE" elif getYouTubeIdIfPresent(citeURL): citeCategory = "YOUTUBE" elif extension in VALID_VIDEO_EXTENSIONS: citeCategory = "NORMAL_VIDEO" elif extension in VALID_AUDIO_EXTENSIONS: citeCategory = "AUDIO" else: citeCategory = "NONE" return citeCategory
def AJAX_Add_New_Link(request): from enterlink.media_functions import getYouTubeIdIfPresent # Get the POST variables pageSlug = request.POST['pageSlug'] option = request.POST['group1'] citeHTML = request.POST['citeHTML'] URLComment = request.POST['nl_linkcomment'] # Make sure an empty description was not provided placeholderPresent = any(placeholder in URLComment for placeholder in PLACEHOLDER_LIST) if (placeholderPresent or URLComment.strip() == "" or URLComment is None or URLComment == "<br data-mce-bogus=\"1\">"): return HttpResponse("ERROR_NO_DESCRIPTION") else: pass URLComment = badLinkSanitizer(URLComment) # Get and format the UTC timestamp timestamp = datetime.datetime.utcnow() timestamp = pytz.utc.localize(timestamp).strftime( '%m/%d/%Y %I:%M:%S %p %Z') # Get the article object from the URL slug provided cleanedParamList = getTheArticleObject(pageSlug) pageSlug = cleanedParamList[0] articleObject = cleanedParamList[1] # Parse all the current citations for the article. # This will be used later for things such as finding duplicates and getting the citation number theSoup = BeautifulSoup(citeHTML, "html5lib") resultDictionary = {} parseTinyMCE_Citations(theSoup, resultDictionary) # Set up a blank dictionary for the new link newLinkDict = { "url": None, "thumb": None, "description": URLComment, "category": "NONE", "integer": None, "isSocial": False, "attr": None, "timestamp": timestamp, "mime": None, "attribution_url": None } # Create a list of all current citations citationList, urlList = [], [] try: for citation in resultDictionary["CITATION_OBJECTS"]: citationList.append(int(citation["integer"])) urlList.append(citation["url"]) except: pass # Calculate and set the citation number for the new link if len(citationList) == 0: citationInteger = 1 else: try: citationInteger = max(citationList) + 1 except: citationInteger = None newLinkDict["integer"] = citationInteger # Check if the new link is a file if option == 'id_file': # Get and set some variables theFile = request.FILES['file'] # Add the file to the Amazon S3 bucket and get some information about it resultPack = addMediaImage(request=request, pageID=pageSlug, theFile=theFile, fileComment=URLComment, PLACEHOLDER_LIST=PLACEHOLDER_LIST, inputMime="EMPTY") # Update the new link dictionary with information about the file newLinkDict["url"] = resultPack["url"] newLinkDict["thumb"] = resultPack["thumb"] newLinkDict["mime"] = resultPack["mime"] newLinkDict["category"] = linkCategorizer(resultPack["url"], resultPack["mime"]) # Check for duplicate links if dupeLinkDetector(newLinkDict["url"], urlList): return HttpResponse("ERROR_ALREADY_EXISTS") # Decide where to put the new link. If it is an image, video, or YouTube link, put it in the media gallery. # Otherwise, put it in the normal citation list. if newLinkDict["category"] == "NONE": newLinkHTMLBlock = render_to_string( 'enterlink/ajax_link_singlet.html', {'theLink': newLinkDict}) return JsonResponse({ "type": "NORMAL", "htmlblock": newLinkHTMLBlock }) else: newLinkHTMLBlock = render_to_string( 'enterlink/ajax_media_gallery_singlet.html', {'theLink': newLinkDict}) return JsonResponse({ "type": "MEDIA", "htmlblock": newLinkHTMLBlock }) # Check if the new link is a URL elif option == 'id_linkurl': # Get the URL and check if it is a Wikipedia link. if request.POST['linkurl']: theURL = request.POST['linkurl'] if ('wikipedia.org' in theURL): return HttpResponse("ERROR_WIKIPEDIA_LINK") else: pass else: return HttpResponse("ERROR_NO_URL") # Check for a duplicate link if dupeLinkDetector(theURL, urlList): return HttpResponse("ERROR_ALREADY_EXISTS") # Update the new link JSON data newLinkDict["url"] = theURL newLinkDict["isSocial"] = profileLinkTester(theURL)["isProfileLink"] # Check if the URL is a link to a media object isMedia = False try: # Ping the URL to see get the data and response headers mediaTest = requests.get(theURL, headers=REQUEST_HEADER, timeout=6, verify=False, stream=True) # See if the response header indicates it is a media item. If so, set isMedia to True listOfMedias = ["video", "image", "audio"] for mediaItem in listOfMedias: mediaName = u"%s/" % mediaItem if mediaName in mediaTest.headers['Content-Type']: isMedia = True break # Check for YouTube youtubeID = getYouTubeIdIfPresent(theURL) if youtubeID: isMedia = True # Analyze and process the URL if it is a media file if isMedia: # Process depending on media type if youtubeID: newLinkDict[ "thumb"] = "https://i.ytimg.com/vi/%s/hqdefault.jpg" % youtubeID newLinkDict["mime"] = "youtube" newLinkDict["category"] = "YOUTUBE" else: # Create an empty string buffer and fill it with the media file data streamObject = StringIO.StringIO() streamObject.write( mediaTest.raw.read(decode_content=False)) # Create a file in memory from the media file theFile = InMemoryUploadedFile( streamObject, None, theURL.split("?")[0].split("/")[-1], mediaTest.headers['Content-Type'], streamObject.len, None) # Add the media file to the Amazon S3 bucket and analyze the media file resultPack = addMediaImage( request=request, pageID=pageSlug, theFile=theFile, fileComment=URLComment, PLACEHOLDER_LIST=PLACEHOLDER_LIST, inputMime=mediaTest.headers['Content-Type']) # Update the new link dictionary with info about the file newLinkDict["thumb"] = resultPack["thumb"] newLinkDict["mime"] = resultPack["mime"] newLinkDict["category"] = linkCategorizer( resultPack["url"], resultPack["mime"]) # Render the new media object as an HTML block newLinkHTMLBlock = render_to_string( 'enterlink/ajax_media_gallery_singlet.html', {'theLink': newLinkDict}) return JsonResponse({ "type": "MEDIA", "htmlblock": newLinkHTMLBlock }) except Exception as e: print(str(e)) print( "Is not media, or timeout. Considering link as normal url...") # Find the thumbnail for the new link web page, if it has one from the og:image, schema, etc. theThumbURL = fetchMetaThumbnail(request, theURL, articleObject.slug, articleObject.ipfs_hash_current[:10]) newLinkDict["thumb"] = theThumbURL # Render the new link as an HTML block newLinkHTMLBlock = render_to_string('enterlink/ajax_link_singlet.html', {'theLink': newLinkDict}) return JsonResponse({"type": "NORMAL", "htmlblock": newLinkHTMLBlock})
def AJAX_Search(request, url_param): FETCH_LIMIT = 20 resultDictionary = {} # See if the search request is for a citation # If so, create a list of citations in the article and construct the citation HTML superscript block (e.g. [24]) # that is to be injected into the article if url_param == 'tinymce-cite-source': try: cited_by = request.GET.get('cited_by') citer_rank = request.GET.get('citer_rank') citer_is_verified = request.GET.get('citer_is_verified') except: cited_by = "" citer_rank = "" citer_is_verified = "" # Get the POST variables slug = request.POST['slug'] htmlBlock = request.POST['htmlblock'] # Create the BeautifulSoup object theSoup = BeautifulSoup(htmlBlock, "html5lib") # Parse the citations from the HTML and add them to the context dictionary parseTinyMCE_Citations(theSoup, resultDictionary) # Create the citation HTML (e.g. [26]) resultArray = [] for linkNugget in resultDictionary["CITATION_OBJECTS"]: stringToRespond = u"<span class='tooltip-wrap'><a class='tooltippableCarat' rel='nofollow'" stringToRespond = stringToRespond + u" href='/wiki/" + unicode(slug) + u"/" stringToRespond = stringToRespond + u"' data-username='******' data-cited_by='" + unicode(cited_by) + \ u"' data-cited_rank='" + unicode(citer_rank) + \ u"' data-citer_is_verified='" + unicode(citer_is_verified) + \ u"' ><sup>" stringToRespond = stringToRespond + u"[" + unicode(linkNugget["integer"]) + u"]" stringToRespond = stringToRespond + u"</sup></a></span>​" linkNugget["tooltipHTML"] = stringToRespond resultArray.append(linkNugget) # Return the list of citations on the page return render(request, 'enterlink/ajaxresults.html', {"result": resultArray, "renderType": url_param, "cited_by": cited_by, "citer_rank": citer_rank, "citer_is_verified": citer_is_verified}) # Generate a list of the photos if url_param == 'tinymce-cite-picture': try: cited_by = request.GET.get('cited_by') citer_rank = request.GET.get('citer_rank') citer_is_verified = request.GET.get('citer_is_verified') except: cited_by = "" citer_rank = "" citer_is_verified = "" # Get the POST variables slug = request.POST['slug'] htmlBlock = request.POST['htmlblock'] # Create the BeautifulSoup object theSoup = BeautifulSoup(htmlBlock, "html5lib") # Parse the media gallery objects from the HTML and add them to the context dictionary parseTinyMCE_Media(theSoup, resultDictionary) # Create the in-article image container. mediaNugget, resultArray = [], [] for linkNugget in resultDictionary["MEDIA_OBJECTS"]: # Get the URL theLinkURL = linkNugget["url"] # Parse the name and extension name, extension = os.path.splitext(theLinkURL) extension = extension.lower() # Get some relevant data from the media object theMIME = linkNugget["mime"] theLinkComment = unicode(linkNugget["caption"].decode("UTF-8")) theLinkID = "" theLinkTimestamp = linkNugget["timestamp"] # Structure the media container according to what type of MIME it is if theMIME == "" or theMIME is None: theMIME = "NONE" if theMIME == 'image/gif': thumbURL = linkNugget["thumb"] mediaNugget = [theLinkURL, theLinkComment, "GIF", theLinkID, slug, thumbURL, theLinkTimestamp] picString = u"<td>" picString += u"<a class='imagelink' href='/wiki/%s/%s/'>" % (slug, theLinkID) picString += u"<img class='caption-img lazyloadable' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' data-src='%s' ></a></td>" % (u'Image', theMIME, thumbURL, theLinkURL ) elif 'image' in theMIME: thumbURL = linkNugget["thumb"] mediaNugget = [theLinkURL, theLinkComment, "PICTURE", theLinkID, slug, thumbURL, theLinkTimestamp, theMIME] picString = u"<td>" picString += u"<a class='imagelink' href='/wiki/%s/%s/'>" % (slug, theLinkID) picString += u"<img class='caption-img lazyloadable' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' data-src='%s' ></a></td>" % (u'Image', theMIME, thumbURL, theLinkURL) elif getYouTubeIdIfPresent(theLinkURL): yt_id = getYouTubeIdIfPresent(theLinkURL) LOAD_YOUTUBE_JS = True mediaNugget = [theLinkURL, theLinkComment, "YOUTUBE", yt_id, theLinkID, slug, theLinkTimestamp] picString = u"<td class='inline-video-wrapper'>" picString += u"<a class='imagelink inline-video-overlay' href='/wiki/%s/%s/'>" % (slug, theLinkID) picString += u"<img class='caption-img caption-video lazyloadable' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='https://i.ytimg.com/vi/%s/default.jpg' data-src='https://i.ytimg.com/vi/%s/hqdefault.jpg' ></a></td>" % (u'Youtube Video', theMIME, yt_id, yt_id ) elif extension in VALID_VIDEO_EXTENSIONS: mediaNugget = [theLinkURL, theLinkComment, "NORMAL_VIDEO", theLinkID, theMIME, theLinkTimestamp] picString = u"<td class='inline-video-wrapper'>" picString += u"<a class='imagelink inline-video-overlay' href='/wiki/%s/%s/'>" % (slug, theLinkID) picString += u"<video class='caption-img caption-video' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' preload='metadata'><source src='%s#t=0.1' type='%s'></video></a></td>" % (u'Video', theMIME, theLinkURL, theLinkURL, theMIME ) elif extension in VALID_AUDIO_EXTENSIONS: mediaNugget = [theLinkURL, theLinkComment, "AUDIO", theLinkID, theMIME, theLinkTimestamp] picString = u"<td class='inline-audio-wrapper'>" picString += u"<a class='imagelink inline-audio-overlay' href='/wiki/%s/%s/'>" % (slug, theLinkID) picString += u"<img class='caption-img caption-audio' alt='%s' height='AAA111BBB222CCC333DDD444EEE' width='200' data-mimetype='%s' src='%s' ></a></td>" % (u'Audio', theMIME, theLinkURL ) else: continue stringToRespond = u"<table class='mce-item-table blurb-inline-image-container' data-nlid='%s' data-mce-selected='1'>" \ u"<tbody><tr>%s</tr><tr><caption class='blurbimage-caption'>%s</caption></tr></tbody></table>" % (theLinkID, picString, theLinkComment) resultArray.append([mediaNugget, stringToRespond]) return render(request, 'enterlink/ajaxresults.html', {"result": resultArray, "renderType": url_param, "cited_by": cited_by, "citer_rank": citer_rank, "citer_is_verified": citer_is_verified}) # Get the search term searchterm = request.GET['searchterm'] # Get the conditions of the search if request.GET.get('search_type'): search_type = request.GET.get('search_type') else: search_type = None # The TinyMCE bookmark is a pointer to where the cursor / mouse was in the TinyMCE window. This is useful for inserting # objects there. if request.GET.get('tinymceBookmark'): tinymceBookmark = request.GET.get('tinymceBookmark') else: tinymceBookmark = None # Get the ID of a relevant element, if applicable if request.GET.get('elementID'): elementID = request.GET.get('elementID') else: elementID = None # tinymce-link-page BLURB_CHAR_LIMIT = 250 # Perform the search result = mainSearch(url_param, search_type, searchterm, FETCH_LIMIT, BLURB_CHAR_LIMIT) blurbResults = [] # Blank result if ((result == 0) and (blurbResults == 0)): return render(request, 'enterlink/ajaxresults.html', {"result": result, "renderType": url_param, "blurbResults": blurbResults, "searchterm": searchterm, "search_type": search_type, "tinymceBookmark": tinymceBookmark, "elementID": elementID}) # Return the results return render(request, 'enterlink/ajaxresults.html', {"result": result, "renderType": url_param, "blurbResults": blurbResults, "searchterm": searchterm, "search_type": search_type, "tinymceBookmark": tinymceBookmark, "elementID": elementID})
def AJAX_Hoverlink(request, url_param): # Get the article from the url parameter cleanedParamList = getTheArticleObject(url_param) articleObject = cleanedParamList[1] # Fail if the article has been removed try: if articleObject.is_removed == True: return HttpResponseRedirect('/error/') except: pass # Determine whether to use the lightbox (if desktop or tablet) or the hover bubble (if mobile AMP) useLightBox = False if request.GET.get('lightbox') == "1": useLightBox = True # Determine which area to parse try: mediaType = request.GET.get('media_type') except: mediaType = "" # Get the link to use try: linkURL = request.GET['target_url'] linkURL = urllib.unquote_plus(linkURL) except: linkURL = "" print("LinkURL not found") # Get the cached HTML for the article cacheObject = HashCache.objects.get(ipfs_hash=articleObject.ipfs_hash_current) # Parse the HTML resultDictionary = parseBlockchainHTML(cacheObject.html_blob) # Check for YouTube youtubeResult = getYouTubeIdIfPresent(linkURL) # Get all the citations from the parsed article and loop through them until the requested one is found # When found, save the JSON for that citation citationObject = "" for citation in resultDictionary["CITATION_OBJECTS"]: if citation["url"] == linkURL: citationObject = citation break if youtubeResult: if youtubeResult in citation["url"]: citationObject = citation break # Fill the Django template context with relevant data from both the article... contextDictionary = {} contextDictionary.update({"ARTICLE_NAME": articleObject.page_title}) contextDictionary.update({"ARTICLE_SLUG": articleObject.slug}) contextDictionary.update({"ARTICLE_SLUG_ALT": articleObject.slug_alt}) contextDictionary.update({"ARTICLE_IS_REMOVED": articleObject.is_removed}) contextDictionary.update({"ARTICLE_PHOTO_URL": articleObject.photo_url}) contextDictionary.update({"ARTICLE_THUMB_URL": articleObject.photo_thumb_url}) contextDictionary.update({"ARTICLE_PAGE_TYPE": articleObject.page_type}) contextDictionary.update({"BLURB_SNIPPET": articleObject.blurb_snippet}) # ... and the citation JSON try: # Try the main citations first contextDictionary.update({"CITATION_DESCRIPTION": citationObject["description"]}) contextDictionary.update({"CITATION_TIMESTAMP": citationObject["timestamp"]}) contextDictionary.update({"CITATION_URL": citationObject["url"]}) contextDictionary.update({"CITATION_THUMB": citationObject["thumb"]}) contextDictionary.update({"CITATION_MIME": citationObject["mime"]}) contextDictionary.update({"CITATION_CATEGORY": citationObject["category"]}) contextDictionary.update({"CITATION_YOUTUBE_ID": youtubeResult}) except: # Otherwise try the media ones mediaObject = "" for mediaItem in resultDictionary["MEDIA_OBJECTS"]: # print(mediaItem) # print(linkURL) if mediaItem["url"] == linkURL or mediaItem["thumb"] == linkURL: mediaObject = mediaItem break if youtubeResult: if youtubeResult in mediaItem["url"]: mediaObject = mediaItem break contextDictionary.update({"CITATION_DESCRIPTION": mediaObject["caption"]}) contextDictionary.update({"CITATION_TIMESTAMP": mediaObject["timestamp"]}) contextDictionary.update({"CITATION_URL": mediaObject["url"]}) contextDictionary.update({"CITATION_THUMB": mediaObject["thumb"]}) contextDictionary.update({"CITATION_MIME": mediaObject["mime"]}) contextDictionary.update({"CITATION_CATEGORY": mediaObject["class"]}) contextDictionary.update({"CITATION_YOUTUBE_ID": youtubeResult}) # Render the hoverlink bubble appropriately if (useLightBox): # Desktop and Tablet return render(request, "enterlink/hoverlink_iframe_blockchain.html", contextDictionary) else: # Mobile return render(request, 'enterlink/hoverlink_ajax_blockchain.html', contextDictionary)