Skip to content

Commit

Permalink
fix redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanie-A committed Oct 15, 2024
1 parent 2d9c8e4 commit 8dec78c
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
"""
"""
Python URI shortener
"""
import hashlib
import uuid
import boto3

# Initialize DynamoDB
region_name = 'us-east-1'
dynamoDB = boto3.resource('dynamodb', region_name=region_name)
table = dynamoDB.Table('newURI')

def generate_short_url(long_url):
"""
URI function
Generate a short URL based on the MD5 hash of the long URL.
"""
hash_object = hashlib.md5(long_url.encode())
hash_digest = hash_object.hexdigest()
short_url = hash_digest[:5]
base_url = "http://short_url/" #replace with your domain name
base_url = "http://short_url/" # Replace with your actual domain name
return f"{base_url}{short_url}"

def update_table(table):
table.put_item(
def update_table(table, short_url):
"""
Update the DynamoDB table with the generated short URL.
"""
try:
table.put_item(
Item={
'id': str(uuid.uuid()),
'shorturl' : SHORT_URI
}
'id': str(uuid.uuid4()),
'shorturl': short_url
}
)

try:
update_table(table)
except Exception as e:
print(f"Error updating table: {e}")

print(f"Successfully updated table with short URL: {short_url}")
except Exception as e:
print(f"Error updating table: {e}")

if __name__ == "__main__":
try:
# Get URL input from the user
input_url = input("Enter the URL to shorten: ")

# Generate short URL
SHORT_URI = generate_short_url(input_url)
print(f"Shortened URL: {SHORT_URI}")

# Update DynamoDB table
update_table(table, SHORT_URI)

except EOFError:
print("No input provided. Please run the script again.")
except Exception as e:
print(f"Error: {e}")
print(f"Error: {e}")

0 comments on commit 8dec78c

Please sign in to comment.