! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
-USING: kernel sequences io.files io.files.temp io.launcher
-io.pathnames io.encodings.ascii io.streams.string http.client
-generalizations combinators math.parser math.vectors
-math.intervals interval-maps memoize csv accessors assocs
-strings math splitting grouping arrays combinators.smart ;
+USING: accessors arrays assocs combinators combinators.smart csv
+grouping http.client interval-maps io.encodings.ascii io.files
+io.files.temp io.launcher io.pathnames ip-parser kernel math
+math.parser memoize sequences strings ;
IN: geo-ip
-: db-path ( -- path ) "IpToCountry.csv" temp-file ;
+: db-path ( -- path ) "IpToCountry.csv" cache-file ;
CONSTANT: db-url "http://software77.net/geo-ip/?DL=1"
MEMO: ip-db ( -- seq )
download-db ascii file-lines
- [ "#" head? not ] filter "\n" join <string-reader> csv
+ [ "#" head? ] reject "\n" join string>csv
[ parse-ip-entry ] map ;
: filter-overlaps ( alist -- alist' )
GENERIC: lookup-ip ( ip -- ip-entry )
-M: string lookup-ip
- "." split [ string>number ] map
- { HEX: 1000000 HEX: 10000 HEX: 100 HEX: 1 } v.
- lookup-ip ;
+M: string lookup-ip ipv4-aton lookup-ip ;
M: integer lookup-ip ip-intervals interval-at ;