Advertisement
MaxDjently

update_blog-wsl.sh

Mar 27th, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.60 KB | Software | 0 0
  1. #!/usr/bin/bash
  2.  
  3. bash update_dependencies-wsl.sh
  4.  
  5. # Define the HTML file and the post file
  6. HTML_FILE="index.html"
  7. POST_FILE="post.txt"
  8. TEMP_FILE="temp.html"
  9.  
  10. # Function to add lazy loading to iframes
  11. add_lazy_loading() {
  12.     echo "$1" | sed 's/<iframe\(.\{1,\}loading="lazy"\)\{0,1\}/<iframe loading="lazy"/g'
  13. }
  14.  
  15. # Function to create a blog entry
  16. create_blog_entry() {
  17.     local title="$1"
  18.     local channel="$2"
  19.     local content="$3"
  20.     local blog_entry
  21.  
  22.     # Construct blog_entry as a single line
  23.     if grep -q "<iframe" <<< "$content"; then
  24.         content=$(add_lazy_loading "$content")
  25.         blog_entry="<div class='post'>$(date):<p><h3>$title</h3></p>"
  26.         [ -n "$channel" ] && blog_entry+="<p>Channel: $channel</p>"
  27.         blog_entry+="</div><div class='video-container'>$content</div>"
  28.     else
  29.         blog_entry="<div class='post'>$(date):<p><h3>$title</h3></p>"
  30.         [ -n "$channel" ] && blog_entry+="<p>Channel: $channel</p>"
  31.         blog_entry+="$content</div>"
  32.     fi
  33.  
  34.     # Insert blog_entry as a single line into index.html
  35.     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"
  36.  
  37.     echo "New blog post added to $HTML_FILE"
  38. }
  39.  
  40.  
  41. # Check if URL is provided
  42. if [ -z "$1" ]; then
  43.     echo "No URL provided. Using content from $POST_FILE."
  44.     if [ -e "$POST_FILE" ]; then
  45.         TITLE=$(awk 'NR==1' "$POST_FILE" | tr -d '\n')
  46.         CHANNEL=$(awk 'NR==2' "$POST_FILE" | tr -d '\n')
  47.         POST_CONTENT=$(awk 'NR>2' "$POST_FILE" | tr -d '\n')
  48.         create_blog_entry "$TITLE" "$CHANNEL" "$POST_CONTENT"
  49.     else
  50.         echo "$POST_FILE not found. Exiting..."
  51.         exit 0
  52.     fi
  53. else
  54.     # Get the video metadata in JSON format using yt-dlp
  55.     metadata=$(yt-dlp -j "$1" 2>/dev/null)
  56.  
  57.     # Check if yt-dlp succeeded in fetching metadata
  58.     if [ $? -eq 0 ] && [ -n "$metadata" ]; then
  59.         # Extract video title, ID, and channel name
  60.         title=$(echo "$metadata" | jq -r '.title')
  61.         video_id=$(echo "$metadata" | jq -r '.id')
  62.         channel_name=$(echo "$metadata" | jq -r '.uploader')
  63.         channel_url=$(echo "$metadata" | jq -r '.uploader_url')
  64.     else
  65.         # Default values when metadata retrieval fails
  66.         title="Unknown Title"
  67.         video_id=""
  68.         channel_name="Unknown Channel"
  69.         channel_url="#"
  70.     fi
  71.  
  72.     # Construct the embed code
  73.     if [ -n "$video_id" ]; then
  74.         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>"
  75.     else
  76.         embed_code="Video could not be embedded."
  77.     fi
  78.  
  79.     # Create a hyperlink for the channel
  80.     if [ -n "$channel_url" ] && [ "$channel_url" != "#" ]; then
  81.         channel_link="<a href=\"$channel_url\" target=\"_blank\" rel=\"noopener\">$channel_name</a>"
  82.     else
  83.         channel_link="$channel_name"
  84.     fi
  85.  
  86.     # Write the title, channel link, and embed code to the post file
  87.     {
  88.         echo "$title"
  89.         echo "$channel_link"
  90.         echo "$embed_code"
  91.     } > "$POST_FILE"
  92.  
  93.     # Read the title, channel, and content of the post file
  94.     TITLE=$(awk 'NR==1' "$POST_FILE" | tr -d '\n')
  95.     CHANNEL=$(awk 'NR==2' "$POST_FILE" | tr -d '\n')
  96.     POST_CONTENT=$(awk 'NR>2' "$POST_FILE" | tr -d '\n')
  97.     create_blog_entry "$TITLE" "$CHANNEL" "$POST_CONTENT"
  98. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement