forked from Python-World/python-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
142 lines (129 loc) · 4.21 KB
/
app.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
#!/usr/bin/python
from utils.summarize import summarize
import csv
import shutil
import os
import textwrap
import logging
import argparse
import sys
def parse_args(argv):
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
A command line utility for website summarization.
-----------------------------------------------
These are common commands for this app.'''))
parser.add_argument(
'action',
help='This action should be summarize')
parser.add_argument(
'--url',
help='A link to the website url'
)
parser.add_argument(
'--sentence',
help='Argument to define number of sentence for the summary',
type=int,
default=2)
parser.add_argument(
'--language',
help='Argument to define language of the summary',
default='English')
parser.add_argument(
'--path',
help='path to csv file')
return parser.parse_args(argv[1:])
def readCsv(path):
print('\n\n Processing Csv file \n\n')
sys.stdout.flush()
data = []
try:
with open(path, 'r') as userFile:
userFileReader = csv.reader(userFile)
for row in userFileReader:
data.append(row)
except:
with open(path, 'r', encoding="mbcs") as userFile:
userFileReader = csv.reader(userFile)
for row in userFileReader:
data.append(row)
return data
def writeCsv(data, LANGUAGE, SENTENCES_COUNT):
print('\n\n Updating Csv file \n\n')
sys.stdout.flush()
with open('beneficiary.csv', 'w') as newFile:
newFileWriter = csv.writer(newFile)
length = len(data)
position = data[0].index('website')
for i in range(1, length):
if i == 1:
_data = data[0]
_data.append("summary")
newFileWriter.writerow(_data)
try:
__data = data[i]
summary = summarize(
(data[i][position]), LANGUAGE, SENTENCES_COUNT)
__data.append(summary)
newFileWriter.writerow(__data)
except:
print('\n\n Error Skipping line \n\n')
sys.stdout.flush()
def processCsv(path, LANGUAGE, SENTENCES_COUNT):
try:
print('\n\n Proessing Started \n\n')
sys.stdout.flush()
data = readCsv(path)
writeCsv(data, LANGUAGE, SENTENCES_COUNT)
except:
print('\n\n Invalid file in file path \n\n')
sys.stdout.flush()
def main(argv=sys.argv):
# Configure logging
logging.basicConfig(filename='applog.log',
filemode='w',
level=logging.INFO,
format='%(levelname)s:%(message)s')
args = parse_args(argv)
action = args.action
url = args.url
path = args.path
LANGUAGE = "english" if args.language is None else args.language
SENTENCES_COUNT = 2 if args.sentence is None else args.sentence
if action == 'bulk':
if path is None:
print(
'\n\n Invalid Entry!, please Ensure you enter a valid file path \n\n')
sys.stdout.flush()
return
# guide against errors
try:
processCsv(path, LANGUAGE, SENTENCES_COUNT)
except:
print(
'\n\n Invalid Entry!, please Ensure you enter a valid file path \n\n')
sys.stdout.flush()
print('Completed')
sys.stdout.flush()
if os.path.isfile('beneficiary.csv'):
return shutil.move('beneficiary.csv', path)
return
if action == 'simple':
# guide against errors
try:
summarize(url, LANGUAGE, SENTENCES_COUNT)
except:
print(
'\n\n Invalid Entry!, please Ensure you enter a valid web link \n\n')
sys.stdout.flush()
print('Completed')
sys.stdout.flush()
else:
print(
'\nAction command is not supported\n for help: run python3 app.py -h'
)
sys.stdout.flush()
return
if __name__ == '__main__':
main()