05-30-2020, 07:44 +0200
Forum: Overviewworkscript announcements
A small call-it-a-lib script able to handle rss feeds
AuthorPost
SIYB  04-15-2009, 23:09   | RSS lib Jabber  Homepage
Member since 06/2007
8 Posts
A small script that will help writing applications that need to parse RSS(2.0) feeds. The most important (from my point of view) features of RSS2.0 have been included, please feel free to enhance the script and share your results if you feel that they are useful.

There is a small demo at the end of the script, it should cover the usage, don't forget to free the mallocs :>. Any questions? -> email / irc / thread


                                                                                                                              
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright (c) 2009 siyb <siyb |insert "you know what here[/quote] geekosphere.org>
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
#    1. The origin of this software must not be misrepresented; you must not
#    claim that you wrote the original software. If you use this software
#    in a product, an acknowledgment in the product documentation would be
#    appreciated but is not required.
#
#    2. Altered source versions must be plainly marked as such, and must not be
#    misrepresented as being the original software.
#
#    3. This notice may not be removed or altered from any source
#    distribution.


package require tdom

namespace eval feedRead {
    variable idPrefix "feedread";# the prefix to be used for feeds
    variable formats [list rss]
    variable tmpId;# stores the id temporarily (when a feed is added -> upvaring)
    variable feedId 0;# the id counter
    variable feedList [list];# list containing all feedids and the corresponding data
    namespace eval handle {}
}

#
# Enduser relevant
#

# Creates a feed
proc feedRead::create {xmlData id} {
    upvar 1 $id rid
    # create domtree for the feed
    set doc [dom parse -keepEmpties $xmlData]
    set rid [feedRead::addFeed $doc];# add feed to feedlist and return the id
}

# return the feedlist of all feeds currently active
proc feedRead::getFeedList {} {
    return $feedRead::feedList
}

# clean up (destroy) all feeds
proc feedRead::destroyAll {} {
    foreach item [feedRead::feedList] {
        feedRead::destroy [lindex $item 0]
    }
}

#
# Lib
#

# deletes a feed from the list and gets rid of all traces
proc feedRead::destroy {id} {
    set doc [$id infoDoc];# get the document
    rename $id "";# delete the feed's procedure
    set search [feedRead::findFeed $id]
    set feedRead::feedList [lreplace $feedRead::feedList $search $search]
    $doc delete
}

# checks if a format is known to the script
proc feedRead::checkFormat {format} {
    return [lsearch $feedRead::formats $format]
}

# joins prefix and id
proc feedRead::joinId {id} {
    return [append foo $feedRead::idPrefix $id]
}

# Returns a feedread feedid
proc feedRead::mkid {} {
    set id [feedRead::joinId $feedRead::feedId]
    incr feedRead::feedId
    return $id
}

# find feed in list
proc feedRead::findFeed {id} {
    return [lsearch -index 0 $feedRead::feedList $id]
}

# replace feed in feedlist
proc feedRead::feedReplace {id replaceWith} {
    set search [feedRead::findFeed $id]
    set feedRead::feedList [lreplace $feedRead::feedList $search $search $replaceWith]
}

# find and return feed from list
proc feedRead::returnFeed {id} {
    return [lindex $feedRead::feedList [feedRead::findFeed $id]]
}

# determine the feed type
proc feedRead::determineType {rootNode} {
    return [$rootNode nodeName]
}

# count the number of <item> of a feed
proc feedRead::countItems {id} {
    return [llength [lindex [feedRead::returnFeed $id] end]]
}

# returns an <item> by its place in the itemlist of the feed
proc feedRead::returnItemById {id iid} {
    if {[feedRead::countItems $id] < $iid} {
        error "Itemid '$iid' out of range"
    }
    return [lindex [lindex [feedRead::returnFeed $id] end] $iid]
}

