Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
UniqueClouds authored Jan 3, 2024
1 parent b9f86c9 commit eb910e9
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import os
import re

from marko.ext.gfm import gfm as marko
from github import Github
import markdown
from feedgen.feed import FeedGenerator
from github import Github
from lxml.etree import CDATA
from marko.ext.gfm import gfm as marko

MD_HEAD = """## Gitblog
### *Less is more.*
My personal blog using issues and GitHub Actions (随意转载,无需署名)
[RSS Feed](https://raw.githubusercontent.com/{repo_name}/master/feed.xml)
"""
Expand Down Expand Up @@ -153,17 +153,24 @@ def add_md_top(repo, md, me):
def add_md_firends(repo, md, me):
s = FRIENDS_TABLE_HEAD
friends_issues = list(repo.get_issues(labels=FRIENDS_LABELS))
if not FRIENDS_LABELS or not friends_issues:
return
friends_issue_number = friends_issues[0].number
for issue in friends_issues:
for comment in issue.get_comments():
if is_hearted_by_me(comment, me):
try:
s += _make_friend_table_string(comment.body)
s += _make_friend_table_string(comment.body or "")
except Exception as e:
print(str(e))
pass
s = markdown.markdown(s, output_format="html", extensions=["extra"])
with open(md, "a+", encoding="utf-8") as md:
md.write("## 友情链接\n")
md.write(
f"## [友情链接](https://github.com/{str(me)}/gitblog/issues/{friends_issue_number})\n"
)
md.write(s)
md.write("\n\n")


def add_md_recent(repo, md, me, limit=5):
Expand All @@ -178,20 +185,33 @@ def add_md_recent(repo, md, me, limit=5):
count += 1
if count >= limit:
break
except:
return
except Exception as e:
print(str(e))


def add_md_header(md, repo_name):
with open(md, "w", encoding="utf-8") as md:
md.write(MD_HEAD.format(repo_name=repo_name))
md.write("\n")


def add_md_label(repo, md, me):
labels = get_repo_labels(repo)

# sort lables by description info if it exists, otherwise sort by name,
# for example, we can let the description start with a number (1#Java, 2#Docker, 3#K8s, etc.)
labels = sorted(
labels,
key=lambda x: (
x.description is None,
x.description == "",
x.description,
x.name,
),
)

with open(md, "a+", encoding="utf-8") as md:
for label in labels:

# we don't need add top label again
if label.name in IGNORE_LABELS:
continue
Expand Down Expand Up @@ -276,16 +296,16 @@ def main(token, repo_name, issue_number=None, dir_name=BACKUP_DIR):

def save_issue(issue, me, dir_name=BACKUP_DIR):
md_name = os.path.join(
dir_name, f"{issue.number}_{issue.title.replace(' ', '.')}.md"
dir_name, f"{issue.number}_{issue.title.replace('/', '-').replace(' ', '.')}.md"
)
with open(md_name, "w") as f:
f.write(f"# [{issue.title}]({issue.html_url})\n\n")
f.write(issue.body)
f.write(issue.body or "")
if issue.comments:
for c in issue.get_comments():
if is_me(c, me):
f.write("\n\n---\n\n")
f.write(c.body)
f.write(c.body or "")


if __name__ == "__main__":
Expand Down

0 comments on commit eb910e9

Please sign in to comment.