-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalis_gourmet_map_test2.py
More file actions
116 lines (89 loc) · 5.16 KB
/
alis_gourmet_map_test2.py
File metadata and controls
116 lines (89 loc) · 5.16 KB
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
import urllib
import json
import sys
import codecs
import urllib.request
import pprint
import datetime
import time
import requests
import gspread
from oauth2client.service_account import ServiceAccountCredentials
api_tag = 'https://alis.to/api/search/articles?tag=ALIS%E3%82%B0%E3%83%AB%E3%83%A1%E4%BC%81%E7%94%BB'
url_tag = urllib.request.urlopen(api_tag)
article_tags = json.loads(url_tag.read().decode("utf-8"))
article_ids = [article_tag.get('article_id') for article_tag in article_tags]
#article idを取り出す
user_ids = [article_tag.get('user_id') for article_tag in article_tags]
#user id を取り出す
api_article_ids = ["https://alis.to/api/articles/"+article_id for article_id in article_ids]
#article_idをAPIで取り出しやすい形にする
user_id_info_apis = ["https://alis.to/api/users/"+user_id+"/info" for user_id in user_ids]
#user_id_infoをAPIで取り出しやすい形にする
user_id_infos = [json.loads(urllib.request.urlopen(user_id_info_api).read().decode("utf-8")) for user_id_info_api in user_id_info_apis]
#/users/{user_id}/infoを取り出す
user_display_names = [user_id_info.get('user_display_name') for user_id_info in user_id_infos]
#user_display_name を取り出す
likes_apis = ["https://alis.to/api/articles/"+article_id+"/likes" for article_id in article_ids]
#likes_apiをAPIで取り出しやすい形にする
likes = [json.loads(urllib.request.urlopen(likes_api).read().decode("utf-8")) for likes_api in likes_apis]
likes_counts = [like.get('count') for like in likes]
#Like数 を取り出す
articles_bodys = [json.loads(urllib.request.urlopen(api_article_id).read().decode("utf-8")) for api_article_id in api_article_ids]
def get_block(text, start_text, end_text):
if not text.find(start_text) >= 0:
return []
new_texts = []
for split_text in text.split(start_text):
if split_text.find(end_text) >= 0:
new_texts.append(split_text.split(end_text)[0])
return new_texts
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
#googleスプレッドシートに書き込む準備をする
credentials = ServiceAccountCredentials.from_json_keyfile_name('gspread-sample-31b5a8f7ef13.json', scope)
#jsonファイル
gc = gspread.authorize(credentials)
workbook = gc.open_by_key('1u3J0zY9CXc4zHPtcrWDeGNkazFaCPVmObpu3CWVVvXA')
#googleスプレッドシートを指定する
worksheet = workbook.sheet1
#sheet1を選択する
for i, articles_body in enumerate(articles_bodys):
texts = get_block(articles_body.get("body"), "<blockquote>", "</blockquote>")
texts = [text for text in texts if text.find("〒") >= 0]
project_names = get_block("".join(texts), "企画名", "<br>")
project_names = [name.replace(":", "").replace(":", "").replace("</blockquote><p>", "") for name in project_names ]
worksheet.update_cell(i+2, 1, project_names[-1])
#スプレッドシートのA列に企画名を書き込む
for i, articles_body in enumerate(articles_bodys):
texts = get_block(articles_body.get("body"), "<blockquote>", "</blockquote>")
texts = [text for text in texts if text.find("〒") >= 0]
food_genre = get_block("".join(texts), "料理ジャンル", "<br>")
food_genre = [name.replace(":", "").replace(":", "").replace("</blockquote><p>", "") for name in food_genre ]
worksheet.update_cell(i+2, 2, food_genre[-1])
#スプレッドシートのB列に記事URLを書き込む
for i, articles_body in enumerate(articles_bodys):
texts = get_block(articles_body.get("body"), "<blockquote>", "</blockquote>")
texts = [text for text in texts if text.find("〒") >= 0]
store_names = get_block("".join(texts), "店名", "<br>")
store_names = [name.replace(":", "").replace(":", "").replace("</blockquote><p>", "") for name in store_names ]
worksheet.update_cell(i+2, 3, store_names[-1])
#スプレッドシートのC列に店名を書き込む
for i, articles_body in enumerate(articles_bodys):
texts = get_block(articles_body.get("body"), "<blockquote>", "</blockquote>")
texts = [text for text in texts if text.find("〒") >= 0]
locations = get_block(articles_body.get("body"), "住所:","<br>")
locations = [location for location in locations if location.find("〒") >= 0]
locations = [location.replace(":", "").replace(":", "").replace("</blockquote><p>", "") for location in locations ]
worksheet.update_cell(i+2, 4, locations[-1])
#スプレッドシートのD列に住所を書き込む
for i, (article_id, user_id) in enumerate(zip(article_ids, user_ids)):
article_url="https://alis.to/"+str(user_id) +"/articles/"+ str(article_id)
worksheet.update_cell(i+2, 5, article_url)
#スプレッドシートのE列に記事URLを書き込む
for i, user_display_name in enumerate(user_display_names):
worksheet.update_cell(i+2, 6, user_display_name)
#スプレッドシートのF列にユーザー名を書き込む
for i, likes_count in enumerate(likes_counts):
worksheet.update_cell(i+2, 7, likes_count)
#スプレッドシートのG列にLike数を書き込む