forked from acmpesuecc/py-getrfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
170 lines (113 loc) · 4.1 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
import argparse
import requests
from requests.exceptions import HTTPError
from rich.console import Console
from rich.prompt import Prompt
console = Console()
console.print("Hello, Welcome to GetRFC", style="green on black")
RFC = Prompt.ask("Enter the RFC Number", default="15")
console.log(RFC)
page=Prompt.ask("Do you want just the first page?", default="Y")
if page.lower() == 'y':
page='fpage'
else:
page='full'
tofile=Prompt.ask("Do you want to write the output to a file", default="Y")
if tofile.lower() == 'y':
tofile=True
else:
tofile=False
if tofile:
filename = input("Please enter the file name to store RFC at: ")
else:
filename = None
'''
Python script to fetch RFC data from IETF's website using the requests
library and perform one of two actions -
1) Display it in stdout (terminal)
2) Write it to a file specified by this user
Open to collaboration.
'''
class Error(Exception):
''' base class for all errors '''
pass
class Error404(Error):
''' exception class for resources not being found '''
pass
class ForbiddenError(Error):
''' exception class for Forbidden resources '''
pass
def getURL(rfc_no):
# reuturn access URL for a specific RFC no
return f'https://www.ietf.org/rfc/rfc{rfc_no}.txt'
def runRequest(url):
# function to run a request with the requests library
# and return data with the appropriate HTTP codes
try:
response = requests.get(url)
except requests.ConnectionError:
print("Please connect to the internet and try again.")
return 0
if response.status_code == 200:
print('200')
return response
elif response.status_code == 404:
raise Error404
elif response.status_code == 403:
raise ForbiddenError
runRequest((getURL(RFC)))
def getContentFromRFCNo(number, option, tofile, filename):
out = ''
if option == 'full':
try:
data = runRequest(getURL(number))
except Error404:
print("Sorry! The resource you are looking for does not exist!")
return 0
except ForbiddenError:
print("Sorry! It appears that you do not have access to the above URL")
return 0
out = data.text
elif option == 'fpage':
try:
data = runRequest(getURL(number))
except Error404:
print("Sorry! The resource you are looking for does not exist!")
return 0
except ForbiddenError:
print("Sorry! It appears that you do not have access to the above URL")
return 0
iterable_content = data.iter_lines()
buf = ""
for line in iterable_content:
line = line.decode('utf-8')
if '[Page 1]' not in line:
buf += line + '\n'
else:
buf += line + '\n'
break
out = buf
if tofile:
# we have been asked to write the output data to the file
if filename == '' or filename == None:
filename = input("Please enter the file name to store RFC at: ")
with open(filename, 'w+') as current_file:
current_file.write(out)
print(f"Successfully written to file {filename} !")
else:
print(out)
getContentFromRFCNo(RFC, page,tofile, filename)
# if __name__ == '__main__':
# # setup ArgParse for ease of use
# parser = argparse.ArgumentParser(description='Get RFC data from command line ')
# parser.add_argument('rfc', type=int, metavar='RFCno', help='RFC Number that you want to get')
# parser.add_argument('-notall', action="store_true", help='specify if you want only first page')
# parser.add_argument('-tofile', action="store_true", help='Specify if you want to write to a file or just display in shell (default - false)')
# parser.add_argument('-name', help='Name of a file you want to write to')
# args = parser.parse_args()
# #simplifying the last part
# if args.notall == False:
# para = 'full'
# else:
# para = 'fpage'
# getContentFromRFCNo(args.rfc, para, args.tofile, args.name)