-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a5c64f
commit 3a92a24
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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() |
This file contains 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
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 |