-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhack.rb
98 lines (84 loc) · 2.57 KB
/
hack.rb
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
# hack.rb
require 'rubygems'
require 'sinatra'
require 'httparty'
require 'json'
class SkyNews
include HTTParty
end
helpers do
def cleanNewsML(newsml)
clean = {}
stories = []
newsml["NewsML"]["NewsItem"].each{|key, value|
story = {}
newsitem = key["NewsComponent"]["NewsComponent"]["NewsComponent"]
# story["id"] = key["Identification"]["NewsIdentifier"]["NewsItemId"]
story["headline"] = newsitem[0]["NewsLines"]["HeadLine"]
story["slug"] = newsitem[0]["NewsLines"]["SlugLine"]
story["body"] = newsitem[0]["ContentItem"]["DataContent"]
imageIndex = newsitem[1]["ContentItem"].index{|item|item["Characteristics"]["Property"][0]["Value"]=='400'}
if (imageIndex)
story["image"] = newsitem[1]["ContentItem"].at(imageIndex)["Href"]
else
story["image"] = '/img/breaking-news.jpg'
end
# Do we have a video
if newsitem.size() > 2
videoWithPhoto = newsitem[2]["ContentItem"][0]
video = ""
if videoWithPhoto
video = newsitem[2]["ContentItem"][0]["Href"]
# story["image"] = newsitem[2]["ContentItem"][1]["Href"]
else
video = newsitem[2]["ContentItem"]["Href"]
end
story["video"] = video
end
stories.push story
}
# puts stories.to_s
clean["stories"] = stories
return clean
end
def cleanBlogs(blogs)
clean{}
blogs[]
blogs["rss"]["channel"]["item"]..each{|key, value|
blogs["author"] = key['category']
blogs["image"] = '/img/'
blogs["title"] = key['title']
blogs["desc"] = key['description']
}
end
end
get '/' do
File.read(File.join('public', 'index.html'))
end
get '/feed/:feed' do |f|
callback = params.delete('callback')
resp = HTTParty.get("http://news.sky.com/sky-news/newsml/#{f}/index.xml")
json = resp.parsed_response.to_json
json = cleanNewsML(resp.parsed_response).to_json
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
response
end
get '/blogs' do
callback = params.delete('callback')
resp = HTTParty.get("http://news.sky.com/sky-news/feeds/blogs/topBlogs.xml")
json = resp.parsed_response.to_json
if callback
content_type :js
response = "#{callback}(#{json})"
else
content_type :json
response = json
end
response
end