Skip to content

Commit

Permalink
randomly selected link
Browse files Browse the repository at this point in the history
  • Loading branch information
kantarcise committed Jan 27, 2025
1 parent 7a5c64f commit 3a92a24
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/scripts/generate_random_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import random
import re

# Base directory containing markdown files
TARGET_DIR = "./Success"
OUTPUT_FILE = "./daily_random_link.md" # File to save the random link

def extract_links_from_file(filepath):
"""Extract Markdown links from a file."""
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
# Extract links in the format [text](url)
return re.findall(r'\[.*?\]\((.*?)\)', content)

def get_all_links(directory):
"""Recursively gather all links from markdown files in the given directory."""
links = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".md"): # Only process .md files
filepath = os.path.join(root, file)
links.extend(extract_links_from_file(filepath))
return links

def main():
# Extract all links from the markdown directory
links = get_all_links(TARGET_DIR)
if not links:
print("No links found in the markdown files.")
return

# Randomly select one link
random_link = random.choice(links)

# Write the selected link to the output file
with open(OUTPUT_FILE, 'w', encoding='utf-8') as file:
file.write(f"Here is a random link from the repository:\n\n{random_link}\n")
print(f"Random link written to {OUTPUT_FILE}")

if __name__ == "__main__":
main()
30 changes: 30 additions & 0 deletions .github/workflows/random_link_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Generate Random Link

on:
schedule:
- cron: '0 12 * * *' # Runs daily at 12:00 UTC (adjust as needed)
workflow_dispatch: # Allows manual triggering

jobs:
generate-link:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Generate Random Link
run: python .github/scripts/generate_random_link.py

- name: Commit and Push Changes
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add random_link.md
git commit -m "Update random link"
git push

0 comments on commit 3a92a24

Please sign in to comment.