質問編集履歴

3

修正したプログラムを反映した

2021/04/13 14:04

投稿

ino_way
ino_way

スコア1

test CHANGED
File without changes
test CHANGED
@@ -20,15 +20,15 @@
20
20
 
21
21
  Traceback (most recent call last):
22
22
 
23
- File "secom2.py", line 62, in <module>
23
+ File "a.py", line 51, in <module>
24
24
 
25
- messages = test.GetMessageList(DateFrom='2018-01-01',DateTo='2021-02-01',MessageFrom='a@gmail.com')
25
+ gmail_get_messages()
26
26
 
27
- File "secom2.py", line 24, in GetMessageList
27
+ File "a", line 26, in gmail_get_messages
28
28
 
29
- service = self.ConnectGmail()
29
+ for msg in msg_list['messages'].split():
30
30
 
31
- AttributeError: 'GmailAPI' object has no attribute 'ConnectGmail'
31
+ AttributeError: 'list' object has no attribute 'split'
32
32
 
33
33
  ```
34
34
 
@@ -40,153 +40,19 @@
40
40
 
41
41
  ```
42
42
 
43
- from __future__ import print_function
43
+ import httplib2, os
44
44
 
45
- from googleapiclient.discovery import build
46
-
47
- from httplib2 import Http
48
-
49
- from oauth2client import file, client, tools
45
+ from apiclient import discovery
50
46
 
51
47
  import gmail # 先ほど作成したプログラム
52
48
 
53
49
 
54
50
 
55
- class GmailAPI:
51
+ import imaplib
56
52
 
57
- def __init__(self):
53
+ import email
58
54
 
59
- # If modifying these scopes, delete the file token.json.
60
-
61
- self._SCOPES = "https://www.googleapis.com/auth/gmail.readonly"
62
-
63
-
64
-
65
- def connect_gmail(self):
66
-
67
- store = file.Storage("C:\Users\a\Desktop\gmail\client_secret.json")
68
-
69
- creds = store.get()
70
-
71
- if not creds or creds.invalid:
72
-
73
- flow = client.flow_from_clientsecrets("C:\Users\a\Desktop\gmail\credentials-gmail.json", self._SCOPES)
74
-
75
- creds = tools.run_flow(flow, store)
76
-
77
- service = build("gmail", "v1", http=creds.authorize(Http()))
78
-
79
-
80
-
81
- return service
82
-
83
- def GetMessageList(self, DateFrom,DateTo,MessageFrom):
84
-
85
-
86
-
87
- #APIに接続
88
-
89
- service = self.ConnectGmail()
90
-
91
-
92
-
93
- MessageList = []
94
-
95
-
96
-
97
- query = ''
98
-
99
- # 検索用クエリを指定する
100
-
101
- if DateFrom != None and DateFrom !="":
102
-
103
- query += 'after:' + DateFrom + ' '
104
-
105
- if DateTo != None and DateTo !="":
106
-
107
- query += 'before:' + DateTo + ' '
108
-
109
- if MessageFrom != None and MessageFrom !="":
110
-
111
- query += 'From:' + MessageFrom + ' '
112
-
113
-
114
-
115
- # メールIDの一覧を取得する(最大100件)
116
-
117
- messageIDlist = service.users().messages().list(userId='me',maxResults=100,q=query).execute()
118
-
119
- #該当するメールが存在しない場合は、処理中断
120
-
121
- if messageIDlist['resultSizeEstimate'] == 0:
122
-
123
- print("Message is not found")
124
-
125
- return MessageList
126
-
127
- #メッセージIDを元に、メールの詳細情報を取得
128
-
129
- for message in messageIDlist['messages']:
130
-
131
- row = {}
132
-
133
- row['ID'] = message['id']
134
-
135
- MessageDetail = service.users().messages().get(userId='me',id=message['id']).execute()
136
-
137
- for header in MessageDetail['payload']['headers']:
138
-
139
- #日付、送信元、件名を取得する
140
-
141
- if header['name'] == 'Date':
142
-
143
- row['Date'] = header['value']
144
-
145
- elif header['name'] == 'From':
146
-
147
- row['From'] = header['value']
148
-
149
- elif header['name'] == 'Subject':
150
-
151
- row['Subject'] = header['value']
152
-
153
- MessageList.append(row)
154
-
155
- return MessageList
156
-
157
-
158
-
159
- if __name__ == '__main__':
160
-
161
- test = GmailAPI()
162
-
163
- #パラメータは、任意の値を指定する
164
-
165
- messages = test.GetMessageList(DateFrom='2018-01-01',DateTo='2021-02-01',MessageFrom='a@gmail.com')
166
-
167
- #結果を出力
168
-
169
- for message in messages:
170
-
171
- print(message)
172
-
173
-
174
-
175
- ```
176
-
177
-
178
-
179
- ### エラーなしのソースコード
180
-
181
-
182
-
183
- ```Python
184
-
185
- import httplib2, os
55
+ import re
186
-
187
- from apiclient import discovery
188
-
189
- import gmail # 先ほど作成したプログラム
190
56
 
