4 htsn-import \- Import XML files from The Sports Network into an RDBMS.
8 \fBhtsn-import\fR [OPTIONS] [FILES]
12 The Sports Network <http://www.sportsnetwork.com/> offers an XML feed
13 containing various sports news and statistics. Our sister program
14 \fBhtsn\fR is capable of retrieving the feed and saving the individual
15 XML documents contained therein. But what to do with them?
17 The purpose of \fBhtsn-import\fR is to take these XML documents and
18 get them into something we can use, a relational database management
19 system (RDBMS), otherwise known as a SQL database. The structure of
20 relational database, is, well, relational, and the feed XML is not. So
21 there is some work to do before the data can be imported into the
24 First, we must parse the XML. Each supported document type (see below)
25 has a full pickle/unpickle implementation (\(dqpickle\(dq is simply a
26 synonym for serialize here). That means that we parse the entire
27 document into a data structure, and if we pickle (serialize) that data
28 structure, we get the exact same XML document tha we started with.
30 This is important for two reasons. First, it serves as a second level
31 of validation. The first validation is performed by the XML parser,
32 but if that succeeds and unpicking fails, we know that something is
33 fishy. Second, we don't ever want to be surprised by some new element
34 or attribute showing up in the XML. The fact that we can unpickle the
35 whole thing now means that we won't be surprised in the future.
37 The aforementioned feature is especially important because we
38 automatically migrate the database schema every time we import a
39 document. If you attempt to import a \(dqnewsxml.dtd\(dq document, all
40 database objects relating to the news will be created if they do not
41 exist. We don't want the schema to change out from under us without
42 warning, so it's important that no XML be parsed that would result in
43 a different schema than we had previously. Since we can
44 pickle/unpickle everything already, this should be impossible.
46 .SH SUPPORTED DOCUMENT TYPES
48 The XML document types obtained from the feed are uniquely identified
49 by their DTDs. We currently support documents with the following DTDs:
51 AutoRacingResultsXML.dtd
53 Auto_Racing_Schedule_XML.dtd
57 Injuries_Detail_XML.dtd
78 Matchup_NBA_NHL_XML.dtd
80 MLB_Gaming_Matchup_XML.dtd
90 NBA_Gaming_Matchup_XML.dtd
92 NBA_Playoff_Matchup_XML.dtd
98 NCAA_FB_Preview_XML.dtd
100 NFL_NCAA_FB_Matchup_XML.dtd
108 WorldBaseballPreviewXML.dtd
116 Cbask_All_Tourn_Teams_XML.dtd
124 Cbask_Conf_Standings_XML.dtd
126 Cbask_DivII_III_Indv_Stats_XML.dtd
128 Cbask_DivII_Team_Stats_XML.dtd
130 Cbask_DivIII_Team_Stats_XML.dtd
138 Cbask_Indv_Scoring_XML.dtd
144 CBASK_ReboundsXML.dtd
146 CBASK_ScoringLeadersXML.dtd
148 Cbask_Team_ThreePT_Made_XML.dtd
150 Cbask_Team_ThreePT_PCT_XML.dtd
152 Cbask_Team_Win_Pct_XML.dtd
154 Cbask_Top_Twenty_Five_XML.dtd
156 CBASK_TopTwentyFiveResult_XML.dtd
158 Cbask_Tourn_Awards_XML.dtd
160 Cbask_Tourn_Champs_XML.dtd
162 Cbask_Tourn_Indiv_XML.dtd
164 Cbask_Tourn_Leaders_XML.dtd
166 Cbask_Tourn_MVP_XML.dtd
168 Cbask_Tourn_Records_XML.dtd
170 LeagueScheduleXML.dtd
174 Minor_Baseball_League_Leaders_XML.dtd
176 Minor_Baseball_Standings_XML.dtd
178 Minor_Baseball_Transactions_XML.dtd
182 mlbdoublesleadersxml.dtd
184 MLBGamesPlayedXML.dtd
190 mlbhitsleadersxml.dtd
208 mlbrunsleadersxml.dtd
216 mlbsluggingpctxml.dtd
220 mlbstandxml_preseason.dtd
224 mlbtotalbasesleadersxml.dtd
226 mlbtriplesleadersxml.dtd
230 mlbwalksleadersxml.dtd
232 MLBXtraBaseHitsXML.dtd
234 MLB_Pitching_Appearances_Leaders.dtd
238 MLB_Pitching_Balks_Leaders.dtd
240 MLB_Pitching_CG_Leaders.dtd
242 MLB_Pitching_ER_Allowed_Leaders.dtd
244 MLB_Pitching_Hits_Allowed_Leaders.dtd
246 MLB_Pitching_Hit_Batters_Leaders.dtd
248 MLB_Pitching_HR_Allowed_Leaders.dtd
250 MLB_Pitching_IP_Leaders.dtd
252 MLB_Pitching_Runs_Allowed_Leaders.dtd
254 MLB_Pitching_Saves_Leaders.dtd
256 MLB_Pitching_Shut_Outs_Leaders.dtd
258 MLB_Pitching_Starts_Leaders.dtd
260 MLB_Pitching_Strike_Outs_Leaders.dtd
262 MLB_Pitching_Walks_Leaders.dtd
264 MLB_Pitching_WHIP_Leaders.dtd
266 MLB_Pitching_Wild_Pitches_Leaders.dtd
268 MLB_Pitching_Win_Percentage_Leaders.dtd
270 MLB_Pitching_WL_Leaders.dtd
272 NBA_Team_Stats_XML.dtd
275 The GameInfo and SportInfo types do not have their own top-level
276 tables in the database. Instead, their raw XML is stored in either the
277 \(dqgame_info\(dq or \(dqsport_info\(dq table respectively.
281 At the top level (with two notable exceptions), we have one table for
282 each of the XML document types that we import. For example, the
283 documents corresponding to \fInewsxml.dtd\fR will have a table called
284 \(dqnews\(dq. All top-level tables contain two important fields,
285 \(dqxml_file_id\(dq and \(dqtime_stamp\(dq. The former is unique and
286 prevents us from inserting the same data twice. The time stamp on the
287 other hand lets us know when the data is old and can be removed. The
288 database schema make it possible to delete only the outdated top-level
289 records; all transient children should be removed by triggers.
291 These top-level tables will often have children. For example, each
292 news item has zero or more locations associated with it. The child
293 table will be named <parent>_<children>, which in this case
294 corresponds to \(dqnews_locations\(dq.
296 To relate the two, a third table may exist with name
297 <parent>__<child>. Note the two underscores. This prevents ambiguity
298 when the child table itself contains underscores. The table joining
299 \(dqnews\(dq with \(dqnews_locations\(dq is thus called
300 \(dqnews__news_locations\(dq. This is necessary when the child table
301 has a unique constraint; we don't want to blindly insert duplicate
302 records keyed to the parent. Instead we'd like to use the third table
303 to map an existing child to the new parent.
305 Where it makes sense, children are kept unique to prevent pointless
306 duplication. This slows down inserts, and speeds up reads (which are
307 much more frequent). There is a tradeoff to be made, however. For a
308 table with a small, fixed upper bound on the number of rows (like
309 \(dqodds_casinos\(dq), there is great benefit to de-duplication. The
310 total number of rows stays small, so inserts are still quick, and many
311 duplicate rows are eliminated.
313 But, with a table like \(dqodds_games\(dq, the number of games grows
314 quickly and without bound. It is therefore more beneficial to be able
315 to delete the old games (through an ON DELETE CASCADE, tied to
316 \(dqodds\(dq) than it is to eliminate duplication. A table like
317 \(dqnews_locations\(dq is somewhere in-between. It is hoped that the
318 unique constraint in the top-level table's \(dqxml_file_id\(dq will
319 prevent duplication in this case anyway.
321 The aforementioned exceptions are the \(dqgame_info\(dq and
322 \(dqsport_info\(dq tables. These tables contain the raw XML for a
323 number of DTDs that are not handled individually. This is partially
324 for backwards-compatibility with a legacy implementation, but is
325 mostly a stopgap due to a lack of resources at the moment. These two
326 tables (game_info and sport_info) still possess timestamps that allow
327 us to prune old data.
329 UML diagrams of the resulting database schema for each XML document
330 type are provided with the \fBhtsn-import\fR documentation.
332 .SH XML Schema Oddities
334 There are a number of problems with the XML on the wire. Even if we
335 construct the DTDs ourselves, the results are sometimes
336 inconsistent. Here we document a few of them.
341 The <Notes> elements here are supposed to be associated with a set of
342 <Game> elements, but since the pair
343 (<Notes>...</Notes><Game>...</Game>) can appear zero or more times,
344 this leads to ambiguity in parsing. We therefore ignore the notes
345 entirely (although a hack is employed to facilitate parsing).
350 There appear to be two types of weather documents; the first has
351 <listing> contained within <forecast> and the second has <forecast>
352 contained within <listing>. While it would be possible to parse both,
353 it would greatly complicate things. The first form is more common, so
354 that's all we support for now.
358 .IP \fB\-\-backend\fR,\ \fB\-b\fR
359 The RDBMS backend to use. Valid choices are \fISqlite\fR and
360 \fIPostgres\fR. Capitalization is important, sorry.
364 .IP \fB\-\-connection-string\fR,\ \fB\-c\fR
365 The connection string used for connecting to the database backend
366 given by the \fB\-\-backend\fR option. The default is appropriate for
367 the \fISqlite\fR backend.
369 Default: \(dq:memory:\(dq
371 .IP \fB\-\-log-file\fR
372 If you specify a file here, logs will be written to it (possibly in
373 addition to syslog). Can be either a relative or absolute path. It
374 will not be auto-rotated; use something like logrotate for that.
378 .IP \fB\-\-log-level\fR
379 How verbose should the logs be? We log notifications at four levels:
380 DEBUG, INFO, WARN, and ERROR. Specify the \(dqmost boring\(dq level of
381 notifications you would like to receive (in all-caps); more
382 interesting notifications will be logged as well. The debug output is
383 extremely verbose and will not be written to syslog even if you try.
387 .IP \fB\-\-remove\fR,\ \fB\-r\fR
388 Remove successfully processed files. If you enable this, you can see
389 at a glance which XML files are not being processed, because they're
390 all that should be left.
394 .IP \fB\-\-syslog\fR,\ \fB\-s\fR
395 Enable logging to syslog. On Windows this will attempt to communicate
396 (over UDP) with a syslog daemon on localhost, which will most likely
401 .SH CONFIGURATION FILE
403 Any of the command-line options mentioned above can be specified in a
404 configuration file instead. We first look for \(dqhtsn-importrc\(dq in
405 the system configuration directory. We then look for a file named
406 \(dq.htsn-importrc\(dq in the user's home directory. The latter will
409 The user's home directory is simply $HOME on Unix; on Windows it's
410 wherever %APPDATA% points. The system configuration directory is
411 determined by Cabal; the \(dqsysconfdir\(dq parameter during the
412 \(dqconfigure\(dq step is used.
414 The file's syntax is given by examples in the htsn-importrc.example file
415 (included with \fBhtsn-import\fR).
417 Options specified on the command-line override those in either
422 Import newsxml.xml into a preexisting sqlite database named \(dqfoo.sqlite3\(dq:
425 .I $ htsn-import --connection-string='foo.sqlite3' \\\\
426 .I " test/xml/newsxml.xml"
427 Successfully imported test/xml/newsxml.xml.
428 Imported 1 document(s) total.
431 Repeat the previous example, but delete newsxml.xml afterwards:
434 .I $ htsn-import --connection-string='foo.sqlite3' \\\\
435 .I " --remove test/xml/newsxml.xml"
436 Successfully imported test/xml/newsxml.xml.
437 Imported 1 document(s) total.
438 Removed processed file test/xml/newsxml.xml.
441 Use a Postgres database instead of the default Sqlite. This assumes
442 that you have a database named \(dqhtsn\(dq accessible to user
443 \(dqpostgres\(dq locally:
446 .I $ htsn-import --connection-string='dbname=htsn user=postgres' \\\\
447 .I " --backend=Postgres test/xml/newsxml.xml"
448 Successfully imported test/xml/newsxml.xml.
449 Imported 1 document(s) total.
455 Send bugs to michael@orlitzky.com.