GithubHelp home page GithubHelp logo

chairco / dj-hackmd-notifier Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 39 KB

Notification the change with hackmd.io document on monitor

License: ISC License

Python 89.00% HTML 11.00%
django hackmd django-q diff notification

dj-hackmd-notifier's Introduction

dj-Hackmd-notifier

Django + Django-Q 開發,用來建立排程與比對 Hackmd.io 上的網頁文件內容的線上工具。

環境設定

建議採用 Pipenv 做套件管理,Pipenv 是一個出色的 Python 工具管理套件,類似其他程式語言裡常見的:bundler, composer, npm, cargo, yarn, 等

如果對它還不熟悉,這裡有一篇簡單的 Pipenv 使用教學

pipenv

建立環境:

pipenv --python 3.6.3

安裝必要套件:

pipenv install --dev

進入虛擬環境:

pipenv shell

py3 內建虛擬環境

python3 -m venv env

進入虛擬環境:

source source env/bin/activate 

安裝必要套件:

pip install -r requirements.txt

Django 環境設定

使用 python-dotenv 管理環境變數,首先需要在 settings/ 目錄下先建立 local.env 檔案。

cp dj-Hackmd-notifier/src/src/settings/local_sample.env dj-Hackmd-notifier/src/src/settings/local.env 

接著設定環境需要參數:

  • EMAIL_HOST_USER={} 發送信件 gmail 帳號。
  • EMAIL_HOST_PASSWORD={} 發送信件 gmail 密碼。
  • DATABASE_URL='postgres://localhost/hackmds' 預設採用 postgresql 資料庫,可根據需求修改(sqlite3 目前測試會有問題)。
  • SECRET_KEY={} Django 需要的 secret key。可到這邊取得

因為 Google 目前針對帳號安全開啟二階段認證,因此如果需要透過第三方程式發送 gmail 信件,請先至後台關閉二階段認證並且允許第三方程式存取帳戶。 注意: 這樣修改可能會有資安危險。

新增 logs 資料夾, 專案所有產生的 log file 都會存放在此

cd dj-Hackmd-notifier/src
mkdir -p logs

建立資料庫:

注意,目前資料庫預設 Postgresql 請先在本機端預設安裝,並且建立對應的 table。安裝完成後再執行 migrate 指令。

python manage.py migrate

建立 admin 帳號(請注意一定要輸入信箱,因為信件會發送到此):

python manage.py createsuperuser

啟動伺服器:

python manage.py runserver

如果可以順利連到 localhost:8000/admin 恭喜已經完成一半了。

排程設定

一次一個網頁

先進入 後台網頁,並用剛剛建立的 admin 帳號與密碼登入。

Imgur

點選 Scheduled tasks 旁邊的新增排程。

  • Name 隨意輸入,不要太複雜。
  • Func 執行任務函式,src 是 Django project 名稱, tasks 是檔案名稱, hackmd_task 為函式名稱。
  • Hook 任務結束時會印出 stdout 的結果。目前還沒印出具體訊息。
  • Kwargs 函式需要的參數, 在這裡只需要 url。注意因為比對是以 url 作為 key 儲存在 db 因此請以 hackmd.io 發表的網址,例如: url='https://hackmd.io/s/ByIn4AYaZ', 一定要帶上 https:// 程式未來可為這部分自動偵測。
  • Schedule type 多久運作一次。
  • Repeats 設定 -1 代表永遠。
  • Next Run 點選現在。

最後按儲存完成。

Imgur

Imgur

一次多組網頁

感謝 @mlouielu 提醒。可能會有一次要抓取多個網頁(根據 hw 別)這邊新增一個 hackmd_taskchain 函數來處理。

  • Name 隨意輸入。
  • Func 執行任務函式,src 是 Django project 名稱, tasks 是檔案名稱, hackmd_taskchain 為函式名稱。
  • Hook 任務結束時會印出 stdout 的結果。目前還沒印出具體訊息。
  • Kwargs 函式需要的參數, 在這裡輸入多個網址,例如: urls='https://hackmd.io/s/H1fSXCcpb, https://hackmd.io/s/H1fSXCcp2#1234' 程式會幫忙處理前後空白問題。注意要帶上 https://
  • Schedule type 多久運作一次。
  • Repeats 設定 -1 代表永遠。
  • Next Run 點選現在。

Imgur

Imgur

啟動 Django-Q instance

開啟一個終端機執行 Django-Q:

python manage.py qcluster

看到下面資訊就代表運作成功,接著稍等一下就會開始執行排程。

21:38:27 [Q] INFO Q Cluster-9144 starting.
21:38:27 [Q] INFO Process-1:1 ready for work at 9146
21:38:27 [Q] INFO Process-1:2 ready for work at 9147
21:38:27 [Q] INFO Process-1:3 ready for work at 9148
21:38:27 [Q] INFO Process-1:4 ready for work at 9149
21:38:27 [Q] INFO Process-1:5 monitoring at 9150
21:38:27 [Q] INFO Process-1 guarding cluster at 9145
21:38:27 [Q] INFO Process-1:6 pushing tasks at 9151
21:38:27 [Q] INFO Q Cluster-9144 running.

如果有超過 2 處的修改就會收到信件如:

Imgur

Imgur

參考

本專案同時參考 @uranusjr 的 diffhtml 專案在此感謝。

dj-hackmd-notifier's People

Contributors

chairco avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dj-hackmd-notifier's Issues

前端頁面

目前會儲存每一次差異到後端結果。可以寫一個前端把這些資料抓出來看歷程。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.