Errors, trying to edit and write some text files

Hi, I’m a total ruby noob but i’ve obtained this script from another
forum over at Tracks. I’m trying to extract some info from a plain text
feed of my upcoming calendar events and write specific info to three
different text files (today, tomorrow, and next 7 days). I’m on a Mac.

The script is here: I’ve already made edits specific to my computer (the
address of my text feed for example):

#!/usr/bin/ruby
Generate 3 seperate text files from Tracks TXT feeds
listing actions due today (or overdue), tomorrow or
in the next 7 days.
You can then pull these text files into GeekTool and colour
them separately.

======== Constants to set with your values ===============

URL for the base feed: http://yourdomain.tld/feed/text/[token]
URLÂ =Â “http://0.0.0.0:3000/feed/text/Nathan/b0b2a7c81680d1a1920b199ecb6db0ad86e2e984
Path to temporary directory
TEMP_DIRÂ =Â “/Users/”

=============================================
Grab each feed, split into lines, then get rid of any non-action lines
today = curl -s "#{URL}"?due=0.split(“\n”).select{|line| line =~ /\s+[Due:/ or line =~ /^\w/}
tomorrow = curl -s "#{URL}"?due=1.split(“\n”).select{|line| line =~ /\s+[Due:/ or line =~ /^\w/}
this_week = curl -s "#{URL}"?due=6.split(“\n”).select{|line| line =~ /\s+[Due:/ or line =~ /^\w/}

Get rid of repeated actions in tomorrow and this_week
tomorrow = tomorrow - today
today_or_tomorrow = today + tomorrow
this_week = this_week - today_or_tomorrow

Print the info to three files, stored in TEMP_DIR
file = File.new(“#{TEMP_DIR}/today.txt”, “w”)
  file.print “\nToday:\n" + today.join(”\n")
file.close

file = File.new(“#{TEMP_DIR}/tomorrow.txt”, “w”)
  file.print “\nTomorrow:\n" + tomorrow.join(”\n")
file.close

file = File.new(“#{TEMP_DIR}/later.txt”, “w”)
  file.print “\nLater:\n" + this_week.join(”\n")
file.close

When I run this from BBEdit I get about 50 lines of this:

untitled text:11: Invalid char \302' in expression untitled text:11: Invalid char \240’ in expression
untitled text:11: Invalid char \302' in expression untitled text:11: Invalid char \240’ in expression
untitled text:13: Invalid char \302' in expression untitled text:13: Invalid char \240’ in expression
untitled text:13: Invalid char \302' in expression untitled text:13: Invalid char \240’ in expression
untitled text:17: Invalid char \302' in expression untitled text:17: Invalid char \240’ in expression
untitled text:17: Invalid char \302' in expression untitled text:17: Invalid char \240’ in expression

Any ideas? Sorry if this appears obvious. Many thanks.

Nathan Thickett-menghini [email protected] wrote:

untitled text:17: Invalid char \302' in expression untitled text:17: Invalid char \240’ in expression
untitled text:17: Invalid char \302' in expression untitled text:17: Invalid char \240’ in expression

Even in my newsreader program I had trouble with your message. The
problem seems to be that something went wrong when you did the cut and
paste from the Tracks forum. Your code is full of invisible characters
between the normal characters. BBEdit should in fact be able to help you
track this down and fix the problem. Feel free to email me the BBEdit
file privately and I’ll remove the gremlins for you if you don’t know
how to do it. m.

Many thanks, I had some success by typing out the script myself and this
eliminated the troublesome characters. The script now runs and produces
my three text files. I still can’t get it to work as desired - it seems
to omit the lines of the calendar that I most need, i.e. the events
themselves! I don’t understand enough about the script to be able to fix
it but I think the error comes where the script splits the text feeds
into separate lines - it only seems to include the first line of the
feed and never more. If you fancy taking a look then by all means do but
I appreciate you fixing my first problem.

For example, the feed for ‘today’ is as follows in its entirety (in the
web browser, before ruby has touched it):

CAR:
[17/09/2007] Car to Garage at 8.30am

But the ruby script returns only:

Today:
CAR:

Any ideas?

Nathan Thickett-menghini [email protected] wrote:

For example, the feed for ‘today’ is as follows in its entirety (in the
Today:
CAR:

Any ideas?

Attachments:
http://www.ruby-forum.com/attachment/299/coloured_actions.rb

Well, the script is doing exactly what you’re telling it to do. This
expression:

select{|line|line =~ /\s+[Due:/ or line =~ /^\w/}

…means:

Keep only those lines that begin with the word “[Due:” (possibly after
some whitespace), or that begin with a word character (e.g. “a” or “b”
or “c”…).

Well, the first line does begin with a word character (“C” for “CAR:”),
so it’s kept. But the second line does not - it begins with whitespace,
and the whitespace is not followed by the word “[Due:”, so it’s thrown
away.

So it seems as if the script you’ve adopted is inappropriate to the
format of the actual data you wish to parse.

m.

That’s brilliant thanks! So it seems to me that the Tracks Developers
have changed the format of the feeds since the script was written,
omitting the word ‘Due:’. How can I get it to include all lines that
begin with white space? Many thanks, you’ve been fantastic!

Many thanks - you’re helping me hugely. I’ve gone through the script and
made the changes you suggested and it has worked in the most part. The
part of the script that parses the lines is now as follows:

today = curl -s "#{URL}"?due=0.split("\n").select{|line| line =~
/\s+/}
tomorrow = curl -s "#{URL}"?due=1.split("\n").select{|line| line =~
/\s+/}
this_week = curl -s "#{URL}"?due=6.split("\n").select{|line| line =~
/\s+/}
later = curl -s "#{URL}"?due=365.split("\n").select{|line| line =~
/\s+/}

I’m sure this is not perfect but it seems to do the trick of including
the lines that begin with blank space. Let me show you the feed as you
requested.

HOME:
[18/09/2007] Parcelforce coming after 1pm
[19/09/2007] be a bender
[21/09/2007] Sort important documents etc

WORK:
[24/09/2007] Learn NATS Stuff

CAR:
[30/09/2007] nob

So you see there are lines that begin with letters (these are the
contexts, Home, Work, etc) lines with a small gap followed by events,
and lines that are entirely blank.

You’ll have noticed that part of the script deletes duplicate lines -
so that you don’t get an event that is on today’s feed repeated on the
weekly feed. This works mostly but it also deletes repeated CONTEXTS,
i.e. the context ‘HOME:’ will appear on today’s feed but not on any
others. I’ve tried to get round this by totally abandoning all contexts,
i.e. changing the part that parses the lines to NOT INCLUDE lines that
begin with word characters. This works, but it now still includes the
totally empty lines, which puts gaps in my text files. Could you add a
tiny part to the following line to show me how to NOT include empty
lines? Is this possible? I know it seems like a lot of effort for a
small problem, but I’d really appreciate it, it will make me more
productive to see my events neatly on my desktop.

today = curl -s "#{URL}"?due=0.split("\n").select{|line| line =~
/\s+/}

Nathan Thickett-menghini [email protected] wrote:

That’s brilliant thanks! So it seems to me that the Tracks Developers
have changed the format of the feeds since the script was written,
omitting the word ‘Due:’. How can I get it to include all lines that
begin with white space?

It’s hard to say, because I don’t know the format of the feeds. You
could change the script you have, just deleting the six character phrase
“[Due:” wherever you see it. But that is just a guess, not knowing what
the feeds look like. It would be better if you’d show us a multi-line
feed output so we can see what the desirable and undesirable lines look
like. In the example you gave, all lines were desirable, which made
some of the program wasteful. But perhaps in a fuller example, there are
some undesirable lines. The change I’m suggesting might not eliminate
them; it depends what they look like.

m.

Nathan Thickett-menghini [email protected] wrote:

begin with word characters. This works, but it now still includes the
totally empty lines, which puts gaps in my text files. Could you add a

In my view the entire original script is misconceived. You can’t just
look at individual lines. You need to cycle thru the lines in order,
gathering info as categorized. Something, perhaps, like this:

cats = {}
today = curl -s "#{URL}"?due=0
today.split(“\n”).inject do |memo, line|
line.lstrip!
if line =~ /^\w*:confused:
cats[line] = []
line
else
cats[memo] |= line.to_a
memo
end
end

Now “cats” holds a nice hash where the keys are the categories and each
category’s value is an array of its events. Do that again for “tomorrow”
and the others, using the same “cats”; no duplicate values will be
inserted, because of the use of the |= operator. When you’re all
finished, output “cats” in whatever format you like, such as:

cats.each do |cat, events|
puts cat
events.each do |event|
puts " " + event
end
end

If you wanted to get really neat, you could sort stuff before
outputting.

m.