191
57
 
192
58
 
@@ -224,23 +90,63 @@
224
90
 
225
91
  # 取得したメッセージの一覧を表示
226
92
 
227
- for msg in msg_list['messages']:
93
+ for msg in msg_list['messages'].split():
228
94
 
229
95
  topid = msg['id']
230
96
 
231
97
  msg = messages.get(userId='me', id=topid).execute()
232
98
 
233
- print("---")
99
+
234
100
 
235
- print(msg['snippet']) # 要約表示
101
+ #URL取得
236
102
 
237
-
103
+ len=[]
104
+
105
+ lists=msg_list.split("\n")
106
+
107
+
108
+
109
+ for list in lists:
110
+
111
+ if list.find('http://') >-1 or list.find('https://') > -1:
112
+
113
+ len.append(list)
114
+
115
+
116
+
117
+ httpurl=[]
118
+
119
+ for list in len:
120
+
121
+ top=list.find('http')
122
+
123
+ list=list[top:]
124
+
125
+ a=re.search('[\sあ-んア-ン]',list)
126
+
127
+ start=a.start()
128
+
129
+ list=list[:start]
130
+
131
+ httpurl.append(list)
132
+
133
+
134
+
135
+ print("---")
136
+
137
+ print(httpurl)
138
+
139
+
238
140
 
239
141
  # メッセージの取得を実行
240
142
 
241
143
  gmail_get_messages()
242
144
 
145
+
146
+
243
147
  ```
148
+
149
+
244
150
 
245
151
 
246
152
 
@@ -248,19 +154,11 @@
248
154
 
249
155
 
250
156
 
251
- re.search
157
+ for msg in msg_list['messages'].split():
252
158
 
253
- 恐らく、取得したメッセージの情報が文字列ではなかったことが原因でエラーが出ました。
159
+
254
160
 
255
-
256
-
257
- ②query
258
-
259
- 件名で絞ってメッセージを取得しようとしましたがエラーが出ました。
161
+ for msg in msg_list[0].split():
260
-
261
-
262
-
263
-
264
162
 
265
163
 
266
164
 

2

エラーメッセージの追加

2021/04/13 14:04

投稿

ino_way
ino_way

スコア1

test CHANGED
File without changes
test CHANGED
@@ -18,13 +18,165 @@
18
18
 
19
19
  ```
20
20
 
