Created
May 5, 2023 07:56
-
-
Save gklein/ed9bcfa51001b7b4127a494c90721533 to your computer and use it in GitHub Desktop.
This code takes a LinkedIn post URL and returns the exact date of the post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This code takes a LinkedIn URL and returns the date of the post. | |
The code uses the following steps: | |
1. Extracts the post ID from the URL. | |
2. Converts the post ID to a Unix timestamp. | |
3. Converts the Unix timestamp to a human-readable date. | |
The code uses the following libraries: | |
* `re`: Regular expressions | |
* `datetime`: Date and time handling | |
""" | |
import re | |
from datetime import datetime | |
def get_post_id(url): | |
""" | |
This function extracts the post ID from a LinkedIn URL. | |
Args: | |
url: The LinkedIn URL. | |
Returns: | |
The post ID. | |
""" | |
# Check if the URL is a valid LinkedIn URL. | |
if not re.match(r"https://www.linkedin.com/post/[0-9]+", url): | |
raise ValueError("Invalid URL") | |
# Extract the post ID from the URL. | |
post_id = re.search(r"([0-9]{19})", url).group() | |
return post_id | |
def extract_unix_timestamp(post_id): | |
""" | |
This function converts a post ID to a Unix timestamp. | |
Args: | |
post_id: The post ID. | |
Returns: | |
The Unix timestamp. | |
""" | |
# Convert the post ID to a binary string. | |
as_binary = format(int(post_id), "b") | |
# Take the first 41 characters of the binary string. | |
first_41_chars = as_binary[:41] | |
# Convert the binary string to a Unix timestamp. | |
timestamp = int(first_41_chars, 2) / 1000 | |
return timestamp | |
def unix_timestamp_to_human_date(timestamp): | |
""" | |
This function converts a Unix timestamp to a human-readable date. | |
Args: | |
timestamp: The Unix timestamp. | |
Returns: | |
The human-readable date. | |
""" | |
# Create a datetime object from the Unix timestamp. | |
date_object = datetime.utcfromtimestamp(timestamp) | |
# Format the datetime object in a human-readable format. | |
human_date_format = date_object.strftime("%a, %d %b %Y %H:%M:%S GMT") | |
return human_date_format | |
def unix_timestamp_to_string(timestamp): | |
""" | |
This function converts a Unix timestamp to a string. | |
Args: | |
timestamp: The Unix timestamp. | |
Returns: | |
The string. | |
""" | |
# Create a datetime object from the Unix timestamp. | |
date_object = datetime.utcfromtimestamp(timestamp) | |
# Format the datetime object in a string format. | |
formatted_date = date_object.strftime("%Y-%m-%d %H:%M") | |
return formatted_date | |
def get_date(url): | |
""" | |
This function takes a LinkedIn URL and returns the date of the post. | |
Args: | |
url: The LinkedIn URL. | |
Returns: | |
The date of the post. | |
""" | |
# Extract the post ID from the URL. | |
post_id = get_post_id(url) | |
# Convert the post ID to a Unix timestamp. | |
unix_timestamp = extract_unix_timestamp(post_id) | |
# Convert the Unix timestamp to a human-readable date. | |
human_date_format = unix_timestamp_to_string(unix_timestamp) | |
return human_date_format | |
if __name__ == "__main__": | |
# Get the date of the post from the given URL. | |
post_url = "https://www.linkedin.com/post/6782523308840636416" | |
date = get_date(post_url) | |
# Print the date. | |
print(date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment