check html file size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    check html file size

    would anyone like to translate the following perl script to Python or
    Scheme (scsh)?

    the file takes a inpath, and report all html files in it above certain
    size. (counting inline images)
    also print a sorted report of html files and their size.

    (a copy of the script is here:

    )

    Xah
    xah@xahlee.org
    ∑ http://xahlee.org/


    # perl

    # Tue Oct 4 14:36:48 PDT 2005
    # given a dir, report all html file's size. (counting inline images)
    # XahLee.org

    use Data::Dumper;
    use File::Find;
    use File::Basename;

    $inpath = '/Users/t/web/mydirectory/';
    $sizeLimit = 800 * 1000;

    # $inpath = $ARGV[0]; # should give a full path; else the
    $File::Find::di r won't give full path.
    while ($inpath =~ m@^(.+)/$@) { $inpath = $1;} # get rid of trailing
    slash

    die "dir $inpath doesn't exist! $!" unless -e $inpath;


    ############### ############### ############### #####
    # subroutines


    # getInlineImg($f ile_full_path) returns a array that is a list of
    inline images. For example, it may return ('xx.jpg','../image.png')
    sub getInlineImg ($) { $full_file_name = $_[0];
    @linx =(); open (FF, "<$full_file_na me") or die "error: can not open
    $full_file_name $!";
    while (<FF>) { @txt_segs = split(m/img/, $_); shift @txt_segs;
    for $lin (@txt_segs) { if ($lin =~ m@ src\s*=\s*\"([^\"]+)\"@i) {
    push @linx, $1; }}
    } close FF;
    return @linx;
    }

    # linkFullPath($d ir,$locallink) returns a string that is the full path
    to the local link. For example,
    linkFullPath('/Users/t/public_html/a/b', '../image/t.png') returns
    'Users/t/public_html/a/image/t.png'. The returned result will not
    contain double slash or '../' string.
    sub linkFullPath($$ ){ $result=$_[0] . $_[1]; while ($result =~
    s@\/\/@\/@) {}; while ($result =~ s@/[^\/]+\/\.\.@@) {}; return
    $result;}


    # listLocalLinks( $html_file_full _path) returns a array where each
    element is a full path of local links in the html.
    sub listLocalLinks( $) {
    my $htmlfile= $_[0];

    my ($name, $dir, $suffix) = fileparse($html file, ('\.html') );
    my @aa = getlinks($htmlf ile);
    @aa = grep(!m/\#/, @aa);
    @aa = grep (!m/^mailto:/, @aa);
    @aa = grep (!m/^http:/, @aa);

    my @linkedFiles=() ;
    foreach my $lix (@aa) { push @linkedFiles, linkFullPath($d ir,$lix);}
    return @linkedFiles;
    }


    # listInlineImg($ html_file_full_ path) returns a array where each
    element is a full path to inline images in the html.
    sub listInlineImg($ ) {
    my $htmlfile= $_[0];

    my ($name, $dir, $suffix) = fileparse($html file, ('\.html') );
    my @aa = getInlineImg($h tmlfile);

    my @result=();
    foreach my $ele (@aa) { push @result, linkFullPath($d ir,$ele);}
    return @result;
    }

    ############### ############### ############### #####
    sub checkLink {
    if (
    -T $File::Find::na me
    && $File::Find::na me =~ m@\.html$@
    ) {
    $total= -s $File::Find::na me;
    @h2 = listInlineImg($ File::Find::nam e);
    for my $ln (@h2) {$total += -s $ln;};
    if ( $total > $sizeLimit) {print "problem: file:
    $File::Find::na me, size: $total\n";}

    push (@result, [$total, $File::Find::na me]);
    };
    }

    find(\&checkLin k, $inpath);

    @result = sort { $b->[0] <=> $a->[0]} @result;

    print Dumper(\@result );
    print "done reporting. (any file above size are printed above.)";

    __END__

  • Matt Garrish

    #2
    Re: check html file size


    "Xah Lee" <xah@xahlee.org > wrote in message
    news:1128473042 .653989.175990@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > would anyone like to translate the following perl script to Python or
    > Scheme (scsh)?[/color]

    Even if you weren't an incredibly offensive and petulant poster, what makes
    you think anyone would write a script from you?

    Matt


    Comment

    • Grant Edwards

      #3
      Re: check html file size

      On 2005-10-05, Xah Lee <xah@xahlee.org > wrote:
      [color=blue]
      > would anyone like to translate the following perl script to
      > Python or Scheme (scsh)?[/color]

      Sure. It'll cost you $110/hour with a 2-hour minimum. Where do
      I send the invoice?

      --
      Grant Edwards grante Yow! I'll take ROAST BEEF
      at if you're out of LAMB!!
      visi.com

      Comment

      • Erik Max Francis

        #4
        Re: check html file size

        Matt Garrish wrote:
        [color=blue]
        > Even if you weren't an incredibly offensive and petulant poster, what makes
        > you think anyone would write a script from you?[/color]

        Because in addition to being offensive and petulant, he's also an idiot.

        --
        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
        There is no fate that cannot be surmounted by scorn.
        -- Albert Camus

        Comment

        • Tad McClellan

          #5
          Re: check html file size

          Xah Lee <xah@xahlee.org > wrote:
          [color=blue]
          > would anyone like to translate the following perl script to Python or
          > Scheme (scsh)?[/color]


          Yes, I would.


          --
          Tad McClellan SGML consulting
          tadmc@augustmai l.com Perl programming
          Fort Worth, Texas

          Comment

          • Richard Gration

            #6
            Re: check html file size

            On Tue, 04 Oct 2005 17:44:02 -0700, Xah Lee wrote:
            [color=blue]
            > would anyone like to translate the following perl script to Python or
            > Scheme (scsh)?[/color]

            Are you fucking seriously fucking expecting some fucking moron to
            translate your tech geeking fucking code moronicity? Fucking try writing
            it fucking properly in fucking Perl first.

            --
            I guess everybody's the same: Gotta be good at your job before you can enjoy the rest of your life
            -- Cole Trickle

            Comment

            • Ulrich Hobelmann

              #7
              Re: check html file size

              Richard Gration wrote:[color=blue]
              > Are you fucking seriously fucking expecting some fucking moron to
              > translate your tech geeking fucking code moronicity? Fucking try writing
              > it fucking properly in fucking Perl first.[/color]

              Fucking excuse me?

              Fucking maybe you should fucking go fucking fuck your fucking self...

              Seriously, Xah might be a troll, but this is just pathetic.

              --
              We're glad that graduates already know Java,
              so we only have to teach them how to program.
              somewhere in a German company
              (credit to M. Felleisen and M. Sperber)

              Comment

              • Sherm Pendley

                #8
                Re: check html file size

                Richard Gration <richard@zync.c o.uk> writes:
                [color=blue]
                > Are you fucking seriously fucking expecting some fucking moron to
                > translate your tech geeking fucking code moronicity? Fucking try writing
                > it fucking properly in fucking Perl first.[/color]

                Good fucking job! That's the funniest fucking response I've ever fucking seen
                to Xah's fucking moronistic fucking nonsense.

                Lenny Bruce would be so fucking proud.

                sherm--

                --
                Cocoa programming in Perl: http://camelbones.sourceforge.net
                Hire me! My resume: http://www.dot-app.org

                Comment

                • Sherm Pendley

                  #9
                  Re: check html file size

                  Ulrich Hobelmann <u.hobelmann@we b.de> writes:
                  [color=blue]
                  > Richard Gration wrote:[color=green]
                  >> Are you fucking seriously fucking expecting some fucking moron to
                  >> translate your tech geeking fucking code moronicity? Fucking try writing
                  >> it fucking properly in fucking Perl first.[/color]
                  >
                  > Fucking excuse me?
                  >
                  > Fucking maybe you should fucking go fucking fuck your fucking self...
                  >
                  > Seriously, Xah might be a troll, but this is just pathetic.[/color]

                  I'm guessing you didn't get the joke then. I think Richard's response was a
                  parody of Xah's "style" - a funny parody, at that.

                  sherm--

                  --
                  Cocoa programming in Perl: http://camelbones.sourceforge.net
                  Hire me! My resume: http://www.dot-app.org

                  Comment

                  • Ray Dillinger

                    #10
                    Re: check html file size

                    Richard Gration wrote:
                    [color=blue]
                    > ... fucking ... fucking ... fucking ... fucking ... Fucking ... fucking
                    > ... fucking[/color]

                    My friend, you can learn to use a far richer vocabulary of
                    obscenities. If your creative flow is blocked by the fear
                    that you can't spell more dirty words correctly, you can
                    dispel this fear with a few evenings of study and preparation.

                    Amaze your friends! Amuse your enemies! Enrich your
                    vocabulary! You can learn the joys of cussing seven
                    times in the same sentence without resorting to repetition!
                    For extra points, and with suitable study, you can even
                    learn to write entire paragraphs of _original_ obscenity!

                    Just imagine how much clearer your point would have been if
                    you'd called him a jizz-licking dogcock grabber! Why insult
                    his code with a vague word like "moronicity " when you could
                    use "steaming pile of entrails" or better yet, "bucket of
                    fermented ballsweat?" wouldn't that have made your technical
                    point much clearer?

                    Now go, and don't attempt obscenity in public again until
                    you learn how.

                    Bear


                    Comment

                    • Ulrich Hobelmann

                      #11
                      Re: check html file size

                      Sherm Pendley wrote:[color=blue]
                      > I'm guessing you didn't get the joke then. I think Richard's response was a
                      > parody of Xah's "style" - a funny parody, at that.[/color]

                      If you take all the line noise in Perl as swearing ;)
                      I suppose I'm lucky I can't read it.

                      --
                      We're glad that graduates already know Java,
                      so we only have to teach them how to program.
                      somewhere in a German company
                      (credit to M. Felleisen and M. Sperber)

                      Comment

                      • Richard Gration

                        #12
                        Re: check html file size

                        On Wed, 05 Oct 2005 20:39:18 -0400, Sherm Pendley wrote:
                        [color=blue]
                        > Richard Gration <richard@zync.c o.uk> writes:
                        >[color=green]
                        >> Are you fucking seriously fucking expecting some fucking moron to
                        >> translate your tech geeking fucking code moronicity? Fucking try writing
                        >> it fucking properly in fucking Perl first.[/color]
                        >
                        > Good fucking job! That's the funniest fucking response I've ever fucking seen
                        > to Xah's fucking moronistic fucking nonsense.[/color]

                        Thanks, Sherm. I knew someone would get it. I think Bear and Ulrich
                        haven't yet been exposed to Xah "in full effect" ;-) They're probably
                        denizens of the Scheme group which seems to be a new entry on Xah's "this
                        newsgroup needs spamming" list ;-)
                        [color=blue]
                        > Lenny Bruce would be so fucking proud.[/color]

                        LOL

                        Comment

                        • Xah Lee

                          #13
                          Re: check html file size

                          Xah Lee wrote: « would anyone like to translate the following perl
                          script to Python or Scheme (scsh)?»

                          Here's the Python version.

                          # -*- coding: utf-8 -*-
                          # Python


                          # Wed Oct 5 15:50:31 PDT 2005
                          # given a dir, report all html file's size. (counting inline images)
                          # XahLee.org

                          import re, os.path, sys

                          inpath= '/Users/t/web/'

                          while inpath[-1] == '/': inpath = inpath[0:-1] # get rid of trailing
                          slash

                          if (not os.path.exists( inpath)):
                          print "dir " + inpath + " doesn't exist!"
                          sys.exit(1)

                          ############### ############### ############### #####
                          # subroutines


                          def getInlineImg(fi le_full_path):
                          '''getInlineImg ($file_full_pat h) returns a array that is a list of
                          inline images. For example, it may return ['xx.jpg','../image.png']'''

                          FF = open(file_full_ path,'rb')
                          txt_segs = re.split( r'src', unicode(FF.read (),'utf-8'))
                          txt_segs.pop(0)
                          FF.close()
                          linx=[]
                          for linkBlock in txt_segs:
                          matchResult = re.search(r'\s* =\s*\"([^\"]+)\"', linkBlock)
                          if matchResult: linx.append( matchResult.gro up(1) )
                          return linx


                          def linkFullPath(di r,locallink):
                          '''linkFullPath (dir, locallink) returns a string that is the full
                          path to the local link. For example,
                          linkFullPath('/Users/t/public_html/a/b', '../image/t.png') returns
                          'Users/t/public_html/a/image/t.png'. The returned result will not
                          contain double slash or '../' string.'''
                          result = dir + '/' + locallink
                          result = re.sub(r'//+', r'/', result)
                          while re.search(r'/[^\/]+\/\.\.', result): result =
                          re.sub(r'/[^\/]+\/\.\.', '', result)
                          return result

                          def listInlineImg(h tmlfile):
                          '''listInlineIm g($html_file_fu ll_path) returns a array where each
                          element is a full path to inline images in the html.'''
                          dir=os.path.dir name(htmlfile)
                          imgPaths = getInlineImg(ht mlfile)
                          result = []
                          for aPath in imgPaths:
                          result.append(l inkFullPath( dir, aPath))
                          return result


                          ############### ############### ############### #####
                          # main

                          fileSizeList=[]
                          def checkLink(dummy , dirPath, fileList):
                          for fileName in fileList:
                          if '.html' == os.path.splitex t(fileName)[1] and
                          os.path.isfile( dirPath+'/'+fileName):
                          totalSize = os.path.getsize (dirPath+'/'+fileName)
                          imagePathList = listInlineImg(d irPath+'/'+fileName)
                          for imgPath in imagePathList: totalSize +=
                          os.path.getsize (imgPath)
                          fileSizeList.ap pend([totalSize, dirPath+'/'+fileName])


                          os.path.walk(in path, checkLink, 'dummy')

                          fileSizeList.so rt(key=lambda x:x[0],reverse=True)

                          for it in fileSizeList: print it
                          print "done reporting."



                          -------------------------------------------------
                          This Python version is a direct translation of the Perl version. They
                          match pretty much line by line.

                          for both the Python version and the Perl version, see:


                          Would any lisper provides a Scheme version? i don't think i'll do a
                          Scheme version anytime soon. Please, Schemers, show us some fanfare.

                          Xah
                          xah@xahlee.org
                          ∑ http://xahlee.org/

                          Comment

                          Working...