This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
beeb.rb
115 lines (87 loc) · 3.17 KB
/
beeb.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# beeb.rb @VERSION
#
# Copyright (c) 2008 Libby Miller
# Licensed under the MIT (MIT-LICENSE.txt)
require 'rubygems'
require 'net/http'
require 'uri'
require 'open-uri'
require 'json/pure'
require 'sqlite3'
# This class parses a JSON representation of today's programmes and stores them in a database
class Twitter
SQL_DROP_TABLE = "DROP table beeb;"
# we use TEXT for the timestamp, see http://www.sqlite.org/datatype3.html and http://www.sqlite.org/lang_datefunc.html
SQL_CREATE_TABLE = "CREATE TABLE if not exists beeb(STARTTIME TEXT, PID VARCHAR(8), TITLE VARCHAR(255), SUBTITLE VARCHAR(255));"
SQL_INSERT_ROW = <<-INSERT
INSERT into "beeb"
VALUES
(:starttime, :pid, :title, :subtitle);
INSERT
# should probably go in a constructor or something
@db = SQLite3::Database.open('beeb.db')
def Twitter.status()
u = "http://www.bbc.co.uk/radio4/programmes/schedules/fm/today.json"
da = DateTime.now - (30/1440.0)
url = URI.parse u
puts "checking for updates #{url}"
req = Net::HTTP::Get.new(url.path)
# retrieve the json data
# should be some error checking here
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
# Parse the json data
j = nil
begin
j = JSON.parse(res.body)
rescue OpenURI::HTTPError=>e
case e.to_s
when /^404/
raise 'Not Found'
when /^304/
raise 'No Info'
end
end
j = j['schedule']['day']['broadcasts']
puts "found #{j.length} results"
# Make sure the table is empty
begin
@db.execute(SQL_DROP_TABLE)
rescue
puts "There was an error dropping the table: ", $!
end
# Create the table
begin
@db.execute(SQL_CREATE_TABLE)
rescue
puts "There was an error creating the table: ", $!
end
# Insert the data
begin
x = 0
while x < j.length
starttime = j[x]['start']
pid = j[x]['programme']['pid']
title = j[x]['programme']['display_titles']['title']
subtitle = j[x]['programme']['display_titles']['subtitle']
# subtitle may only be a date, but we leave it to todaysTwits
# to decide whether to display it or not
puts "#{x} #{starttime} #{title}"
# break the ISO8601 timestamp into YYYY-MM-DD and HH:MM:SS and insert
if (starttime != nil && starttime =~ /(.*)T(.*)Z/)
d = $1
t = $2
ds = "#{d} #{t}"
@db.execute(SQL_INSERT_ROW, 'starttime' => starttime,
'pid' => pid,
'title' => title,
'subtitle' => subtitle)
end
x = x + 1
end
rescue
puts "There was an error inserting a row: ", $!
end
end
# where to put an explicit @db.close() ?
end
Twitter.status()