-
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
taowong
committed
Jan 31, 2024
1 parent
9a7dcf7
commit 3eedd10
Showing
16 changed files
with
1,001 additions
and
33 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,36 @@ | ||
/Algorithm.iml | ||
|
||
HELP.md | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
/out/ |
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,145 @@ | ||
import os | ||
import requests | ||
import argparse | ||
from mdcal import update_calendar | ||
|
||
|
||
def get_issue(issue_number=None): | ||
try: | ||
if issue_number is None: | ||
issue_number = os.getenv("ISSUE_NUMBER") | ||
repo_name = os.getenv("GITHUB_REPOSITORY") | ||
gh_token = os.getenv("GH_TOKEN") | ||
|
||
headers = {"Authorization": f"token {gh_token}"} | ||
api_url = f"https://api.github.com/repos/{repo_name}/issues/{issue_number}" | ||
|
||
response = requests.get(api_url, headers=headers) | ||
issue = response.json() | ||
|
||
print(api_url) | ||
print(headers) | ||
print(issue) | ||
|
||
except Exception as e: | ||
print(f"Error in get_issue: {e}") | ||
return None | ||
|
||
return issue | ||
|
||
|
||
def update_records(issue, issue_number=None): | ||
if issue_number is None: | ||
issue_number = os.getenv("ISSUE_NUMBER") | ||
|
||
issue_title = issue["title"] | ||
issue_labels = ["`" + label["name"] + "`" for label in issue["labels"]] | ||
issue_link = issue["html_url"] | ||
|
||
with open("README.md", "r") as file: | ||
lines = file.readlines() | ||
|
||
table_start_index = None | ||
existing_issue_index = None | ||
|
||
for i in range(len(lines)): | ||
if lines[i].strip() == "|#|Title|Tag|Date|": | ||
table_start_index = i + 2 | ||
if lines[i].strip().startswith(f"|{issue_number}|") and table_start_index: | ||
existing_issue_index = i | ||
if existing_issue_index: | ||
break | ||
|
||
new_line = f"|{issue_number}|[{issue_title}]({issue_link})|{' '.join(issue_labels)}|{issue['created_at']}|\n" | ||
if existing_issue_index is not None: | ||
lines[existing_issue_index] = new_line | ||
else: | ||
lines.insert(table_start_index, new_line) | ||
with open("README.md", "w") as file: | ||
file.writelines(lines) | ||
|
||
return "Successfully updated Records of README.md" | ||
|
||
def update_star(issue): | ||
created_at_str = issue['created_at'] | ||
date_str = created_at_str.split("T")[0] | ||
year, month, day = map(int, date_str.split("-")) | ||
return update_calendar(year, month, day) | ||
|
||
def get_comments(issue_number=None): | ||
try: | ||
if issue_number is None: | ||
issue_number = os.getenv("ISSUE_NUMBER") | ||
|
||
repo_name = os.getenv("GITHUB_REPOSITORY") | ||
gh_token = os.getenv("GH_TOKEN") | ||
|
||
headers = {"Authorization": f"token {gh_token}"} | ||
comments_api_url = f"https://api.github.com/repos/{repo_name}/issues/{issue_number}/comments" | ||
|
||
comments_response = requests.get(comments_api_url, headers=headers) | ||
comments = comments_response.json() | ||
|
||
except Exception as e: | ||
print(f"Error in get_comments: {e}") | ||
return [] | ||
|
||
return comments | ||
|
||
def backup_issue_as_md(issue, issue_number): | ||
try: | ||
if issue_number is None: | ||
issue_number = os.getenv("ISSUE_NUMBER") | ||
|
||
issue_title = issue["title"] | ||
issue_body = issue['body'] | ||
issue_labels = ["`" + label['name'] + "`" for label in issue['labels']] | ||
issue_link = issue['html_url'] | ||
issue_date = issue['created_at'] | ||
|
||
comments = get_comments(issue_number) | ||
|
||
if not os.path.exists("backup"): | ||
os.mkdir("backup") | ||
|
||
with open(f"backup/{issue_number}#{issue_title}.md", "w") as file: | ||
file.write("# " + issue_title + "\n\n") | ||
file.write(issue_body + "\n\n") | ||
file.write("---\n\n") | ||
file.write("* Link: " + issue_link + "\n") | ||
file.write("* Labels: " + ', '.join(issue_labels) + "\n") | ||
file.write("* Creation Date: " + issue_date + "\n") | ||
for i, comment in enumerate(comments, start=1): | ||
file.write(f"\n---\n\n") | ||
file.write(comment['body']) | ||
file.write("\n\n*\n") | ||
|
||
except Exception as e: | ||
print(f"Error in backup_issue_as_md: {e}") | ||
return "Backup failed" | ||
|
||
return "Successfully backup records" | ||
|
||
def main(issue_number): | ||
try: | ||
issue = get_issue(issue_number) | ||
if issue is not None: | ||
print(update_records(issue, issue_number)) | ||
print(update_star(issue)) | ||
print(backup_issue_as_md(issue, issue_number)) | ||
else: | ||
print("Issue could not be retrieved.") | ||
except Exception as e: | ||
print(f"Error in main: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--issue_number", help="issue_number", default=None, required=False | ||
) | ||
args = parser.parse_args() | ||
main(args.issue_number) | ||
except Exception as e: | ||
print(f"Error: {e}") |
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,123 @@ | ||
import os | ||
import re | ||
import calendar | ||
from datetime import datetime | ||
import sys | ||
|
||
def create_calendar(year, month, with_isoweek=False, start_from_Sun=False, lang="en"): | ||
firstweekday = 6 if start_from_Sun else 0 | ||
|
||
cal = calendar.Calendar(firstweekday=firstweekday) | ||
|
||
mdstr = "" | ||
dic = get_dict(lang) | ||
|
||
colnames = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] | ||
if start_from_Sun: | ||
colnames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] | ||
if with_isoweek: | ||
colnames.insert(0, "Week") | ||
colnames = [dic[col] for col in colnames] | ||
|
||
mdstr += '|' + '|'.join(colnames) + '|' + '\n' | ||
mdstr += '|' + '|'.join([':-:' for _ in range(len(colnames))]) + '|' + '\n' | ||
|
||
for days in cal.monthdatescalendar(year, month): | ||
if with_isoweek: | ||
isoweek = days[0].isocalendar()[1] | ||
mdstr += '|' + str(isoweek) + '|' + \ | ||
'|'.join([str(d.day) for d in days]) + '|' + '\n' | ||
else: | ||
mdstr += '|' + '|'.join([str(d.day) for d in days]) + '|' + '\n' | ||
|
||
return mdstr | ||
|
||
|
||
def print_calendar(year, month, with_isoweek=False, start_from_Sun=False, lang="en"): | ||
print('{}/{}\n'.format(year, month)) | ||
print(create_calendar(year, month, with_isoweek, start_from_Sun, lang)) | ||
|
||
|
||
def get_dict(lang='en'): | ||
dic = {} | ||
colnames = ['Week', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] | ||
colnames_ja = ['週', '月', '火', '水', '木', '金', '土', '日'] | ||
if lang == 'en': | ||
for col in colnames: | ||
dic[col] = col | ||
elif lang == 'ja': | ||
for col, colja in zip(colnames, colnames_ja): | ||
dic[col] = colja | ||
else: | ||
for col in colnames: | ||
dic[col] = col | ||
return dic | ||
|
||
def update_calendar(year, month, day, with_isoweek=False, start_from_Sun=False, lang="en"): | ||
# Check if README.md exists, if not, create one | ||
if not os.path.exists('README.md'): | ||
with open('README.md', 'w') as f: | ||
print("The file README.md does not exist. Creating a new one...") | ||
f.write('## 🎯 Calendar\n\n## Records\n\n') | ||
|
||
# Read the content of README.md | ||
with open('README.md', 'r') as file: | ||
content = file.read() | ||
|
||
# Extract the part between "## 🎯 Calendar" to the start of the next section "## Records". | ||
calendar_section_match = re.search("## 🎯 Calendar(.*)(?=## 🍃 Records)", content, re.DOTALL) | ||
|
||
# If "## 🎯 Calendar" section doesn't exist or there is no calendar data | ||
if calendar_section_match is None: | ||
return "The 'Calendar' section does not exist in README.md or there is no calendar data." | ||
|
||
calendar_section = calendar_section_match.group(1) | ||
|
||
# Check if the current month/year already exists in the calendar | ||
current_month_exists = "* {}/{}\n".format(year, month) in calendar_section | ||
calendar_section_lines = ["## 🎯 Calendar\n"] | ||
|
||
if not current_month_exists: | ||
# Create the calendar for the current month/year and append it | ||
cal = create_calendar(year, month, with_isoweek, start_from_Sun, lang) | ||
calendar_section_lines.append('* {}/{}\n'.format(year, month)) | ||
calendar_section_lines += cal.split("\n") | ||
calendar_section_lines.append('\n') | ||
else: | ||
# Append the existing calendar for the current month/year | ||
calendar_section_lines += calendar_section.split("\n")[2:] | ||
|
||
star_flag = True | ||
month_start_flag = False | ||
for i in range(4, len(calendar_section_lines)): | ||
if re.match("^\\|([ ]*.*[ ]*\|)+$", calendar_section_lines[i]): | ||
day_cells = calendar_section_lines[i].split("|") | ||
for j in range(1, len(day_cells) - 1): | ||
digit = re.findall(r'\d+', day_cells[j].strip()) | ||
if len(digit) == 0: | ||
continue | ||
if digit[0] == "1": | ||
month_start_flag = True | ||
if digit[0] == str(day) and "🌟" not in day_cells[j] and star_flag and month_start_flag: | ||
day_cells[j] = day_cells[j].strip() + "🌟" | ||
star_flag = False | ||
calendar_section_lines[i] = "|".join(day_cells) | ||
|
||
# Replace 'Calendar' section in README.md with the updated section | ||
new_content = re.sub(r"## 🎯 Calendar(.*)(?=## 🍃 Records)", "\n".join(calendar_section_lines), content, flags=re.DOTALL) | ||
|
||
with open('README.md', 'w') as file: | ||
file.write(new_content) | ||
|
||
return "Successfully updated Calendar of README.md" | ||
|
||
if __name__ == "__main__": | ||
argv = sys.argv | ||
if len(argv) == 3: | ||
year, month = [int(a) for a in argv[1:3]] | ||
print(update_calendar(year, month, datetime.now().day)) | ||
elif len(argv) == 4: | ||
year, month, day = [int(a) for a in argv[1:4]] | ||
print(update_calendar(year, month, day)) | ||
else: | ||
print('Usage: python mdcal.py [year] [month] [day]') |
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
Oops, something went wrong.