123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- """
- 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.
- """
- """
- LIBRARIES FOR PYTHON TO USE
- """
- from pelican import signals
- import os.path
- import re
- import logging
- logger = logging.getLogger(__name__)
- import six
- import six.moves.urllib.request
- from . import video_service_thumbnail_url_generating_functions as video_thumbnail_functions
- """
- END OF LIBRARIES
- """
- """
- SETTINGS
- """
- output_directory_for_thumbnails = "images/video-thumbnails"
- supported_video_services = {
- "youtube": {
- "shortcode_not_including_exclamation_point": "youtube",
- "function_for_generating_thumbnail_url": video_thumbnail_functions.generate_thumbnail_download_link_youtube,
- },
- "vimeo": {
- "shortcode_not_including_exclamation_point": "vimeo",
- "function_for_generating_thumbnail_url": video_thumbnail_functions.generate_thumbnail_download_link_vimeo,
- },
- }
- """
- 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 theme's '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_privacy_video {
- width: 853px;
- 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_privacy_video {
- width: 843px;
- height: 480px;
- }
- ```
- """
- """
- END SETTINGS
- """
- 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 Exception as e:
- logger.error("Video Privacy Enhancer Plugin: Error in checking if thumbnail folder exists and making the directory if it doesn't: %s", e)
- return False
- def download_thumbnail(video_id_from_shortcode, video_thumbnail_url, video_service_name, pelican_output_path):
-
- check_for_thumbnail_directory(pelican_output_path)
-
-
- if not os.path.exists(pelican_output_path + "/" + output_directory_for_thumbnails + "/" + video_service_name + "_" + video_id_from_shortcode + ".jpg"):
- logger.debug("Video Privacy Enhancer Plugin: Downloading thumbnail from the following url: " + video_thumbnail_url)
- six.moves.urllib.request.urlretrieve(video_thumbnail_url, pelican_output_path + "/" + output_directory_for_thumbnails + "/" + video_service_name + "_" + video_id_from_shortcode + ".jpg")
- def process_shortcodes(data_passed_from_pelican):
- dictionary_of_services = supported_video_services
-
- if not data_passed_from_pelican._content:
- return
-
-
- for video_service_name, video_service_information in six.iteritems(dictionary_of_services):
-
-
- logger.debug("Video Privacy Enhancer Plugin: The name of the current service being processed is '" + video_service_name + "'")
-
- shortcode_to_search_for_not_including_exclamation_point = video_service_information['shortcode_not_including_exclamation_point']
- logger.debug("Video Privacy Enhancer Plugin: Currently looking for the shortcode '" + shortcode_to_search_for_not_including_exclamation_point + "'")
-
- function_for_generating_thumbnail_url = video_service_information['function_for_generating_thumbnail_url']
-
- all_instances_of_the_shortcode = re.findall('(?<!\\\)\!' + shortcode_to_search_for_not_including_exclamation_point + '.*?\)', data_passed_from_pelican._content)
-
- if(len(all_instances_of_the_shortcode) > 0):
- replace_shortcode_in_text = ""
-
- for shortcode_to_parse in all_instances_of_the_shortcode:
- video_id_from_shortcode = re.findall('(?<=' + shortcode_to_search_for_not_including_exclamation_point + '\().*?(?=\))', shortcode_to_parse)[0]
-
-
-
-
- pelican_output_path = data_passed_from_pelican.settings['OUTPUT_PATH']
- pelican_site_url = data_passed_from_pelican.settings['SITEURL']
-
-
- video_thumbnail_url = function_for_generating_thumbnail_url(video_id_from_shortcode)
-
- download_thumbnail(video_id_from_shortcode, video_thumbnail_url, video_service_name, pelican_output_path)
-
-
- replace_shortcode_in_text = re.sub(r'\!' + shortcode_to_search_for_not_including_exclamation_point + '\(' + video_id_from_shortcode + '\)', r'<img class="video-embed-dummy-image" id="' + video_id_from_shortcode + '" src="' + pelican_site_url + '/' + output_directory_for_thumbnails + '/' + video_service_name + '_' + video_id_from_shortcode + '.jpg" alt="Embedded Video - Click to view" title="Embedded Video - Click to view" embed-service="' + video_service_name + '"></img>', data_passed_from_pelican._content)
-
-
- data_passed_from_pelican._content = replace_shortcode_in_text
- def register():
- signals.content_object_init.connect(process_shortcodes)
|