<commit_before>package org.helioviewer.jhv.plugins.swek.sources.hek;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.helioviewer.jhv.base.logging.Log;
import org.helioviewer.jhv.base.time.TimeUtils;
import org.helioviewer.jhv.data.datatype.event.JHVEventType;
import org.helioviewer.jhv.data.datatype.event.SWEKEventType;
import org.helioviewer.jhv.database.JHVDatabase;
import org.helioviewer.jhv.plugins.swek.download.SWEKParam;
import org.helioviewer.jhv.plugins.swek.sources.SWEKDownloader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HEKDownloader extends SWEKDownloader {

    @Override
    protected boolean parseEvents(JSONObject eventJSON, JHVEventType type) {
        JSONArray results = eventJSON.getJSONArray("result");

        for (int i = 0; i < results.length(); i++) {
            JSONObject result = results.getJSONObject(i);

            String uid = result.getString("kb_archivid");
            long start;
            long end;
            try {
                start = TimeUtils.utcDateFormat.parse(result.getString("event_starttime")).getTime();
                end = TimeUtils.utcDateFormat.parse(result.getString("event_endtime")).getTime();
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            } catch (ParseException e) {
                e.printStackTrace();
                return false;
            }

            if (end - start > 3 * 24 * 60 * 60 * 1000) {
                Log.error("Possible wrong parsing of a HEK event.");
                Log.error("Event start: " + start);
                Log.error("Event end: " + end);
                Log.error("Event JSON: ");
                Log.error(result.toString());
                return false;
            }
            byte[] compressedJson;
            try {
                compressedJson = JHVDatabase.compress(result.toString());
            } catch (IOException e) {
                Log.error("compression error");
                return false;
            }
            int id = JHVDatabase.dump_event2db(compressedJson, start, end, uid, type);
            if (id == -1) {
                Log.error("failed to dump to database");
                return false;
            }<commit_msg>reduce spawning console on error paths<commit_after>package org.helioviewer.jhv.plugins.swek.sources.hek;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.helioviewer.jhv.base.logging.Log;
import org.helioviewer.jhv.base.time.TimeUtils;
import org.helioviewer.jhv.data.datatype.event.JHVEventType;
import org.helioviewer.jhv.data.datatype.event.SWEKEventType;
import org.helioviewer.jhv.database.JHVDatabase;
import org.helioviewer.jhv.plugins.swek.download.SWEKParam;
import org.helioviewer.jhv.plugins.swek.sources.SWEKDownloader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HEKDownloader extends SWEKDownloader {

    @Override
    protected boolean parseEvents(JSONObject eventJSON, JHVEventType type) {
        JSONArray results = eventJSON.getJSONArray("result");

        for (int i = 0; i < results.length(); i++) {
            JSONObject result = results.getJSONObject(i);

            String uid = result.getString("kb_archivid");
            long start;
            long end;
            try {
                start = TimeUtils.utcDateFormat.parse(result.getString("event_starttime")).getTime();
                end = TimeUtils.utcDateFormat.parse(result.getString("event_endtime")).getTime();
            } catch (JSONException e) {
                return false;
            } catch (ParseException e) {
                return false;
            }

            if (end - start > 3 * 24 * 60 * 60 * 1000) {
                /*
                Log.error("Possible wrong parsing of a HEK event.");
                Log.error("Event start: " + start);
                Log.error("Event end: " + end);
                Log.error("Event JSON: ");
                Log.error(result.toString());
                 */
                return false;
            }
            byte[] compressedJson;
            try {
                compressedJson = JHVDatabase.compress(result.toString());
            } catch (IOException e) {
                Log.error("compression error");
                return false;
            }
            int id = JHVDatabase.dump_event2db(compressedJson, start, end, uid, type);
            if (id == -1) {
                Log.error("failed to dump to database");
                return false;
            }
{'fraction_non_alphanumeric': 0.10755336617405582, 'fraction_numerical': 0.005747126436781609, 'mean_word_length': 2.141843971631206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2684680', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1381, 'n_words': 279}