김건

2차수정 사항 업로드

1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 from __future__ import print_function 3 from __future__ import print_function
4 -
5 -import os
6 import sys 4 import sys
5 +import os
7 import time 6 import time
8 import json 7 import json
9 import requests 8 import requests
...@@ -58,7 +57,6 @@ def ajax_request(session, url, params, data, retries=10, sleep=20): ...@@ -58,7 +57,6 @@ def ajax_request(session, url, params, data, retries=10, sleep=20):
58 def download_comments(youtube_id, sleep=1): 57 def download_comments(youtube_id, sleep=1):
59 session = requests.Session() 58 session = requests.Session()
60 session.headers['User-Agent'] = USER_AGENT 59 session.headers['User-Agent'] = USER_AGENT
61 -
62 # Get Youtube page with initial comments 60 # Get Youtube page with initial comments
63 response = session.get(YOUTUBE_COMMENTS_URL.format(youtube_id=youtube_id)) 61 response = session.get(YOUTUBE_COMMENTS_URL.format(youtube_id=youtube_id))
64 html = response.text 62 html = response.text
...@@ -68,10 +66,8 @@ def download_comments(youtube_id, sleep=1): ...@@ -68,10 +66,8 @@ def download_comments(youtube_id, sleep=1):
68 for comment in extract_comments(html): 66 for comment in extract_comments(html):
69 ret_cids.append(comment['cid']) 67 ret_cids.append(comment['cid'])
70 yield comment 68 yield comment
71 -
72 page_token = find_value(html, 'data-token') 69 page_token = find_value(html, 'data-token')
73 session_token = find_value(html, 'XSRF_TOKEN', 4) 70 session_token = find_value(html, 'XSRF_TOKEN', 4)
74 -
75 first_iteration = True 71 first_iteration = True
76 72
77 # Get remaining comments (the same as pressing the 'Show more' button) 73 # Get remaining comments (the same as pressing the 'Show more' button)
...@@ -102,19 +98,16 @@ def download_comments(youtube_id, sleep=1): ...@@ -102,19 +98,16 @@ def download_comments(youtube_id, sleep=1):
102 98
103 first_iteration = False 99 first_iteration = False
104 time.sleep(sleep) 100 time.sleep(sleep)
105 -
106 # Get replies (the same as pressing the 'View all X replies' link) 101 # Get replies (the same as pressing the 'View all X replies' link)
107 for cid in reply_cids: 102 for cid in reply_cids:
108 data = {'comment_id': cid, 103 data = {'comment_id': cid,
109 'video_id': youtube_id, 104 'video_id': youtube_id,
110 'can_reply': 1, 105 'can_reply': 1,
111 'session_token': session_token} 106 'session_token': session_token}
112 -
113 params = {'action_load_replies': 1, 107 params = {'action_load_replies': 1,
114 'order_by_time': True, 108 'order_by_time': True,
115 'filter': youtube_id, 109 'filter': youtube_id,
116 'tab': 'inbox'} 110 'tab': 'inbox'}
117 -
118 response = ajax_request(session, YOUTUBE_COMMENTS_AJAX_URL, params, data) 111 response = ajax_request(session, YOUTUBE_COMMENTS_AJAX_URL, params, data)
119 if not response: 112 if not response:
120 break 113 break
...@@ -128,36 +121,71 @@ def download_comments(youtube_id, sleep=1): ...@@ -128,36 +121,71 @@ def download_comments(youtube_id, sleep=1):
128 time.sleep(sleep) 121 time.sleep(sleep)
129 122
130 123
131 -def main(argv):
132 - parser = argparse.ArgumentParser(add_help=False, description=('Download Youtube comments without using the Youtube API'))
133 - parser.add_argument('--help', '-h', action='help', default=argparse.SUPPRESS, help='Show this help message and exit')
134 - parser.add_argument('--youtubeid', '-y', help='ID of Youtube video for which to download the comments')
135 - parser.add_argument('--output', '-o', help='Output filename (output format is line delimited JSON)')
136 - parser.add_argument('--limit', '-l', type=int, help='Limit the number of comments')
137 124
138 - try: 125 +def goto_Menu(result_List) :
139 - args = parser.parse_args(argv) 126 + for i in range (len(result_List)) :
127 + a = str(result_List[i]['text'])
128 + if('상무' in a) :
129 + print((result_List)[i]['text'])
130 + else :
131 + print('Not Found')
140 132
141 - youtube_id = args.youtubeid 133 +def main():
142 - output = args.output
143 - limit = args.limit
144 134
135 + #parser = argparse.ArgumentParser(add_help=False, description=('Download Youtube comments without using the Youtube API'))
136 + #parser.add_argument('--help', '-h', action='help', default=argparse.SUPPRESS, help='Show this help message and exit')
137 + #parser.add_argument('--youtubeid', '-y', help='ID of Youtube video for which to download the comments')
138 + #parser.add_argument('--output', '-o', help='Output filename (output format is line delimited JSON)')
139 + #parser.add_argument('--limit', '-l', type=int, help='Limit the number of comments')
140 + Youtube_id1 = input('Youtube_ID 입력 :')
141 + Output1 = input('결과를 받을 파일 입력 :')
142 + Limit1 = input('제한 갯수 입력 : ')
143 + ##### argument로 받지 않고 input으로 받기 위한 것
144 + try:
145 + # args = parser.parse_args(argv)
146 +
147 + #youtube_id = args.youtubeid
148 + #output = args.output
149 + #limit = args.limit
150 + result_List =[]
151 + youtube_id = Youtube_id1
152 + output = Output1
153 + ## input 값을 받고 값에 할당
154 +
155 + if Limit1 == '' :
156 + Limit1 = 100
157 + Limit1 = int(Limit1)
158 + limit = Limit1
159 + ## Limit에 빈 값이 들어갈 경우 Default 값으로 100을 넣게 하였음
145 if not youtube_id or not output: 160 if not youtube_id or not output:
146 - parser.print_usage() 161 + #parser.print_usage()
147 - raise ValueError('you need to specify a Youtube ID and an output filename') 162 + #raise ValueError('you need to specify a Youtube ID and an output filename')
163 + raise ValueError('올바른 입력 값을 입력하세요')
148 164
149 print('Downloading Youtube comments for video:', youtube_id) 165 print('Downloading Youtube comments for video:', youtube_id)
150 count = 0 166 count = 0
151 - with io.open(output, 'w', encoding='utf8') as fp: 167 + Number = input(' 저장 - 0 저장 안함- 1 : ')
168 + if Number == '0' :
169 + with io.open(output, 'w', encoding='utf8') as fp:
170 + for comment in download_comments(youtube_id):
171 + print(comment_json.decode('utf-8') if isinstance(comment_json, bytes) else comment_json, file=fp)
172 + count += 1
173 + sys.stdout.write('Downloaded %d comment(s)\r' % count)
174 + sys.stdout.flush()
175 + if limit and count >= limit:
176 + break
177 + print('\nDone!')
178 + else :
179 + i = 0
152 for comment in download_comments(youtube_id): 180 for comment in download_comments(youtube_id):
153 - comment_json = json.dumps(comment, ensure_ascii=False) 181 + result_List.append(comment)
154 - print(comment_json.decode('utf-8') if isinstance(comment_json, bytes) else comment_json, file=fp)
155 count += 1 182 count += 1
156 - sys.stdout.write('Downloaded %d comment(s)\r' % count) 183 + i += 1
157 - sys.stdout.flush()
158 if limit and count >= limit: 184 if limit and count >= limit:
159 break 185 break
160 - print('\nDone!') 186 + print('\nDone!')
187 + goto_Menu(result_List)
188 +
161 189
162 190
163 except Exception as e: 191 except Exception as e:
...@@ -166,4 +194,4 @@ def main(argv): ...@@ -166,4 +194,4 @@ def main(argv):
166 194
167 195
168 if __name__ == "__main__": 196 if __name__ == "__main__":
169 - main(sys.argv[1:]) 197 + main()
......