# adds a feed and the corresponding feed command
proc feedRead::addFeed {doc} {
    upvar #0 feedRead::tempId id
    set id [feedRead::mkid];# create a new id for this feed
    set root [$doc documentElement]
    set flist [list $id $doc $root [feedRead::determineType $root]]
    set feedRead::feedList [lappend feedRead::feedList $flist];# add feed to feedlist
    uplevel #0 {;# create the feed command
        proc $feedRead::tempId {option {iid -1}} {
            set id [string trim [dict get [info frame 0] proc] ::]
            set format [feedRead::getFeedType $id]

            if {[feedRead::checkFormat $format] != -1} {
                feedRead::handle::${format} $id $option $iid
            } else {
                error "'$format' not supported yet"
            }
        }
    }
    feedRead::feedReplace $id [lappend flist [feedRead::getChannelElement $id item -all]];# add all <item> to the feedlist
    return $id
}

# rss handler
proc feedRead::handle::rss {id option iid} {
    switch $option {
        "destroy"            { return [feedRead::destroy $id] }
        "infoType"            { return [feedRead::getFeedType $id] }
        "infoDoc"            { return [feedRead::getFeedDoc $id] }
        "infoRoot"            { return [feedRead::getFeedRoot $id] }
        "getChannelTitle"        { return [feedRead::getChannelElement $id title] }
        "getChannelLink"        { return [feedRead::getChannelElement $id link] }
        "getChannelDescription"        { return [feedRead::getChannelElement $id description] }
        "getChannelLanguage"        { return [feedRead::getChannelElement $id language] }
        "getChannelCopyright"        { return [feedRead::getChannelElement $id copyright] }
        "getChannelManagingEditor"    { return [feedRead::getChannelElement $id managingEditor] }
        "getChannelWebMaster"        { return [feedRead::getChannelElement $id webMaster] }
        "getChannelPubDate"        { return [feedRead::getChannelElement $id pubDate] }
        "getChannelLastBuildDate"    { return [feedRead::getChannelElement $id lastBuildDate] }
        "getChannelCategory"        { return [feedRead::getChannelElement $id category] }
        "getChannelGenerator"        { return [feedRead::getChannelElement $id generator] }
        "getChannelDocs"        { return [feedRead::getChannelElement $id docs] }
        "getChannelCloud"        { return [feedRead::getChannelElement $id cloud] }
        "getChannelTtl"            { return [feedRead::getChannelElement $id ttl] }
        "getChannelImage"        { return [feedRead::getChannelElement $id image] }
        "getChannelRating"        { return [feedRead::getChannelElement $id rating] }
        "getChannelTextInput"        { return [feedRead::getChannelElement $id textInput] }
        "getChannelSkipHours"        { return [feedRead::getChannelElement $id skipHours] }
        "getChannelSkipDays"        { return [feedRead::getChannelElement $id skipDays] }
        "getChannelItems"        { return [feedRead::getChannelElement $id item -all] }
        "getChannelItemLength"        { return [feedRead::countItems $id] }
        "getItemTitle"            { return [feedRead::getItemElement $id $iid title] }
        "getItemLink"            { return [feedRead::getItemElement $id $iid link] }
        "getItemDescription"        { return [feedRead::getItemElement $id $iid description] }
        "getItemAuthor"            { return [feedRead::getItemElement $id $iid author] }
        "getItemCategory"        { return [feedRead::getItemElement $id $iid category] }
        "getItemComments"        { return [feedRead::getItemElement $id $iid comments] }
        "getItemEnclosure"        { return [feedRead::getItemElement $id $iid enclosure] }
        "getItemGuid"            { return [feedRead::getItemElement $id $iid guid] }
        "getItemPubDate"        { return [feedRead::getItemElement $id $iid pubDate] }
        "getItemSource"            { return [feedRead::getItemElement $id $iid source] }
        default                { error "Unknown option ($option)" }
    }
}

#return the feed's type
proc feedRead::getFeedType {id} {
    return [lindex [split [feedRead::returnFeed $id]] 3]
}

# return the feed's document
proc feedRead::getFeedDoc {id} {
    return [lindex [split [feedRead::returnFeed $id]] 1]
}