21
+ Traceback (most recent call last):
22
+
23
+ File "secom2.py", line 62, in <module>
24
+
25
+ messages = test.GetMessageList(DateFrom='2018-01-01',DateTo='2021-02-01',MessageFrom='a@gmail.com')
26
+
27
+ File "secom2.py", line 24, in GetMessageList
28
+
21
- エラーメッセージ
29
+ service = self.ConnectGmail()
30
+
22
-
31
+ AttributeError: 'GmailAPI' object has no attribute 'ConnectGmail'
32
+
23
- ```
33
+ ```
34
+
35
+
36
+
24
-
37
+ ###エラーありのソースコード
38
+
39
+
40
+
25
-
41
+ ```
42
+
26
-
43
+ from __future__ import print_function
44
+
45
+ from googleapiclient.discovery import build
46
+
47
+ from httplib2 import Http
48
+
49
+ from oauth2client import file, client, tools
50
+
51
+ import gmail # 先ほど作成したプログラム
52
+
53
+
54
+
55
+ class GmailAPI:
56
+
57
+ def __init__(self):
58
+
59
+ # If modifying these scopes, delete the file token.json.
60
+
61
+ self._SCOPES = "https://www.googleapis.com/auth/gmail.readonly"
62
+
63
+
64
+
65
+ def connect_gmail(self):
66
+
67
+ store = file.Storage("C:\Users\a\Desktop\gmail\client_secret.json")
68
+
69
+ creds = store.get()
70
+
71
+ if not creds or creds.invalid:
72
+
73
+ flow = client.flow_from_clientsecrets("C:\Users\a\Desktop\gmail\credentials-gmail.json", self._SCOPES)
74
+
75
+ creds = tools.run_flow(flow, store)
76
+
77
+ service = build("gmail", "v1", http=creds.authorize(Http()))
78
+
79
+
80
+
81
+ return service
82
+
83
+ def GetMessageList(self, DateFrom,DateTo,MessageFrom):
84
+
85
+
86
+
87
+ #APIに接続
88
+
89
+ service = self.ConnectGmail()
90
+
91
+
92
+
93
+ MessageList = []
94
+
95
+
96
+
97
+ query = ''
98
+
99
+ # 検索用クエリを指定する
100
+
101
+ if DateFrom != None and DateFrom !="":
102
+
103
+ query += 'after:' + DateFrom + ' '
104
+
105
+ if DateTo != None and DateTo !="":
106
+
107
+ query += 'before:' + DateTo + ' '
108
+
109
+ if MessageFrom != None and MessageFrom !="":
110
+
111
+ query += 'From:' + MessageFrom + ' '
112
+
113
+
114
+
115
+ # メールIDの一覧を取得する(最大100件)
116
+
117
+ messageIDlist = service.users().messages().list(userId='me',maxResults=100,q=query).execute()
118
+
119
+ #該当するメールが存在しない場合は、処理中断
120
+
121
+ if messageIDlist['resultSizeEstimate'] == 0:
122
+
123
+ print("Message is not found")
124
+
125
+ return MessageList
126
+
127
+ #メッセージIDを元に、メールの詳細情報を取得
128
+
129
+ for message in messageIDlist['messages']:
130
+
131
+ row = {}
132
+
133
+ row['ID'] = message['id']
134
+
135
+ MessageDetail = service.users().messages().get(userId='me',id=message['id']).execute()
136
+
137
+ for header in MessageDetail['payload']['headers']:
138
+
139
+ #日付、送信元、件名を取得する
140
+
141
+ if header['name'] == 'Date':
142
+
143
+ row['Date'] = header['value']
144
+
145
+ elif header['name'] == 'From':
146
+
147
+ row['From'] = header['value']
148
+
149
+ elif header['name'] == 'Subject':
150
+
151
+ row['Subject'] = header['value']
152
+
153
+ MessageList.append(row)
154
+
155
+ return MessageList
156
+
157
+
158
+
159
+ if __name__ == '__main__':
160
+
161
+ test = GmailAPI()
162
+
163
+ #パラメータは、任意の値を指定する
164
+
165
+ messages = test.GetMessageList(DateFrom='2018-01-01',DateTo='2021-02-01',MessageFrom='a@gmail.com')
166
+
167
+ #結果を出力
168
+
169
+ for message in messages:
170
+
171
+ print(message)
172
+
173
+
174
+
175
+ ```
176
+
177
+
178
+
27
- ### 該当のソースコード
179
+ ### エラーなしのソースコード
28
180
 
29
181
 
30
182
 

1

タイトルの改善

2021/04/12 12:55

投稿

ino_way
ino_way

スコア1

test CHANGED
@@ -1 +1 @@
1
- GmailのURL取得
1
+ Gmail受信ボックス内特定のメールのURL取得
test CHANGED
File without changes