// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Script fuer den automatischen Download von Titeldaten aus dem CBS // // Historie: // 15-12-2009 js: created // 16-12-2009 js: added boolean _CONNECT_PER_SESSION to have a // choice between connect per session or per download // 18-12-2009 js: added download of related records (PER, LSW) by use of PPN // select by: _GET_PPN_RECORDS = [true|false] // 04-01-2010 js: date-calculation corrected // // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ++++++++++++++++++++++++++++ // +++++ AUTO START +++++ __startProcedure() // ++++++++++++++++++++++++++++ // Functions: // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // function __check_next_ym: return next YearMonth // function __connect: connect CBS-Host, return boolean // function __last_month: return last month // function __last_year: return last year // function __message: create messagebox // function __PPN_array_download: download records by use of ppn_array // function __PPN_dedup: deduplicate PPN's in ppn_array // function __PPN_prepare: prepare ppn_array // function __PPN_select: select PPN's from title record // function __startProcedure: main procedure for automatic download // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ function __check_next_ym(y, m) { var year = y; var month = m; year = (month == "12") ? String(parseInt(year,10) + 1) : year; month = (month == "12") ? "01" : String(parseInt(month,10) + 1); month = (month.length == 1) ? "0" + month : month; return(year + month); } function __connect(host, port, account, passwd, connect) { var connection; if (connect) { // try to connect connection = application.connect(host, port); if (connection) { application.activeWindow.variable("P3VUN") = account; application.activeWindow.variable("P3VWW") = passwd; // Press 3x the key to login application.activeWindow.simulateIBWKey("FR"); // Press the key to select "KAT" application.activeWindow.simulateIBWKey("FR"); // Press the key to select "GVK-Katalogisierung" application.activeWindow.simulateIBWKey("FR"); } } else connection = false; // __message("connect: " + connection); return(connection); } function __last_month(m) { var month = m; month = (month == "01") ? "12" : String(parseInt(month,10) -1); month = (month.length == 1) ? "0" + month : month; return(month); } function __last_year(y) { return(String(parseInt(y,10) -1)); } function __message(txt) { application.messageBox("Hinweis", txt, "message-icon"); } function __PPN_array_download(ppn_array, downloaddir, filename, format, key, windir) { // Download Records zu ppn_array var j; // zaehler var search_com; // Suchkommando var dwlfile = utility.newFileInput(); // Download-Filecheck j=ppn_array.length; switch (key) { case "PER": search_com = "f idn"; break; case "LSW": search_com = "f idn"; break; default: search_com = "f idn"; break; } // format und key zu filename setzen filename = filename + format + "_" + key; // __message("downloaddir + filename: " + downloaddir + filename); // check, ob download file schon vorhanden if (! dwlfile.open(downloaddir + filename + ".txt")) { // evt. vorhandenes tmp-File loeschen application.shellExecute("file:///" + windir + "system32\\cmd.exe", 6, "open", "/c del " + downloaddir + filename + ".tmp"); // Download-File in user_pref.js eintragen application.writeProfileString("winibw.filelocation", "download", "file:///" + downloaddir + filename + ".tmp"); for (i=0; i < j; i++) { application.activeWindow.command(search_com + " " + ppn_array[i], false); application.activeWindow.command("dow " + format, false); } // tmp-File erst nach abgeschlossenem Download umbenennen application.shellExecute("file:///" + windir + "system32\\cmd.exe", 6, "open", "/c rename " + downloaddir + filename + ".tmp " + filename + ".txt"); } else dwlfile.close(); } function __PPN_dedup(ppn_array){ // PPN deduplizieren var i,j,k; // zaehler try { j = ppn_array.length; } catch (e) { j = 0; } i=0; while (i < j) { k=i+1; while (k < j) { if (String(ppn_array[i]) == String(ppn_array[k])) { ppn_array[k] = ppn_array[ppn_array.length-1]; ppn_array.pop(); k--; j--; } k++; } i++; } // for (i=0; i 15) ? _MM : __last_month(_MM); //Endmonat Download _END_YEAR = ((_END_MONTH == 12) && (_MM == 1)) ? __last_year(_YYYY) : _YYYY; // Endjahr Download _DWL_16bis02 = (parseInt(_DD,10) > 1) && (_END_YEAR + _END_MONTH < _YYYY + _MM); // Endmonat Download in Folgemonat _YM = _START_YEAR + _START_MONTH; // Jahr+Monat Download while ( _YM <= _END_YEAR + _END_MONTH) { // __message("Download _YM/Format: " + _YM + "/" + _DOWNLOAD_FORMAT); // +++ Download Startmonat 2. bis 15. +++ _FILENAME = "dwl" + _START_YEAR + _START_MONTH + "02-15" + _DOWNLOAD_FORMAT; // check, ob download file schon vorhanden if (! _DWLFILE.open(_DOWNLOAD_DIR + _FILENAME + ".txt")) { // __message("Download-File: " + _DOWNLOAD_DIR + _FILENAME + ".txt"); if ( __connect(_HOST, _PORT, _ACCOUNT, _PASSWD, !_CONNECT_PER_SESSION) || _CONNECT_PER_SESSION) { // evt. vorhandenes tmp-File loeschen application.shellExecute("file:///" + _WINDIR + "system32\\cmd.exe", 6, "open", "/c del " + _DOWNLOAD_DIR + _FILENAME + ".tmp"); // Download-File in user_pref.js eintragen application.writeProfileString("winibw.filelocation", "download", "file:///" + _DOWNLOAD_DIR + _FILENAME + ".tmp"); // __message("f sel " + "02-" + _START_MONTH + "-" + _START_YEAR + " bis " + // "15-" + _START_MONTH + "-" + _START_YEAR + " " + _SELCODE); application.activeWindow.command("f sel " + "02-" + _START_MONTH + "-" + _START_YEAR + " bis " + "15-" + _START_MONTH + "-" + _START_YEAR + " " + _SELCODE, false); // SET-Daten ermitteln _SETNR = application.activeWindow.variable("P3GSE"); _SETSIZE = application.activeWindow.variable("P3GSZ"); // __message("_SETSIZE: " + _SETSIZE); if (_SETSIZE > 0 ){ // Download ausfuehren for (i=1; i <= _SETSIZE; i++) { application.activeWindow.command("dow " + i + " " + _DOWNLOAD_FORMAT, false); } // tmp-File erst nach abgeschlossenem Download umbenennen application.shellExecute("file:///" + _WINDIR + "system32\\cmd.exe", 6, "open", "/c rename " + _DOWNLOAD_DIR + _FILENAME + ".tmp " + _FILENAME + ".txt"); // Ueber PPN verlinkte Records auslesen if (_GET_PPN_RECORDS) { // 1. Personen 30xx // Array loeschen - max. 10.000 Elements _PPN_ARRAY.splice(0,10000); // Array aufbereiten und Normdatensaetze downloaden for (i=1; i <= _SETSIZE; i++) _PPN_ARRAY.push(__PPN_select(_SETNR, i, "PER")); // Alle PPN in ein Array _PPN_ARRAY = __PPN_prepare(_PPN_ARRAY); // Array deduplizieren _PPN_ARRAY = __PPN_dedup(_PPN_ARRAY); // Normdatensaetze downloaden __PPN_array_download(_PPN_ARRAY, _DOWNLOAD_DIR, "dwl" + _START_YEAR + _START_MONTH + "02-15", _DOWNLOAD_FORMAT, "PER", _WINDIR); // 2. Lokale Schlagwoerter 65xx // Array loeschen - max. 10.000 Elements _PPN_ARRAY.splice(0,10000); // Array aufbereiten und Normdatensaetze downloaden for (i=1; i <= _SETSIZE; i++) _PPN_ARRAY.push(__PPN_select(_SETNR, i, "LSW")); // Alle PPN in ein Array _PPN_ARRAY = __PPN_prepare(_PPN_ARRAY); // Array deduplizieren _PPN_ARRAY = __PPN_dedup(_PPN_ARRAY); // Normdatensaetze downloaden __PPN_array_download(_PPN_ARRAY, _DOWNLOAD_DIR, "dwl" + _START_YEAR + _START_MONTH + "02-15", _DOWNLOAD_FORMAT, "LSW", _WINDIR); } } // _SETSIZE > 0 // Alle Fenster schliessen if (!_CONNECT_PER_SESSION) { while (application.activeWindow.windowID > 0) application.activeWindow.closeWindow(); } } // else { __message("cannot connect host: " + _HOST); } } else { _DWLFILE.close(); // __message("Kein Download von: " + _FILENAME); } // +++ Download Startmonat 16. bis 1. Folgemonat +++ _FILENAME = "dwl" + _START_YEAR + _START_MONTH + "16-01" + _DOWNLOAD_FORMAT; // check, ob download file schon vorhanden if ((! _DWLFILE.open(_DOWNLOAD_DIR + _FILENAME + ".txt")) && (_START_MONTH < _MM || _DWL_16bis02)) { // __message("Download-File: " + _DOWNLOAD_DIR + _FILENAME + ".txt"); if ( __connect(_HOST, _PORT, _ACCOUNT, _PASSWD, !_CONNECT_PER_SESSION) || _CONNECT_PER_SESSION) { // evt. vorhandenes tmp-File loeschen application.shellExecute("file:///" + _WINDIR + "system32\\cmd.exe", 6, "open", "/c del " + _DOWNLOAD_DIR + _FILENAME + ".tmp"); // Download-File in user_pref.js eintragen application.writeProfileString("winibw.filelocation", "download", "file:///" + _DOWNLOAD_DIR + _FILENAME + ".tmp"); // __message("f sel " + "16-" + _START_MONTH + "-" + _START_YEAR + // " bis " + "01-" + __check_next_ym(_START_YEAR, _START_MONTH).substring(4,7) // + "-" + __check_next_ym(_START_YEAR, _START_MONTH).substring(0,4) + " " + _SELCODE); application.activeWindow.command("f sel " + "16-" + _START_MONTH + "-" + _START_YEAR + " bis " + "01-" + __check_next_ym(_START_YEAR, _START_MONTH).substring(4,7) + "-" + __check_next_ym(_START_YEAR, _START_MONTH).substring(0,4) + " " + _SELCODE, false); // SET-Daten ermitteln _SETNR = application.activeWindow.variable("P3GSE"); _SETSIZE = application.activeWindow.variable("P3GSZ"); // __message("_SETSIZE: " + _SETSIZE); if (_SETSIZE > 0 ){ // Download ausfuehren for (i=1; i <= _SETSIZE; i++) { application.activeWindow.command("dow " + i + " " + _DOWNLOAD_FORMAT, false); } // tmp-File erst nach abgeschlossenem Download umbenennen application.shellExecute("file:///" + _WINDIR + "system32\\cmd.exe", 6, "open", "/c rename " + _DOWNLOAD_DIR + _FILENAME + ".tmp " + _FILENAME + ".txt"); // Ueber PPN verlinkte Records auslesen if (_GET_PPN_RECORDS) { // 1. Personen 30xx // Array loeschen - max. 10.000 Elements _PPN_ARRAY.splice(0,10000); // Array aufbereiten und Normdatensaetze downloaden for (i=1; i <= _SETSIZE; i++) _PPN_ARRAY.push(__PPN_select(_SETNR, i, "PER")); // Alle PPN in ein Array _PPN_ARRAY = __PPN_prepare(_PPN_ARRAY); // Array deduplizieren _PPN_ARRAY = __PPN_dedup(_PPN_ARRAY); // Normdatensaetze downloaden __PPN_array_download(_PPN_ARRAY, _DOWNLOAD_DIR, "dwl" + _START_YEAR + _START_MONTH + "16-01", _DOWNLOAD_FORMAT, "PER", _WINDIR); // 2. Lokale Schlagwoerter 65xx // Array loeschen - max. 10.000 Elements _PPN_ARRAY.splice(0,10000); // Array aufbereiten und Normdatensaetze downloaden for (i=1; i <= _SETSIZE; i++) _PPN_ARRAY.push(__PPN_select(_SETNR, i, "LSW")); // Alle PPN in ein Array _PPN_ARRAY = __PPN_prepare(_PPN_ARRAY); // Array deduplizieren _PPN_ARRAY = __PPN_dedup(_PPN_ARRAY); // Normdatensaetze downloaden __PPN_array_download(_PPN_ARRAY, _DOWNLOAD_DIR, "dwl" + _START_YEAR + _START_MONTH + "16-01", _DOWNLOAD_FORMAT, "LSW", _WINDIR); } } // _SETSIZE > 0 // Alle Fenster schliessen if (!_CONNECT_PER_SESSION) { while (application.activeWindow.windowID > 0) application.activeWindow.closeWindow(); } } // else { __message("cannot connect host: " + _HOST); } } else { _DWLFILE.close(); // __message("Kein Download von: " + _FILENAME); } // weiter im Datum _YM = __check_next_ym(_START_YEAR, _START_MONTH); _START_YEAR = _YM.substring(0,4); _START_MONTH = _YM.substring(4,7); // __message("Next: " + _YM); } // end of while loop } // end of format_counter loop } // end of connect per session if (_CONNECT_PER_SESSION) while (application.activeWindow.windowID > 0) application.activeWindow.closeWindow(); // Press the key to close the WINIBW30 // F4 vorher dem Befehl "Beenden" zuweisen // Funktioniert leider nicht ! // application.activeWindow.simulateIBWKey("F4"); // Protokollieren per E-Mail // application.shellExecute("file:///C:/Soft/tools/wmailto.exe", 6, "open", "-sBIBLIOGRAPHIE:WINIBW-Download -cub-iuk@ub.uni-rostock.de -fUB1LB001"); // EXTERNES kill-kommando zur Sicherheit, falls WINIBW30 haengt ! // PSTOOLS: http://technet.microsoft.com/de-de/sysinternals/bb896649.aspx // application.shellExecute("file:///C:/Soft/Tools/pskill.exe", 6, "open", "WinIBW30"); }