This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
totext.py
executable file
·291 lines (267 loc) · 11.1 KB
/
totext.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python2
# Author: Remy van Elst <raymii.org>
# License: GNU GPLv2
# pip install html2text requests readability-lxml feedparser lxml
import requests
import lxml
from readability import Document
import html2text
import argparse
import feedparser
import sys
import time
import re
import os
from datetime import datetime
from time import mktime
from urlparse import urlparse
import urllib3
#The only insecure call we do is on purpose.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
parser = argparse.ArgumentParser(description='Convert HTML page to text using readability and html2text.')
parser.add_argument('-u','--url', help='URL to convert', required=True)
parser.add_argument('-s','--sleep', help='Sleep X seconds between URLs (only in rss)', default=3)
parser.add_argument('-t','--timeout', help='Timeout for HTTP requests', default=30)
parser.add_argument('-r', '--rss', help="URL is RSS feed. Parse every item in feed",
default=False, action="store_true")
parser.add_argument('-n', '--noprint', help="Dont print converted contents",
default=False, action="store_true")
parser.add_argument('-o', '--original', help="Dont parse contents with readability.",
default=False, action="store_true")
parser.add_argument('-f', '--forcedownload',
help="Force download even if file seems to be something else than text based on the content-type header.",
default=False, action="store_true")
args = vars(parser.parse_args())
class mockResponse(object):
text = ""
def __init__(self, text):
super(mockResponse, self).__init__()
self.text = text
def cookie_workaround_ad(url):
hostname = urlparse(url).hostname
if hostname == "ad.nl" or hostname == "www.ad.nl":
url = "https://www.ad.nl/accept?url=" + url
return url
def custom_workaround_twitter(url):
hostname = urlparse(url).hostname
if hostname == "twitter.com" or hostname == "www.twitter.com":
session_requests = requests.session()
path = urlparse(url).path
jar = requests.cookies.RequestsCookieJar()
content = session_requests.get(url, headers=headers,
timeout=args['timeout'], cookies=jar)
content.encoding = content.apparent_encoding
tree = lxml.html.fromstring(content.text)
auth_token = list(set(
tree.xpath("//input[@name='authenticity_token']/@value")))[0]
url = "https://mobile.twitter.com/" + path
payload = {"authenticity_token": auth_token}
content2 = session_requests.get(url, headers=headers,
timeout=args['timeout'], cookies=jar, allow_redirects=True)
content2.encoding = content2.apparent_encoding
content2.raise_for_status()
args['original'] = True
return content2
def custom_workaround_noparse(url):
hostname = urlparse(url).hostname
if hostname == "news.ycombinator.com" or \
hostname == "youtube.com" or \
hostname == "www.youtube.com":
args['original'] = True
def cookie_workaround_tweakers(url):
hostname = urlparse(url).hostname
if hostname == "tweakers.net" or hostname == "www.tweakers.net":
headers['X-Cookies-Accepted'] = '1'
def custom_workaround_verisimilitudes(url):
hostname = urlparse(url).hostname
if hostname == "verisimilitudes.net" or hostname == "www.verisimilitudes.net":
path = urlparse(url).path.replace("/","",1)
if(path):
response = get_url(args['url'], workarounds=False)
doc = convert_doc(response.text)
text = str(convert_doc_to_text(doc.content()))
if not text:
title = "Parsing failed"
else:
title = doc.short_title().encode('utf-8').strip()
save_gophermap("", title, hostname, 1, path, 70)
def cookie_workaround_rd(url):
hostname = urlparse(url).hostname
if hostname == "rd.nl" or hostname == "www.rd.nl":
headers['cookieInfoV4'] = "1"
def cookie_workaround_geenstijl(url):
hostname = urlparse(url).hostname
if hostname == "geenstijl.nl" or hostname == "www.geenstijl.nl":
headers['Cookie'] = "cpc=10"
def cookie_workarounds_url(url):
url = cookie_workaround_ad(url)
return url
def cookie_workarounds_header(url):
cookie_workaround_tweakers(url)
cookie_workaround_rd(url)
cookie_workaround_geenstijl(url)
def custom_content_workaround(url):
custom_content = custom_workaround_twitter(url)
custom_workaround_noparse(url)
custom_workaround_verisimilitudes(url)
return custom_content
def get_url(url, workarounds=True):
if workarounds:
url = cookie_workarounds_url(url)
cookie_workarounds_header(url)
custom_content = custom_content_workaround(url)
if custom_content:
return custom_content
try:
r = requests.get(url, headers=headers,
timeout=args['timeout'])
r.raise_for_status()
except requests.exceptions.SSLError as e:
print("SSL Error. Retrying once")
time.sleep(5)
r = requests.get(url, headers=headers,
timeout=args['timeout'], verify=False)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print("HTTP Error. Retrying once")
time.sleep(5)
r = requests.get(url, headers=headers,
timeout=args['timeout'])
except requests.exceptions.ReadTimeout as e:
print("ReadTimeout, retrying once")
time.sleep(5)
r = requests.get(url, headers=headers,
timeout=args['timeout'])
r.raise_for_status()
r.encoding = r.apparent_encoding
try:
if "message" in r.headers['content-type'] or \
"image" in r.headers['content-type'] or \
"pdf" in r.headers['content-type'] or \
"model" in r.headers['content-type'] or \
"multipart" in r.headers['content-type'] or \
"audio" in r.headers['content-type'] or \
"font" in r.headers['content-type'] or \
"video" in r.headers['content-type']:
if args['forcedownload']:
return r
else:
return mockResponse("""This might not be a html file but something
else, like a PDF or an audio file. Use the --forcedownload flag
to download and parse this anyway. The content type reported for
this file is: %s\n\n""" % (r.headers['content-type']))
except KeyError:
pass
if len(r.text) > 5:
return r
else:
return mockResponse("Empty Response")
def convert_doc(html_text):
return Document(input=html_text,
positive_keywords=["articleColumn", "article", "content",
"category_news"],
negative_keywords=["commentColumn", "comment", "comments",
"posting_list reply", "posting reply", "reply"])
def convert_doc_to_text(doc_summary):
h = html2text.HTML2Text()
h.inline_links = False # reference style links
h.wrap_links = False
h.body_width = 72
doc = h.handle(doc_summary).encode('utf-8').strip()
if len(doc) > 500:
return doc
def save_doc(text, title, url, rssDate=0):
hostname = urlparse(url).hostname
if not os.path.exists("saved/" + hostname):
os.makedirs("saved/" + hostname)
filename = re.sub(r'[^A-Za-z0-9]', '_', title)
if rssDate:
posttime = datetime.fromtimestamp(mktime(rssDate)).strftime("%Y%m%dT%H%M")
else:
posttime = datetime.now().strftime("%Y%m%dT%H%M")
filename = "saved/" + hostname + "/" + posttime + "_" + filename + ".txt"
if not os.path.exists(filename):
with open(filename, "w") as textfile:
textfile.write("# " + title)
textfile.write("\nSource URL: \t" + url)
textfile.write("\nDate: \t\t" + posttime)
textfile.write("\n\n")
textfile.write(text)
return filename
def save_gophermap(text, title, server, gophertype, filename, gopherport):
if not os.path.exists("saved/" + server):
os.makedirs("saved/" + server)
posttime = datetime.now().strftime("%Y%m%dT%H%M")
if not os.path.exists("saved/" + server + "/" + posttime):
os.makedirs("saved/" + server + "/" + posttime)
gmfile = "saved/" + server + "/" + posttime + "/gophermap"
if not os.path.exists(gmfile):
with open(gmfile, "w") as gophermap:
gophermap.write("i\t/\tlocalhost\t70\n")
gophermap.write("iThis article is available via Gopher. \t/\tlocalhost\t70\n")
gophermap.write("iPlease follow the link\t/\tlocalhost\t70\n")
gophermap.write(("%i%s on %s\t/%s\t%s\t%i\n") % (gophertype,
title, server, filename, server, gopherport))
gophermap.write("i\t/\tlocalhost\t70\n")
gophermap.write(("iLast update: %s\t/\tlocalhost\t70\n") % (posttime))
gophermap.write(text)
return gmfile
headers = {'User-Agent': 'Tiny Tiny RSS/19.2 (1a484ec) (http://tt-rss.org/)'}
response = get_url(args['url'])
if args['rss']:
feed = feedparser.parse(response.text)
if feed.bozo:
print("Invalid XML.")
sys.exit(1)
else:
for post in feed.entries:
try:
response = get_url(post['link'])
except Exception as e:
response = mockResponse("Failed to get RSS article.")
doc = convert_doc(response.text)
if args['original']:
text = str(convert_doc_to_text(doc.content()))
else:
text = convert_doc_to_text(doc.summary())
if not text:
text = "Parsing with Readability failed. Original content:\n\n"
text += str(convert_doc_to_text(doc.content()))
title = doc.short_title().encode('utf-8').strip()
try:
rssDate = post['published_parsed']
except KeyError:
try:
rssDate = post['updated_parsed']
except KeyError:
rssDate = post['created_parsed']
filename = save_doc(text, title[:150], post['link'], rssDate)
if not args['noprint']:
print("\n\n========================\n\n")
print("# " + title)
print("Source URL: " + post['link'])
print("\n")
print(text)
print("file saved as " + filename)
if args['sleep']:
time.sleep(args['sleep'])
else:
doc = convert_doc(response.text)
if args['original']:
text = str(convert_doc_to_text(doc.content()))
else:
text = convert_doc_to_text(doc.summary())
if not text:
text = "Parsing with Readability failed. Original content:\n\n"
text += str(convert_doc_to_text(doc.content()))
title = doc.short_title().encode('utf-8').strip()
filename = save_doc(text, title[:150], args['url'])
if not args['noprint']:
print("\n\n========================\n\n")
print("# " + title)
print("Source URL: " + args['url'])
print("\n")
print(text)
print("file saved as " + filename)