[erlang-questions] page counter - thx for everyones help

Colm Dougan colm.dougan@REDACTED
Sat May 1 00:17:43 CEST 2010


Wes,

It would be typical to move the "extract page count" code into a
function.  Depending on how happy you are depend on the format of the
page you are scraping you could use something like this (see below).
Note: I've used the default "http" library here rather than "ibrowse"
as I don't have ibrowse installed.  If you decide to use "re" instead
then you only have to change the extract_page_count function.

Typically lists:nth isn't very erlangy.  It would probably be more
typical (and more tolerant to the page changing) to iterate over Lines
looking for a given pattern.

Colm

-define(MAGIC_LINE_NUMBER, 424).
-define(URL, ""http://192.168.1.1/printer/configu.html?autoref=0&weblang=0").

getc() ->
        inets:start(),
        {ok, {_StatusLine, _Headers, Body}} = http:request(?URL),
        Lines = string:tokens(Body, "\r\n"),
        PageCount = extract_page_count(lists:nth(?MAGIC_LINE_NUMBER, Lines)),
        io:format("COUNT [~p]~n", [PageCount]),
        ok.

extract_page_count("<B>Page Counter</B></TD><TD>" ++ Rest) ->
        {Count, _} = string:to_integer(Rest),
        Count.



On Fri, Apr 30, 2010 at 12:17 PM, Wes James <comptekki@REDACTED> wrote:
> Here is the code so far:
>
> -module('gpc').
>
> -export([start/0, start/1, getc/0]).
>
>
> start() ->
>        ibrowse:start(),
>        timer:apply_interval(3000, gpc, getc, []).
>
> start(Interval) ->
>        ibrowse:start(),
>        timer:apply_interval(Interval*1000, gpc, getc, []).
>
> getc() ->
>        S = string:substr(
>                binary_to_list(
>                        lists:nth(
>                                424,
>                                re:replace(
>                                        erlang:element(
>                                                4,
>                                                ibrowse:send_req("http://192.168.1.1/printer/configu.html?autoref=0&weblang=0",
> [], get)
>                                        ),
>                                        "\\n",
>                                        "",
>                                        [global]
>                                )
>                        )
>                ),
>        34),
>        S2=list_to_integer(string:substr(S, 1, string:len(S)-10)),
>        io:format("~w~n", [S2]).
>
>
> So this by default gives me a page count every 3 seconds or I can put
> in gpc:start(10) for 10 second intervals.
>
> What I would like to do is take daily page counts and stick them some
> where.  I could make a file on the system with the date as the name
> (20100430.txt) and just write the value there or I could write the
> value to mnesia and keep track via the data base.  I would be doing a
> current count - yesterday count to get a total count for just each
> day.  I'm doing this so I can keep track of what the students in a
> university computer lab are printing each day.
>
> If you have any suggestions, please drop them in this list :)
>
> I'll be experimenting anyway since I've yet to read or write to a file
> with erlang on a disc or gone beyond just reading what mnesia can do
> via web page info.
>
> thx,
>
> -wes
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list