Skip to main content

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

License Tests Python PyPI PyPI Downloads Documentation Interactive Demo

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.

OG Image

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:

Predefined Platforms
Apple Music Bandcamp Behance
Bluesky Crunchbase Dev.to
Discord Douyin Dribbble
Etsy Exercism Facebook
Flickr GitHub GitLab
Gravatar Gumroad Hacker News
Hashnode Instagram Keybase
Kuaishou Lemmy World LinkedIn (personal & company)
Linktree Mastodon Medium
Patreon Pinterest Product Hunt
Quora Reddit Signal
SlideShare Snapchat SoundCloud
Spotify Stack Overflow Steam
Substack Telegram Threads
TikTok Tumblr Twitch
Vimeo VK WeChat
Weibo Wellfound (AngelList) WhatsApp
X (Twitter) YouTube

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

License

MIT License - see LICENSE file for details.

Author

Y. Siva Sai Krishna

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

social_links-1.2.1.tar.gz (411.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

social_links-1.2.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page