Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
viveknaskar
python-experiments
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
python-experiments
/
url_shortener.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (35 loc) · 1.22 KB
main
Breadcrumbs
python-experiments
/
url_shortener.py
Copy path
Top
File metadata and controls
Code
Blame
46 lines (35 loc) · 1.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import json
import time
import random
# Load the URL mapping from the JSON file
def load_url_mapping():
with open('url_mapping.json', 'r') as f:
return json.load(f)
# Save the URL mapping to the JSON file
def save_url_mapping(url_mapping):
with open('url_mapping.json', 'w') as f:
json.dump(url_mapping, f)
# Generate a shortened URL
def generate_short_url():
# Use a combination of a randomly generated string and a timestamp to create a unique shortened URL
return f"{int(time.time())}_{random.randint(1000000, 9999999)}"
# Shorten a URL
def shorten_url(original_url):
url_mapping = load_url_mapping()
# Check if the original URL is already in the mapping
if original_url in url_mapping:
return url_mapping[original_url]
# Generate a new shortened URL
short_url = generate_short_url()
# Save the mapping to the database
url_mapping[original_url] = short_url
save_url_mapping(url_mapping)
return short_url
# Expand a shortened URL
def expand_url(short_url):
url_mapping = load_url_mapping()
# Find the original URL in the mapping
for original_url, url in url_mapping.items():
if url == short_url:
return original_url
return None
You can’t perform that action at this time.