0

So, I'm trying to use Maxmind GeoLiteCity.dat through GeoIP gem: https://github.com/cjheath/geoip And, what bothers me: How would I possibly return the list of all countries (this is fairly easy, since there is an appropriate YAML file down there) and cities - but I don't have a clue, how to read the city list. All I know is that they are somehow stored in this .dat file

Can anyone help me with this one ? Where and how should I look ?

Dmitri
  • 2,451
  • 6
  • 35
  • 55

1 Answers1

1

.dat file is a database file where, in one way, contents are stored in table form, but in compressed form. Just find out which columns refers to countries and cities. In following links, there are similar questions:

Ruby sorting a .dat file by column

How can I handle large files in Ruby?

Getting a user country name from originating IP address with Ruby on Rails

Refer to above links and modify the codes to suit your needs.

Community
  • 1
  • 1
RAM
  • 2,413
  • 1
  • 21
  • 33
  • Other than adding a couple of links can you provide some working code and comment it? – Yaroslav Oct 09 '12 at 06:57
  • Thank you. Will definitely take a look, but, still - maybe someone could provide a working code example ? – Dmitri Oct 09 '12 at 07:05
  • Just found out that this question is already answered in StackOverflow. Here is the link: http://stackoverflow.com/questions/12293211/can-any-body-tell-me-how-to-open-a-file-with-dat-extension-in-cent-os – RAM Oct 09 '12 at 07:25
  • Well, readlines outputs something wierd (wierd format). Now how to decrypt it ? Ugh, but thanks again – Dmitri Oct 09 '12 at 07:56
  • If you read .dat file, you can use .city to get the city attribute. Refer to this link for more information: http://rubydoc.info/gems/geoip/1.1.2/GeoIP/City. Otherwise, you read .csv file, normal read operations like readline can be used and then, parsed according to the formats defined. – RAM Oct 09 '12 at 09:02
  • I don't need to get a city for a specific IP, I need to get the list of cities (and countries as well). So should I stick with .csv version ? – Dmitri Oct 09 '12 at 11:01
  • For now, you can use .csv version since it meets your needs at least, albeit at the cost of speed. Later on, you can explore further by looking at .dat version and then improve/optimize your codebase. – RAM Oct 12 '12 at 05:21