도구/Git

[github] 티스토리 게시글 깃허브에 커밋으로 업로드하기

작은소행성 2021. 7. 19. 17:44

티스토리에 블로그를 작성할 때마다 깃허브에 커밋하면서 블로그 최신 내용을 업로드 하고싶었다. 

 

나는 파이썬 파일에 README 내용을 만들고

새로운 블로그 내용이 있을때마다 README 를 다시 작성해서 커밋 후 배포하는 방법으로 사용했다. 

 

 

먼저 피드를 사용하기 위해 아래 라이브러리를 먼저 설치해준다.

python -m pip install feedparser

 

피드가 잘 설치가 되었는지 아래 예제를 통해 확인한다.

import feedparser

feed = feedparser.parse("https://bsssss.tistory.com/rss") 
feed

 

피드를 불러오면 아래 이미지와 같이 나오는 것을 확인할 수 있다. 

 

 

게시글의 url 과 게시글 제목만 필요하기 때문에 아래 내용을 통해 

url 과 제목만 출력해 보았다. 

for i in feed['entries']:
    print(i['link'], i['title'])

 

글 포스팅 시간도 확인가능하다

feed['entries'][0]['published']

 

다음 데이터 형식을  datetime을 이용해 가공해서 사용도 가능하다

import datetime
datetime.datetime.strptime(feed['entries'][0]['published'], "%a, %d %b %Y %H:%M:%S %z").strftime("%Y.%m.%d %H:%M")

 

 

 

 

아래부분은 파이썬 파일로 작성되어 있는데 

markdown 부분을 html 로 작성해두었기 때문에

블로그 게시물을 링크로 사용하기 위해서 a 태그를 사용했다. 

import feedparser, datetime
 
tistory_blog_uri="https://bsssss.tistory.com"
feed = feedparser.parse(tistory_blog_uri+"/rss")
 
markdown_text = """# Hello, World!
(자기소개)
## Recent blog posts
""" # list of blog posts will be appended here
 
lst = []
 
dt = datetime.datetime.strptime(i['published'], "%a, %d %b %Y %H:%M:%S %z").strftime("%b %d, %Y")
 
for i in feed['entries']:
    dt = datetime.datetime.strptime(i['published'], "%a, %d %b %Y %H:%M:%S %z").strftime("%b %d, %Y")
    markdown_text += f"<a href =\"{i['link']}\"> {i['title']} </a> <br>"
 
f = open("README.md",mode="w", encoding="utf-8")
f.write(markdown_text)
f.close()

 

 

 

 

워크플로우 폴더를 만들고 yml 파일을 만들어준다. 

.github/workflow/main.yml

 

 

github action을 이용해서 코드가 매일 실행될 수 있도록 한다.

# This is a basic workflow to help you get started with Actions
name: Readme Update

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  schedule:
    - cron: "0 0 */1 * *"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: '3.7'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install feedparser
      - name: Update README
        run: |
          python readme_update.py

      - name: Commit README
        run: |
          git pull
          git add .
          git diff
          git config --local user.email "@naver.com"
          git config --local user.name "Bae"
          git commit -m "Update README.md"
          git push

 

 

완성된 내용은 아래 깃에서 확인할 수 있다. 

 

https://github.com/qpyu66

 

qpyu66 - Overview

qpyu66 has 11 repositories available. Follow their code on GitHub.

github.com

 

반응형