<commit_before>require 'sun_times'
require 'date'
require_relative '../conf/cacheConf.rb'
require 'timezone'


class Location
    attr_reader :latitude
    attr_reader :longitude
    attr_reader :code
    attr_reader :name
    attr_reader :timezone

    DATA_LOCATION = $cacheConf['cityList']['dataLocation']


    def initialize(code, name, lat, long, skipTZ = FALSE)
        @code = code
        @name = name
        @latitude = lat.to_f
        @longitude = long.to_f


        @timezone = Timezone::Zone.new(:latlong => [@latitude, @longitude])
    end

    def self.configureTimeZoneLookup
        require_relative '../conf/timezoneGoogleApiConf.rb'
        Timezone::Lookup.config(:google) do |c|
            c.api_key = $googleAPIkey
        end
    end

    def self.testTimeZoneLookup
        Timezone.lookup(-34.92771808058, 138.477041423321).name == "Australia/Adelaide"
    end

    

    def getCode
        @code
    end

    def getName
        @name
    end<commit_msg>we can now save timezone data for offline usage<commit_after>require 'sun_times'
require 'date'
require_relative '../conf/cacheConf.rb'
require 'timezone'


class Location
    attr_reader :latitude
    attr_reader :longitude
    attr_reader :code
    attr_reader :name

    # Path of local copy of EC location file
    DATA_LOCATION = $cacheConf['cityList']['dataLocation']

    # CSV file that includes timezone data and hopefully will replace the above one day
    CSV_LOCATION = File.expand_path('~/.tlaloc.locationData.tsv')
    CSV_OPTIONS = {col_sep: "\t", row_sep: "\n", headers: true, encoding: "UTF-8", header_converters: :symbol}


    def initialize(code, name, lat, long, skipTZ = FALSE)
        @code = code
        @name = name
        @latitude = lat.to_f
        @longitude = long.to_f


    end

    def timezone()
        begin
            @timezone or @timezone = Timezone.lookup(@latitude, @longitude)
        rescue Timezone::Error::InvalidZone
            puts "Invalid zone for #{self.to_s}"
            raise
        end
    end

    ########################################################################
    # Generate a string that contains all the timezone data per city code
    ########################################################################
    def self.updateTimeZoneData(seperator = "\t")
        Location::configureTimeZoneLookup()
        locations = Location::createLocations('')
        existingLocations = Location::readLocationData()
        str = ''
        i=0
        locations.each { |locationCode, locObj |
            i += 1
            next if locObj.latitude == 0.0 

            # Check if we genuinely have a location object
            if locObj.nil? 
                $stderr.puts "Nil location object for #{locationCode}"
                puts
                next
            end

            if not existingLocations[locObj.code].nil?
                puts "Already have:\t#{locObj.to_s}"
                next
            end


            print "#{i} / #{locations.count}: #{locObj.to_s} "
            print "Fetching... "

            begin
                str += (locationCode + seperator + locObj.timezone.name).chomp + "\n"
            rescue Timezone::Error::InvalidZone
                sleep 1
                next
            end

            print "Obtained... "
            self.appendLocationData(locObj)
            puts "Saved."
            sleep 1
        }

        return str
    end

    def to_s
        "#{@code}: #{@name} (#{@latitude}; #{@longitude})"
    end

    def self.readLocationData()
        require 'csv'
        returnHash = {}
        if File.file?(Location::CSV_LOCATION)
            CSV.foreach(Location::CSV_LOCATION, Location::CSV_OPTIONS) do |row|
                rowHash = row.to_hash
                returnHash[rowHash[:code]] = rowHash
            end
        end
        return returnHash
    end

    def self.appendLocationData(locObj)
        require 'csv'

        # If the file does not exist, create it with headers
        if not File.file?(Location::CSV_LOCATION)
            CSV.open(Location::CSV_LOCATION, "w", Location::CSV_OPTIONS) do |csv|
                csv << ["code", "timezoneName", "latitude", "longitude"]
            end
        end

        # Append the last bit of location data
        CSV.open(Location::CSV_LOCATION, "a+", Location::CSV_OPTIONS) do |csv|
            csv << [locObj.code, locObj.timezone.name, locObj.latitude, locObj.longitude]
        end
    end

    ########################################################################
    # Test if the timezone lookup has been configured. 
    # If not, catches the error and returns false
    ########################################################################
    def self.timeZoneLookupReady?()
        begin
            Location::testTimeZoneLookup
            # TODO: What if the above returns false?  Name of test time zone has changed.
        rescue Timezone::Error::InvalidConfig
            return false
        end
    end


    ########################################################################
    # Configures the time zone lookup IF not already set up
    ########################################################################
    def self.configureTimeZoneLookup
        if not Location::timeZoneLookupReady?()
            require_relative '../conf/timezoneGoogleApiConf.rb'
            Timezone::Lookup.config(:google) do |c|
                c.api_key = $googleAPIkey
                c.open_timeout = 60
                c.read_timeout = 60
            end
        end
    end

    ########################################################################
    # Returns true if the location is working correctly
    ########################################################################
    def self.testTimeZoneLookup
        Timezone.lookup(-34.92771808058, 138.477041423321).name == "Australia/Adelaide"
    end

    
{'fraction_non_alphanumeric': 0.18095881161377447, 'fraction_numerical': 0.011309925725860905, 'mean_word_length': 2.2825484764542936, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4288968', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1538, 'n_words': 407}