123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- """
- Video Privacy Enhancer
- --------------------------
- Authored by Jacob Levernier, 2014
- Released under the GNU AGPLv3
- For more information on this plugin, please see the attached Readme.md file.
- """
- """
- SETTINGS
- """
- output_directory_for_thumbnails = "images/video-thumbnails"
- """
- In order for this plugin to work optimally, you need to do just a few things:
- 1. Enable the plugn in pelicanconf.py (see http://docs.getpelican.com/en/3.3.0/plugins.html for documentation):
- PLUGIN_PATH = "/pelican-plugins"
- PLUGINS = ["video_privacy_enhancer"]
- 2a. If necessary, install jQuery on your site (See https://stackoverflow.com/questions/1458349/installing-jquery -- the jQuery base file should go into your Pelican themes 'static' directory)
- 2b. Copy the jQuery file in this folder into, for example, your_theme_folder/static/video_privacy_enhancer_jQuery.js, and add a line like this to the <head></head> element of your website's base.html (or equivalent) template:
- `<script src="{{ SITEURL }}/theme/video_privacy_enhancer_jquery.js"></script> <!--Load jQuery functions for the Video Privacy Enhancer Pelican plugin -->`
- 3. Choose a default video embed size and add corresponding CSS to your theme's CSS file:
- Youtube allows the following sizes in its embed GUI (as of this writing, in March 2014). I recommend choosing one, and then having the iframe for the actual video embed match it (so that it's a seamless transition). This can be handled with CSS in both cases, so I haven't hard-coded it here:
- 1280 W x 720 H
- 853 W x 480 H
- 640 W x 360 H
- 560 W x 315 H
- Here's an example to add to your CSS file:
- ```
- /* For use with the video-privacy-enhancer Pelican plugin */
- img.video-embed-dummy-image.
- iframe.embedded_youtube_video {
- width: 843px;
- max-height: 480px;
- /* Center the element on the screen */
- display: block;
- margin-top: 2em;
- margin-bottom: 2em;
- margin-left: auto;
- margin-right: auto;
- }
- iframe.embedded_youtube_video {
- width: 843px;
- height: 480px;
- }
- ```
- """
- """
- END SETTINGS
- """
- from pelican import signals
- import os.path
- import re
- import urllib
- import logging
- logger = logging.getLogger(__name__)
- def check_for_thumbnail_directory(pelican_output_path):
-
- try:
- if not os.path.exists(pelican_output_path + "/" + output_directory_for_thumbnails):
- os.makedirs(pelican_output_path + "/" + output_directory_for_thumbnails)
- return True
- except:
- print logger.debug("Error in checking if thumbnail folder exists and making the directory if it doesn't.")
- return False
- def download_thumbnail(video_id_from_shortcode, pelican_output_path):
-
- check_for_thumbnail_directory(pelican_output_path)
-
-
- if not os.path.exists(pelican_output_path + "/" + output_directory_for_thumbnails + "/" + video_id_from_shortcode + ".jpg"):
- urllib.urlretrieve("https://img.youtube.com/vi/" + video_id_from_shortcode + "/0.jpg", pelican_output_path + "/" + output_directory_for_thumbnails + "/" + video_id_from_shortcode + ".jpg")
- def process_youtube_shortcodes(data_passed_from_pelican):
- if data_passed_from_pelican._content:
- full_content_of_page_or_post = data_passed_from_pelican._content
- else:
- return
- all_instances_of_the_youtube_shortcode = re.findall('\!youtube.*?\)', full_content_of_page_or_post)
- if(len(all_instances_of_the_youtube_shortcode) > 0):
- replace_shortcode_in_text = ""
-
- for youtube_shortcode_to_parse in all_instances_of_the_youtube_shortcode:
- video_id_from_shortcode = re.findall('(?<=youtube\().*?(?=\))', youtube_shortcode_to_parse)[0]
-
-
-
-
- pelican_output_path = data_passed_from_pelican.settings['OUTPUT_PATH']
- pelican_site_url = data_passed_from_pelican.settings['SITEURL']
-
- download_thumbnail(video_id_from_shortcode, pelican_output_path)
-
- replace_shortcode_in_text = re.sub(r'\!youtube\(' + video_id_from_shortcode + '\)', r'<img class="youtube-embed-dummy-image" id="' + video_id_from_shortcode + '" src="' + pelican_site_url + '/' + output_directory_for_thumbnails + '/' + video_id_from_shortcode + '.jpg" alt="Embedded Video - Click to view" title="Embedded Video - Click to view"></img>', full_content_of_page_or_post)
-
- data_passed_from_pelican._content = replace_shortcode_in_text
- def register():
- signals.content_object_init.connect(process_youtube_shortcodes)
|