Skip to content

feat: ['TOTP', 'Albuminator'] #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none"
}
92 changes: 92 additions & 0 deletions Advanced Python Programming/Albuminator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# IMPORTANT: pip install tinytag
# Scans the current working directory for audio files, extracts the artist and album metadata from each file,
# creates folders based on the metadata, and moves the files into the respective folders.
# This helps in organizing the audio files by artist and album.
# This code assumes that the audio file has embedded metadata in it.

# NOTE: Optimized for windows only, but still can use in unix
# UNSUPPORTED: '.wav'

# Author: Jorn Blaedel Garbosa

import os
import shutil
from tinytag import TinyTag
import re

# Current directory as the source directory
source_directory = os.getcwd()

# Function to sanitize and capitalize folder names


def sanitize_and_capitalize_folder_name(name):
# Remove characters not allowed in Windows file names
sanitized_name = re.sub(r'[<>:"/\\|?*]', "", name)

# Split the name into words and capitalize words outside parentheses or brackets
words = sanitized_name.split()
capitalized_words = []
capitalize_next_word = True

for word in words:
if '(' in word or '[' in word:
capitalize_next_word = False

if capitalize_next_word:
word = word.capitalize()

capitalized_words.append(word)

if ')' in word or ']' in word:
capitalize_next_word = True

capitalized_name = ' '.join(capitalized_words)
return capitalized_name.strip()


# Iterate over each file in the source directory
for file_name in os.listdir(source_directory):
file_path = os.path.join(source_directory, file_name)

# Check if the file is an audio file using tinytag
if os.path.isfile(file_path) and TinyTag.is_supported(file_path):
audio = TinyTag.get(file_path)

# Extract the artist and album from the audio file's metadata
artist = audio.artist
album = audio.album

# Skip the file if artist or album metadata is missing
if not artist or not album:
print(
f"Skipping file: {file_name} (Missing artist or album metadata)")
continue

# If the artist field contains multiple artists separated by a delimiter, select the first artist
artist = artist.split('/')[0]

else:
continue

# Format and sanitize the folder names as artist and album, and capitalize them
artist_folder_name = sanitize_and_capitalize_folder_name(artist)
album_folder_name = sanitize_and_capitalize_folder_name(album)

# Create the artist folder if it doesn't exist
artist_folder = os.path.join(source_directory, artist_folder_name)
if not os.path.exists(artist_folder):
os.makedirs(artist_folder)
print(f"Created folder: {artist_folder_name}")

# Create the album folder inside the artist folder if it doesn't exist
album_folder = os.path.join(artist_folder, album_folder_name)
if not os.path.exists(album_folder):
os.makedirs(album_folder)
print(f"Created folder: {album_folder_name}")

# Move the file to the album folder
destination_path = os.path.join(album_folder, file_name)
shutil.move(file_path, destination_path)
print(
f"Moved file: {file_name} to {os.path.join(artist_folder_name, album_folder_name)}")
54 changes: 54 additions & 0 deletions Advanced Python Programming/TOTP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Emulates behavior of an authenticator app like Google Authenticator
# Sample usage:
# '''
# Provider [...Facebook, Google]: Facebook
# Secret Key: I3LQHRSPGB72JUSD5FAPJVDL75D5I4IL
# TOTP: 618282
# '''

# Author: Jorn Blaedel Garbosa

import hashlib
import hmac
import struct
import time
import base64


def generate_totp(secret_key, digits=6, time_interval=30):
# Get the current Unix time
current_time = int(time.time())

# Calculate the number of time intervals that have elapsed since the Unix epoch
time_steps = current_time // time_interval

# Convert the time steps to a byte string
time_steps_bytes = struct.pack(">Q", time_steps)

# Compute the HMAC-SHA1 hash using the secret key and time steps
hmac_sha1 = hmac.new(base64.b32decode(secret_key),
time_steps_bytes, hashlib.sha1).digest()

# Get the offset value from the last 4 bits of the hash
offset = hmac_sha1[-1] & 0x0F

# Get the 4 bytes at the specified offset
truncated_hash = hmac_sha1[offset:offset + 4]

# Convert the truncated hash to an integer
totp = struct.unpack(">L", truncated_hash)[0] & 0x7FFFFFFF

# Normalize the TOTP to the desired length
totp %= 10**digits

# Convert the TOTP to a string
totp_str = str(totp).zfill(digits)

return totp_str


str(input("Provider [...Facebook, Google]: "))

secret_key = str(input("Secret Key: "))

print("TOTP: ", generate_totp(secret_key=secret_key))