Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to import FoxClocks clocks #21

Open
aaferrari opened this issue Nov 24, 2024 · 0 comments
Open

Option to import FoxClocks clocks #21

aaferrari opened this issue Nov 24, 2024 · 0 comments

Comments

@aaferrari
Copy link

Something I did when I first installed the application was to make a script to parse the FoxClocks configuration file* and convert each clock into a SQL statement to then insert them into the app database (since adding the clocks manually was going to be very time consuming).

The problem with this is that it's impractical since it requires having a rooted phone, so it could be very useful to have an option to import the FoxClocks clocks directly into the app.

For what it's worth, the parser could be done with this code (adapted version of the Python script I originally made):

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FoxclocksDataParser {
    private static final Pattern extractor = Pattern.compile(
        "id=\"([^\"]+)\"/>(?:<Name>([^<]+)</Name>)?<Coordinates latitude=\"([^\"]+)\" longitude=\"([^\"]+)\"|<Location><Zone id=\"([^\"]+)\"/></Location>"
    );

    public static void dataParser(String data) {
        Matcher matcher = extractor.matcher(data);
        
        while (matcher.find()) {
            String timezoneId = matcher.group(1);
            String name = matcher.group(2) == null ? "" : matcher.group(2);
            String latitude = matcher.group(3);
            String longitude = matcher.group(4);
            
            String city = "";
            if (timezoneId != null) {
                String[] parts = timezoneId.split("/");
                if (parts.length > 1) {
                    city = parts[1].replace("_", " ");
                }
            }
            
            // UTC is a special case
            if (matcher.group(0).indexOf("Etc/UTC") > -1) {
                timezoneId = "GMT";
                city = "UTC";
                name = "Coordinated Universal Time";
                latitude = longitude = "0.0";
            }

            System.out.printf(
                "insert into clocks (time_diff, timezone_id, city, area, latitude, longitude) " +
                "values (0, '%s', '%s', '%s', '%s', '%s');%n",
                timezoneId, city, name, latitude, longitude
            );
        }
    }

    public static void main(String[] args) {
        // Use example
        if (args.length >= 1) {
            BufferedReader objReader = null;
            try {
                String strCurrentLine;
                objReader = new BufferedReader(new FileReader(args[0]));
                StringBuilder stringBuilder = new StringBuilder();
                while ((strCurrentLine = objReader.readLine()) != null) {
                    stringBuilder.append(strCurrentLine);
                    stringBuilder.append("\n");
                }
                dataParser(stringBuilder.toString());
                
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (objReader != null) { objReader.close(); }
                }
                catch (IOException ex) { ex.printStackTrace(); }
            }
        }
        else { System.out.println("A file is needed to parse. Exiting"); }
    }
}

To test it just save it as FoxclocksDataParser.java and run the following:

javac FoxclocksDataParser.java
java FoxclocksDataParser /path/to/the/foxclocks/config

Which returns an insert statement for each clock found. When inserting that in /data/data/ch.corten.aha.worldclock/databases/worldclock, the result is the following after restarting the application:
Screenshot_2024-11-24-10-13-41
I also take the opportunity to leave the configuration file* with which I did this and to be able to test the example code.

  • The file is from an old/legacy version of FoxClocks, since when they migrated it to WebExtensions the option to import/export the configuration disappeared or was never integrated (at least in the Firefox extension, but it is possible that it is the same in other browsers).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant