Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/bash
- bash update_dependencies-wsl.sh
- # Define the HTML file and the post file
- HTML_FILE="index.html"
- POST_FILE="post.txt"
- TEMP_FILE="temp.html"
- # Function to add lazy loading to iframes
- add_lazy_loading() {
- echo "$1" | sed 's/<iframe\(.\{1,\}loading="lazy"\)\{0,1\}/<iframe loading="lazy"/g'
- }
- # Function to create a blog entry
- create_blog_entry() {
- local title="$1"
- local channel="$2"
- local content="$3"
- local blog_entry
- # Construct blog_entry as a single line
- if grep -q "<iframe" <<< "$content"; then
- content=$(add_lazy_loading "$content")
- blog_entry="<div class='post'>$(date):<p><h3>$title</h3></p>"
- [ -n "$channel" ] && blog_entry+="<p>Channel: $channel</p>"
- blog_entry+="</div><div class='video-container'>$content</div>"
- else
- blog_entry="<div class='post'>$(date):<p><h3>$title</h3></p>"
- [ -n "$channel" ] && blog_entry+="<p>Channel: $channel</p>"
- blog_entry+="$content</div>"
- fi
- # Insert blog_entry as a single line into index.html
- awk -v entry="$blog_entry" '/<div id='\''blog-entries'\''>/ {printf "%s\n", $0; printf "%s\n", entry; next}1' "$HTML_FILE" > "$TEMP_FILE" && mv "$TEMP_FILE" "$HTML_FILE"
- echo "New blog post added to $HTML_FILE"
- }
- # Check if URL is provided
- if [ -z "$1" ]; then
- echo "No URL provided. Using content from $POST_FILE."
- if [ -e "$POST_FILE" ]; then
- TITLE=$(awk 'NR==1' "$POST_FILE" | tr -d '\n')
- CHANNEL=$(awk 'NR==2' "$POST_FILE" | tr -d '\n')
- POST_CONTENT=$(awk 'NR>2' "$POST_FILE" | tr -d '\n')
- create_blog_entry "$TITLE" "$CHANNEL" "$POST_CONTENT"
- else
- echo "$POST_FILE not found. Exiting..."
- exit 0
- fi
- else
- # Get the video metadata in JSON format using yt-dlp
- metadata=$(yt-dlp -j "$1" 2>/dev/null)
- # Check if yt-dlp succeeded in fetching metadata
- if [ $? -eq 0 ] && [ -n "$metadata" ]; then
- # Extract video title, ID, and channel name
- title=$(echo "$metadata" | jq -r '.title')
- video_id=$(echo "$metadata" | jq -r '.id')
- channel_name=$(echo "$metadata" | jq -r '.uploader')
- channel_url=$(echo "$metadata" | jq -r '.uploader_url')
- else
- # Default values when metadata retrieval fails
- title="Unknown Title"
- video_id=""
- channel_name="Unknown Channel"
- channel_url="#"
- fi
- # Construct the embed code
- if [ -n "$video_id" ]; then
- embed_code="<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/$video_id\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen></iframe>"
- else
- embed_code="Video could not be embedded."
- fi
- # Create a hyperlink for the channel
- if [ -n "$channel_url" ] && [ "$channel_url" != "#" ]; then
- channel_link="<a href=\"$channel_url\" target=\"_blank\" rel=\"noopener\">$channel_name</a>"
- else
- channel_link="$channel_name"
- fi
- # Write the title, channel link, and embed code to the post file
- {
- echo "$title"
- echo "$channel_link"
- echo "$embed_code"
- } > "$POST_FILE"
- # Read the title, channel, and content of the post file
- TITLE=$(awk 'NR==1' "$POST_FILE" | tr -d '\n')
- CHANNEL=$(awk 'NR==2' "$POST_FILE" | tr -d '\n')
- POST_CONTENT=$(awk 'NR>2' "$POST_FILE" | tr -d '\n')
- create_blog_entry "$TITLE" "$CHANNEL" "$POST_CONTENT"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement