-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
133 lines (118 loc) · 4.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import util
import json
import requests
from datetime import datetime as dt
URL = 'https://qiita.com/api/v2/items'
HEADERS = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.environ.get("QIITA_AUTH")}'
}
def get_qiita_articles_by_page(page):
'''
第何ページ目の文章を取得
@param page ページ目
@return 文章リストを返す
'''
params = {
'page': page,
'per_page': 100
}
response = requests.get(URL, headers=HEADERS, params=params)
if response.status_code == 200:
articles = response.json()
articles = [article for article in articles if article.get(
'likes_count') > 0 or article.get('stocks_count') > 0]
return articles
return None
def get_qiita_articles():
'''
qiitaの文章を取得
'''
articles = []
for i in range(1, 999):
article = get_qiita_articles_by_page(i)
if article:
articles.extend(article)
else:
break
if articles:
articles = [{
'title': article.get('title'),
'url': article.get('url'),
'likes_count': article.get('likes_count'),
'stocks_count': article.get('stocks_count'),
'tags': ','.join([tag.get('name') for tag in article.get('tags')]),
'created_at': str(dt.fromisoformat(article.get('created_at'))).replace('+09:00', ''),
'user_id': article.get('user').get('id'),
'user_name': article.get('user').get('name'),
'user_url': f'{URL[-12:]}{article.get("user").get("id")}'} for article in articles]
# likes_count,stocks_count降順でソートする
articles = util.sort_articles(articles, 'likes_count', 'stocks_count')
return articles
def out_put_articels(articles, file_name):
'''
qiitaの文章を出力
'''
if articles:
file_name = util.make_path(file_name)
util.write_json_file(articles, file_name)
def update_post(url, article_id, post_content):
'''
記事を更新する
'''
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.environ.get("QIITA_AUTH")}'
}
url = f'{url}/{article_id}'
res = requests.patch(url, headers=headers, json=post_content)
if res.status_code >= 400:
res.raise_for_status()
return res
def start():
articles = get_qiita_articles()
out_put_articels(articles, 'archive/total_result.json')
body = '## はじめ\n ### この記事ではGithubActionとQiita Apiを利用して定期的に自動でQiitaの記事を取得し、いいねとストックで順位を付けて、この記事を更新する。\n ## Github Open ソース \n https://github.com/japan-tools/qiita-job \n ## いいね数とストック数ランキング記事一覧 \n'
articles = [article for article in articles if article.get(
'likes_count') > 50 or article.get('stocks_count') > 50]
for article in articles:
article_title = article.get('title')
article_url = article.get('url')
article_likes_count = article.get('likes_count')
article_stocks_count = article.get('stocks_count')
article_tags = article.get('tags')
article_created_at = article.get('created_at')
body += f"## [{article_title}]({article_url}) \n - いいね数: {article_likes_count} \n - ストック数: {article_stocks_count} \n - タグ: {article_tags} \n - 投稿日: {article_created_at} \n"
post_content = {
'body': body,
"coediting": False,
"private": False,
"tags": [
{
"name": "qiita",
"versions": []
},
{
"name": "QiitaAPI",
"versions": []
},
{
"name": "GitHub",
"versions": []
},
{
"name": "GithubActions",
"versions": []
},
{
"name": "記事まとめ",
"versions": []
}
],
"title": "GithubActionでQiita記事のいいね数とストック数ランキング更新",
"slide": False
}
update_post(URL, 'b22a79d04eae4a401067', post_content)
if __name__ == '__main__':
start()