# return the feed root
proc feedRead::getFeedRoot {id} {
    return [lindex [split [feedRead::returnFeed $id]] 2]
}

# get channel
proc feedRead::getChannel {id} {
    set root [feedRead::getFeedRoot $id]
    return [$root getElementsByTagName channel]
}

# get a channel element
proc feedRead::getChannelElement {id chanItem {option -single}} {
    set channel [feedRead::getChannel $id]
    set ci [$channel getElementsByTagName $chanItem]
    switch $option {
        "-single"     {
            foreach item $ci {;# loop all titles, if the parentNode of the title matches the channel, return it's text value
                if {[$item parentNode] == $channel} { return [$item asText] }
            }
        }
        "-all"     {
            return $ci
        }
        default    { error "bad option \"$option\": must be -all or -single" }
    }
}

# get an <item> element
proc feedRead::getItemElement {id iid itemElement} {
    set item [[feedRead::returnItemById $id $iid] getElementsByTagName $itemElement]
    if {$item == ""} { return "" }
    if {[llength $item] > 1} {;# of there is more than one element by that name
        set rlist [list]
        foreach i $item {
            set rlist [lappend rlist [$i asText]]
        }
        return $rlist
    } else {
        return [$item asText]
    }
}

#
# documentation demonstration
#
proc feedRead::demo {feed} {

    package require http

    set token [::http::geturl $feed -timeout 2000]
    set data [::http::data $token]
    ::http::cleanup $token

    # lets create the feed first, $data is the feed's data
    feedRead::create $data id
    puts "Feedlist: [feedRead::getFeedList]";# print feedlist

    # some information on the feed
    puts "\nfeed information:\n"
    puts "    [$id infoType]";# the type of the doc
    puts "    [$id infoDoc]";# the document identifier
    puts "    [$id infoRoot]";# the root node identifier of the doc

    # channel related information -> [$id getChannelTitle] for instance
    set l {
        "getChannelTitle"
        "getChannelLink"
        "getChannelDescription"
        "getChannelLanguage"
        "getChannelCopyright"
        "getChannelManagingEditor"
        "getChannelWebMaster"
        "getChannelPubDate"
        "getChannelLastBuildDate"
        "getChannelCategory"
        "getChannelGenerator"
        "getChannelDocs"
        "getChannelCloud"
        "getChannelTtl"
        "getChannelImage"
        "getChannelRating"
        "getChannelTextInput"
        "getChannelSkipHours"
        "getChannelSkipDays"
        "getChannelItems"
        "getChannelItemLength"
    }
    puts "\nChannel related information:\n"
    foreach item $l {
        puts "    $item: '[$id $item]'"
    }

    # item related stuff
    puts "\nitem related stuff:\n"
    set l {
        "getChannelItemLength"
        "getItemTitle"
        "getItemLink"
        "getItemDescription"
        "getItemAuthor"
        "getItemCategory"
        "getItemComments"
        "getItemGuid"
        "getItemPubDate"
        "getItemSource"
    }
    foreach item $l {
        puts "    $item: '[$id $item 0]'"
    }

    # now that we are done we need to destroy all data related to the feed in order to prevent memory from going apeshit
    $id destroy
}

This post was edited 1 times, last on 04-21-2009, 20:10 by SIYB
  The user has attached a file: feedRead.tcl (Save, 9,831 Bytes, downloaded 152 times)
CyBex  04-16-2009, 00:24   Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
can you add a download file to the posting? the forum has problems with backslashes.. :-)

mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
SIYB  04-21-2009, 20:12   Jabber  Homepage
Member since 06/2007
8 Posts
Quote by CyBex on 04-16-2009, 00:24:
can you add a download file to the posting? the forum has problems with backslashes.. :-)

done :>
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen | Time: 165.2 msec, CPU time: 77.1 msec, 35 Database queries in 114.3 msec | 10 kB (65.7 kB) | Timezone: +0200