1. Web Scraping and DOM Extraction
Efficiently retrieve remote HTML documents and parse specific elements using requests and BeautifulSoup. The implementation supports custom headers for rate limiting avoidance and provides utility methods for targeted tag retrieval.
import requests
from bs4 import BeautifulSoup
def fetch_and_parse(target_url: str, headers: dict = None) -> dict:
if headers is None:
headers = {"User-Agent": "CustomBot/1.0"}
response = requests.get(target_url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
return {
"raw_html": response.text,
"parsed_doc": soup,
"headings": soup.find_all(["h1", "h2", "h3"]),
"links": [a.get("href") for a in soup.find_all("a", href=True)],
"paragraph_text": soup.find("p").get_text(strip=True) if soup.find("p") else None
}
2. QR Code Decoding Utility
Automate the extraction of embedded data from image files using pyzbar and Pillow. This approach handles both grayscale and color formats seamlessly.
from PIL import Image
from pyzbar import pyzbar
def decode_qr_image(image_path: str) -> str:
with Image.open(image_path) as img:
symbols = pyzbar.decode(img)
if not symbols:
raise ValueError("No QR code detected.")
return symbols[0].data.decode("utf-8")
3. Screen Capture Manager
Capture full displays or defined regions programmatically. The refactored script incorporates a delay mechanism and utilizes mss for optimized performance over traditional methods.
import mss
import time
from PIL import Image
class ScreenRecorder:
@staticmethod
def capture_full(output_filename: str):
with mss.mss() as sct:
monitor = sct.monitors[1]
sct_img = sct.grab(monitor)
Image.frombytes("RGB", sct_img.size, sct_img.rgb).save(output_filename)
@staticmethod
def capture_region(bbox: tuple, output_filename: str, delay_sec: int = 0):
if delay_sec > 0:
time.sleep(delay_sec)
with mss.mss() as sct:
region = {"top": bbox[1], "left": bbox[0], "width": bbox[2], "height": bbox[3]}
sct_img = sct.grab(region)
Image.frombytes("RGB", sct_img.size, sct_img.rgb).save(output_filename)
4. PDF-to-Audiobook Converter
Transform printed documentation into spoken audio by chaining pypdf for text etxraction and gTTS for speech synthesis. The logic iterates through each page, sanitizes the text, and generates sequential audio files.
from pypdf import PdfReader
from gtts import gTTS
import re
def generate_audio_from_pdf(pdf_path: str, lang: str = "en"):
document = PdfReader(pdf_path)
for idx, page in enumerate(document.pages):
raw_text = page.extract_text()
cleaned_text = re.sub(r'\s+', ' ', raw_text).strip()
if not cleaned_text:
continue
audio = gTTS(text=cleaned_text, lang=lang)
audio.save(f"chapter_{idx+1}.mp3")
5. Document Manipulation Toolkit
Perform structural edits on portable document format files, including page rotation, extraction, and merging, using the pypdf libray.
from pypdf import PdfReader, PdfWriter
from pathlib import Path
def extract_pages(source_path: str, indices: list, output_path: str):
reader = PdfReader(source_path)
writer = PdfWriter()
for i in indices:
writer.add_page(reader.pages[i])
with open(output_path, "wb") as f:
writer.write(f)
def merge_documents(file_list: list[str], output_path: str):
writer = PdfWriter()
for fp in file_list:
reader = PdfReader(fp)
writer.append(reader)
writer.write(output_path)
def rotate_all_pages(source_path: str, degrees: int, output_path: str):
reader = PdfReader(source_path)
writer = PdfWriter()
for page in reader.pages:
page.rotate(degrees)
writer.add_page(page)
writer.write(output_path)
6. Command-Line Developer Assistant
Integrate the howdoi utility directly into Python workflows to fetch technical solutions without leaving the terminal. The script wraps the CLI tool via subprocess and formats the output.
import subprocess
import shlex
def query_developer_help(search_query: str) -> str:
command = shlex.split(f"howdoi {search_query}")
process = subprocess.run(
command,
capture_output=True,
text=True
)
if process.returncode != 0:
return "Query failed or package not installed."
return process.stdout
7. Android Device Automation Controller
Interface with mobile devices via the Android Debug Bridge (ADB). This class-based implementation standardizes touch input, media control, and file synchronization.
import subprocess
import shlex
class MobileDeviceController:
@staticmethod
def _execute(cmd: str) -> str:
result = subprocess.run(shlex.split(cmd), capture_output=True, text=True)
return result.stdout.strip()
def perform_swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration_ms: int = 500):
return self._execute(f"adb shell input swipe {start_x} {start_y} {end_x} {end_y} {duration_ms}")
def tap_coordinate(self, x: int, y: int):
return self._execute(f"adb shell input tap {x} {y}")
def initiate_call(self, phone_number: str):
return self._execute(f'adb shell am start -a android.intent.action.CALL -d tel:{phone_number}')
def retrieve_file(self, remote_path: str, local_dest: str = None):
dest = local_dest if local_dest else "."
return self._execute(f"adb pull {remote_path} {dest}")
8. Hardware Thermal Monitor
Retrieve real-time temperature metrics from system sensors. The implementation leverages pythonnet to bridge Python with the OpenHardwareMonitor library on Windows environments.
import clr
import time
clr.AddReference("OpenHardwareMonitorLib")
from OpenHardwareMonitorLib import Computer
def monitor_thermal_readings(interval: float = 2.0):
hardware_interface = Computer()
hardware_interface.GPUEnabled = True
hardware_interface.CPUEnabled = True
hardware_interface.Open()
try:
while True:
for hw_component in hardware_interface.Hardware:
hw_component.Update()
for sensor in hw_component.Sensors:
if "temperature" in sensor.Identifier.ToString().lower():
if sensor.Value is not None:
print(f"{sensor.Name}: {sensor.Value}°C")
time.sleep(interval)
except KeyboardInterrupt:
hardware_interface.Close()
9. Social Media Content Publisher
Automate media uploads to Instagram platforms using instagrapi. The script handles authentication, metadata injection, and supports both feed posts and temporary stories.
from instagrapi import Client
def publish_media(username: str, password: str, file_path: str, caption: str, media_type: str = "photo"):
client = Client()
client.login(username, password)
if media_type == "photo":
client.photo_upload(file_path, caption=caption)
elif media_type == "video":
client.video_upload(file_path, caption=caption)
elif media_type == "story":
client.photo_upload_to_story(file_path)
else:
raise ValueError("Unsupported media type.")
print(f"Successfully published {file_path} as {media_type}.")
10. Dynamic Video Watermarking
Overlay text or grapihcal elements onto video streams using moviepy. The refactored script calculates optimal positioning and composites the watermark across the entire timeline.
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
def apply_video_watermark(video_path: str, watermark_text: str, output_path: str):
base_clip = VideoFileClip(video_path)
text_layer = TextClip(watermark_text, fontsize=30, color="white", font="Arial-Bold")
text_layer = text_layer.set_duration(base_clip.duration)
text_layer = text_layer.set_pos(lambda t: (base_clip.w * 0.05, base_clip.h - 0.1 - text_layer.h))
composite = CompositeVideoClip([base_clip, text_layer])
composite.write_videofile(output_path, codec="libx264", audio_codec="aac")
base_clip.close()