-
Notifications
You must be signed in to change notification settings - Fork 5
/
smmrycli.py
156 lines (104 loc) · 3.93 KB
/
smmrycli.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import pandas as pd
import argparse
import pathlib
import csv
import os
from urllib.parse import urlparse
from smmryapi import SmmryAPI
from smmryapi import SmmryAPIException
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('key', help="Input your SMMRY API key.")
parser.add_argument('path', help="Path to you file containing URLs.")
parser.add_argument('-l', '--length', type=int, default=7,
help="Select the sentence length for the summaries.")
parser.add_argument('-k', '--keywords', type=int, default=3,
help="Select the number of keywords to return.")
parser.add_argument('-b', '--with_break', action='store_true',
help="Select to include [BREAK] (default False).")
parser.add_argument('-r', '--replace_break', type=str,
help="If -b is selected, select what you want to replace [BREAK] with.")
parser.add_argument('-q', '--quote_avoid', action='store_true',
help="Select if you want to avoid quotes in your summary.")
return parser.parse_args()
def parse_input_file(path):
abspath = os.path.abspath(path)
_, ext = os.path.splitext(abspath)
name = os.path.basename(path).split('.')[0]
if ext == '.xlsx' or ext == '.xls':
df = pd.read_excel(
abspath,
cols='A',
header=None
)
urls = df.iloc[:, 0].tolist()
elif ext == '.txt' or ext == '.csv':
urls = open(abspath, 'r').read().splitlines()
else:
raise Exception("URLs must be in a single-column .txt, .csv, .xlsx, or .xls file.")
if len(urls) == 0:
raise Exception("File %s does not contain any URLs." % path)
return urls, name
def get_output_filename(name):
path = pathlib.PurePath(os.getcwd())
export_path = path / 'exports' / (name + '-summaries.csv')
return str(export_path)
def validate_url(urls):
good_urls = []
for url in urls:
result = urlparse(url)
if len(result.netloc) == 0:
print("Invalid url: %s" % url)
else:
good_urls.append(url)
if len(good_urls) == 0:
raise Exception("File does not contain any working URLs.")
return good_urls
def main():
args = get_arguments()
urls, name = parse_input_file(args.path)
output_file = get_output_filename(name)
with open(output_file, 'w', newline="", encoding='utf-8') as file:
writer = csv.writer(file, dialect='excel')
writer.writerow([
'url',
'summary',
'length',
'title',
'reduction',
'keyword_count',
'keyword_array'
])
smmry = SmmryAPI(args.key)
requests_remaining = ''
total_urls = len(urls)
successful_urls = 0
good_urls = validate_url(urls)
for url in good_urls:
try:
s = smmry.summarize(
url,
sm_length=args.length,
sm_keyword_count=args.keywords,
sm_with_break=args.with_break,
sm_break_with=args.replace_break
)
successful_urls += 1
writer.writerow([
url,
s.sm_api_content,
s.sm_length,
s.sm_api_title,
s.sm_api_content_reduced,
args.keywords,
s.sm_api_keyword_array
])
requests_remaining = s.sm_requests_remaining
except SmmryAPIException as e:
print(e, url)
print("\nSuccess! A total of %s out of %s summaries have been retrieved.\n"
% (successful_urls, total_urls))
print("You have %s requests remaining for today."
% requests_remaining)
if __name__ == '__main__':
main()