-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfeed2ical.py
executable file
·150 lines (138 loc) · 5.1 KB
/
feed2ical.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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime, timedelta
import os
import feedparser
import html2text
import logging
import re
from urllib import quote_plus
from xml.dom import minidom
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api.urlfetch import fetch
class MainHandler(webapp.RequestHandler):
def get(self):
template_values = { 'host':self.request.headers.get('host', 'no host') }
path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
self.response.out.write(template.render(path, template_values))
class Convert(webapp.RequestHandler):
def get(self):
url = self.request.get('url')
try:
logging.info(url)
result = fetch(url, headers = {'Cache-Control' : 'max-age=300'})
if result.status_code == 200:
sContent = result.content
sContent = re.sub(r'(<|</)([a-zA-Z]+):start(time|date)>', r'\1dcterms:created>', sContent)
sContent = re.sub(r'(<|</)([a-zA-Z]+):end(time|date)>', r'\1expirationDate>', sContent)
d = feedparser.parse(sContent)
try:
d.feed.title_text = re.sub(r'\s+', " ", html2text.html2text(d.feed.title))
d.feed.description_text = re.sub(r'\s+', " ", html2text.html2text(d.feed.description))
except:
pass
for entry in d.entries:
entry['all_day'] = True
try:
datPubDate = datetime(*(entry.updated_parsed[0:6]))
except:
datPubDate = datetime.utcnow()
try:
if (datPubDate.hour + datPubDate.minute + datPubDate.second) > 0:
entry['all_day'] = False
except:
pass
for entry in d.entries:
try:
entry.description_text = html2text.html2text(entry.description)
entry.description_text = re.sub(r'[ ]*\n+[ ]*', "\N", entry.description_text)
entry.description_text = re.sub(r';', "\;", entry.description_text)
entry.description_text = re.sub(r',', "\,", entry.description_text)
entry.description_text = re.sub(r'[ \t\r]+', " ", entry.description_text)
except:
pass
try:
try:
entry.title_text = re.sub(r'\s+', " ", html2text.html2text(entry.title))
except:
entry.title_text = (entry.description_text[:70] + '...') if len(entry.description_text) > 70 else entry.description_text
except:
pass
try:
try:
datPubDate = datetime(*(entry.updated_parsed[0:6]))
except:
datPubDate = datetime.utcnow()
try:
datStartDate = datetime(*(entry.created_parsed[0:6]))
except:
datStartDate = datPubDate
try:
datEndDate = datetime(*(entry.expired_parsed[0:6]))
except:
if entry['all_day']:
datEndDate = (datPubDate + timedelta(days=1))
else:
datEndDate = (datPubDate + timedelta(hours=1))
entry.datetime_ical = datStartDate.strftime("%Y%m%dT%H%M%SZ");
if entry['all_day']:
stfmt = "%Y%m%d"
else:
stfmt = "%Y%m%dT%H%M%SZ"
entry.start_ical = datStartDate.strftime(stfmt)
entry.end_ical = datEndDate.strftime(stfmt)
except:
pass
template_values = { 'd':d }
path = os.path.join(os.path.dirname(__file__), 'templates/convert.html')
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write(template.render(path, template_values))
else:
self.response.out.write("Error " + str(result.status_code) + " when retrieving feed.")
except:
self.response.out.write("Error downloading feed.")
logging.info("Error downloading feed url")
raise
class Opml(webapp.RequestHandler):
def post(self):
try:
opml = self.request.get('opmlfile')
mydom = minidom.parseString(opml)
opml_title = mydom.getElementsByTagName("title")[0].firstChild.data
opml_items = mydom.getElementsByTagName("outline")
channels = []
for item in opml_items:
item_text = item.getAttribute("text")
item_htmlurl = item.getAttribute("htmlUrl")
item_xmlurl = item.getAttribute("xmlUrl")
channels.append({'text':item_text, 'htmlurl':item_htmlurl, 'xmlurl':quote_plus(item_xmlurl)})
template_values = { 'opml_title':opml_title, 'channels':channels, 'host':self.request.headers.get('host', 'no host') }
path = os.path.join(os.path.dirname(__file__), 'templates/opml.html')
self.response.out.write(template.render(path, template_values))
except:
self.response.out.write("Error processing opml file.")
application = webapp.WSGIApplication([
('/', MainHandler),
('/convert', Convert),
('/opml', Opml)
], debug=True)
def main():
util.run_wsgi_app(application)
if __name__ == '__main__':
main()