A lightweight, zero-dependency Python library for detecting, validating, and sanitizing social media profile URLs. Supports 50+ platforms out of the box with automatic URL normalization, username extraction, and customizable regex patterns for extensibility.
Project description
Social Links
A lightweight, zero-dependency Python library for detecting, validating, and sanitizing social media profile URLs. Supports 50+ platforms out of the box with automatic URL normalization, username extraction, and customizable regex patterns for extensibility.
🚀 Try it interactively in your browser! Test the library with our Interactive Demo - no installation required.
Features
- 🔍 Auto-detect social media platforms from URLs
- ✅ Validate URLs against specific platforms
- 🧹 Sanitize URLs to canonical format
- 🎯 50+ predefined platforms (LinkedIn, GitHub, Twitter/X, Facebook, Instagram, YouTube, and more)
- 🔧 Customizable - Add your own platforms with regex patterns
- 🚀 Zero dependencies - Pure Python, no external libraries
Installation
pip install social-links
Or using uv:
uv pip install social-links
Quick Start
from sociallinks import SocialLinks
# Initialize with predefined platforms
sl = SocialLinks()
# Detect platform from URL
platform = sl.detect_platform("https://www.linkedin.com/in/ysskrishna/")
print(platform) # "linkedin"
# Validate URL for a specific platform
is_valid = sl.is_valid("linkedin", "https://www.linkedin.com/in/ysskrishna/")
print(is_valid) # True
# Sanitize URL to canonical format
sanitized = sl.sanitize("linkedin", "https://www.linkedin.com/in/ysskrishna/")
print(sanitized) # "https://linkedin.com/in/ysskrishna"
Supported Platforms
The library comes with 50+ predefined platforms:
Usage Examples
Detect Platform
sl = SocialLinks()
# Detect from full URL
sl.detect_platform("https://github.com/ysskrishna") # "github"
sl.detect_platform("https://x.com/ysskrishna") # "x"
sl.detect_platform("https://example.com") # None
# Works with various URL formats
sl.detect_platform("http://linkedin.com/in/ysskrishna")
sl.detect_platform("www.facebook.com/ysskrishna")
sl.detect_platform(" https://instagram.com/ysskrishna ") # Handles whitespace
Validate URLs
sl = SocialLinks()
# Validate against specific platform
sl.is_valid("linkedin", "https://www.linkedin.com/in/ysskrishna/") # True
sl.is_valid("linkedin", "https://example.com") # False
sl.is_valid("github", "https://github.com/ysskrishna") # True
Sanitize URLs
sl = SocialLinks()
# Normalize to canonical format
sl.sanitize("linkedin", "https://www.linkedin.com/in/ysskrishna/")
# Returns: "https://linkedin.com/in/ysskrishna"
sl.sanitize("github", "http://www.github.com/ysskrishna")
# Returns: "https://github.com/ysskrishna"
sl.sanitize("x", "https://twitter.com/ysskrishna")
# Returns: "https://x.com/ysskrishna"
Custom Platforms
sl = SocialLinks(use_predefined_platforms=False)
# Add a custom platform
custom_platform = [{
"patterns": [
r"https?://(www\.)?example\.com/(?P<id>[A-Za-z0-9_]+)/?$",
r"^(?P<id>[A-Za-z0-9_]+)$" # Username-only pattern
],
"sanitized": "https://example.com/{id}"
}]
sl.set_platform("example", custom_platform)
# Now you can use it
sl.detect_platform("https://example.com/user123") # "example"
sl.sanitize("example", "https://example.com/user123") # "https://example.com/user123"
Platform Management
sl = SocialLinks()
# List all platforms
platforms = sl.list_platforms()
# Returns: ["behance", "dev_to", "dribbble", ...]
# Get platform configuration
config = sl.get_platform("github")
# Add multiple platforms
new_platforms = {
"platform1": [{
"patterns": [r"https?://example1.com/(?P<id>\w+)"],
"sanitized": "https://example1.com/{id}"
}],
"platform2": [{
"patterns": [r"https?://example2.com/(?P<id>\w+)"],
"sanitized": "https://example2.com/{id}"
}]
}
sl.set_platforms(new_platforms, override=False)
# Delete platforms
sl.delete_platform("custom_platform")
sl.delete_platforms(["platform1", "platform2"])
# Clear all platforms
sl.clear_platforms()
Custom Regex Flags
import re
from sociallinks import SocialLinks
# Use custom regex flags
sl = SocialLinks(regex_flags=re.IGNORECASE | re.MULTILINE)
Changelog
See CHANGELOG.md for a detailed list of changes and version history.
Roadmap
The following improvements are planned for upcoming releases:
- Add method to configure custom sanitization patterns
- Create Streamlit demo application
- Integrate development tools (flake8, black, isort) for code quality
- Add code coverage reporting with pytest-cov
- Refactor platform entries using dataclasses for better structure
- Consider functional API alternative to SocialLinks class for simpler usage
Contributing
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct, development setup, and the process for submitting pull requests.
Support
If you find this library useful, please consider:
-
⭐ Starring the repository on GitHub to help others discover it.
-
💖 Sponsoring to support ongoing maintenance and development.
Become a Sponsor on GitHub | Support on Patreon
Links
-
Documentation: ysskrishna.github.io/social-links
-
Interactive Demo: ysskrishna.github.io/social-links/demo/
-
Homepage: github.com/ysskrishna/social-links
-
Repository: github.com/ysskrishna/social-links.git
License
MIT License - see LICENSE file for details.
Author
Y. Siva Sai Krishna
-
GitHub: @ysskrishna
-
LinkedIn: ysskrishna
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file
social_links-1.2.1.tar.gz.File metadata
File hashes
28d6f80db1070b85f5b0fbbdbe5c5b368f18a8c3869ba23acbd773420dae59bf86cd0c7b01835ac5ed3c6b80c3ff7a477516ea36c6b1d0fc613f3d9061e1e86e86f6e8721ade905d1dc754828c5465ecSee more details on using hashes here.