public static void Main() { #region UserVariable string journalUrl = @"ftp://ftp.ncbi.nih.gov/pubmed/J_Entrez.txt"; // URL for journal list text file #endregion if (Program.ProjectShells.Count == 0) { return; //no project open } Project activeProject = Program.ActiveProjectShell.Project; List <Periodical> journalCollection = new List <Periodical>(); string completeList; try { // Get list of journals from website Cursor.Current = Cursors.WaitCursor; DebugMacro.WriteLine("Reading list of journals from " + journalUrl); WebClient client = new WebClient(); Stream stream = client.OpenRead(journalUrl); StreamReader streamReader = new StreamReader(stream); completeList = streamReader.ReadToEnd(); } catch (Exception e) { Cursor.Current = Cursors.Default; DebugMacro.WriteLine("Could not read file from " + journalUrl + ": " + e.Message); return; } // split into individual Journals try { Cursor.Current = Cursors.WaitCursor; string[] entrySplitters = { @"--------------------------------------------------------" }; List <string> individualJournalEntries = new List <string>(completeList.Split(entrySplitters, StringSplitOptions.RemoveEmptyEntries)); DebugMacro.WriteLine("{0} journal entries found.", individualJournalEntries.Count.ToString()); DebugMacro.WriteLine("Parsing entries ..."); int counter = 0; Regex splitEntry = new Regex(@"^(?:JrId: )(?<JournalId>\d+?)(?:\nJournalTitle: )(?<JournalTitle>.*?)(?:\nMedAbbr: )(?<Abbreviation2>.*?)(?:\nISSN \(Print\): )(?<IssnPrint>.*?)(?:\nISSN \(Online\): )(?<IssnOnline>.*?)(?:\nIsoAbbr: )(?<Abbreviation1>.*?)(?:\nNlmId: )(?<NlmId>.*?)$", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.Multiline); foreach (string journalEntry in individualJournalEntries) { counter++; string journalTitle; string abbreviation1; // this one should have full stops string abbreviation2; // this one shouldn't string abbreviation3; // this one should be any all uppercase acronym after a colon in JournalTitle string issnPrint; string issnOnline; string nlmId; // split into fields Match m = splitEntry.Match(journalEntry); //if (String.IsNullOrEmpty(m.Groups["JournalId"].Value)) continue; // nothing here journalTitle = m.Groups["JournalTitle"].Value; abbreviation1 = m.Groups["Abbreviation1"].Value; abbreviation2 = m.Groups["Abbreviation2"].Value; issnPrint = m.Groups["IssnPrint"].Value; // to be validated issnOnline = m.Groups["IssnOnline"].Value; nlmId = m.Groups["NlmId"].Value; // check format of abbreviation1 if (String.IsNullOrEmpty(abbreviation1)) { abbreviation1 = abbreviation2; } if (!abbreviation1.Contains(".") && !String.IsNullOrEmpty(abbreviation1)) { string[] journalTitleWords = journalTitle.ToLowerInvariant().Split(new char[] { ' ', '.', ';', ',', ':', '&', '-' }, StringSplitOptions.RemoveEmptyEntries); string[] abbreviation1Words = abbreviation1.Split(' '); List <string> abbreviation1WithFullStops = new List <string>(); foreach (string word in abbreviation1Words) { if (word.StartsWith("(") || word.EndsWith(")")) { abbreviation1WithFullStops.Add(word); } else if (!Array.Exists(journalTitleWords, x => x == word.ToLowerInvariant())) { abbreviation1WithFullStops.Add(word + "."); } else { abbreviation1WithFullStops.Add(word); } } abbreviation1 = String.Join(" ", abbreviation1WithFullStops); } // try to establish Abbreviation3 abbreviation3 = Regex.Match(journalTitle, @"(?:: )[A-Z]{2,6}$").ToString(); Periodical journal = new Periodical(activeProject, journalTitle); if (!String.IsNullOrEmpty(abbreviation1)) { journal.StandardAbbreviation = abbreviation1; } if (!String.IsNullOrEmpty(abbreviation2)) { journal.UserAbbreviation1 = abbreviation2; } if (!String.IsNullOrEmpty(abbreviation3)) { journal.UserAbbreviation2 = abbreviation3; } if (!string.IsNullOrEmpty(issnPrint) && IsValidISSN(issnPrint)) { journal.Issn = issnPrint; } else if (!string.IsNullOrEmpty(issnOnline) && IsValidISSN(issnOnline)) { journal.Issn = issnOnline; } if (!string.IsNullOrEmpty(issnPrint) && IsValidISSN(issnPrint) && !string.IsNullOrEmpty(issnOnline) && IsValidISSN(issnOnline)) { journal.Notes = "ISSN (Online): " + issnOnline; } if (!String.IsNullOrEmpty(nlmId)) { journal.Notes = journal.Notes + "\nNlmID: " + nlmId; } journalCollection.Add(journal); } activeProject.Periodicals.AddRange(journalCollection); } catch (Exception e) { Cursor.Current = Cursors.Default; DebugMacro.WriteLine("An error occurred importing the journal data: " + e.Message); return; } finally { Cursor.Current = Cursors.Default; var saveOptions = new DesktopDatabaseSaveOptions(); saveOptions.ForceMultiUserUpdate = true; activeProject.Save(saveOptions); Cursor.Current = Cursors.Default; if (journalCollection != null) { MessageBox.Show(journalCollection.Count.ToString() + " journal(s) were imported (not counting duplicates)", "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); journalCollection = null; } } } //end Main()
public static void Main() { #region UserVariable string journalUrl = @"http://journal-abbreviations.library.ubc.ca/dump.php"; // URL for journal list text file #endregion if (Program.ProjectShells.Count == 0) { return; //no project open } Project activeProject = Program.ActiveProjectShell.Project; List <Periodical> journalCollection = new List <Periodical>(); string completeList; try { // Get list of journals from website Cursor.Current = Cursors.WaitCursor; DebugMacro.WriteLine("Reading list of journals from " + journalUrl); WebClient client = new WebClient(); Stream stream = client.OpenRead(journalUrl); StreamReader streamReader = new StreamReader(stream); completeList = streamReader.ReadToEnd(); } catch (Exception e) { Cursor.Current = Cursors.Default; DebugMacro.WriteLine("Could not read file from " + journalUrl + ": " + e.Message); return; } // split into individual Journals int counter = 0; int refCounter = 0; try { Cursor.Current = Cursors.WaitCursor; // prepare data completeList = Regex.Match(completeList, @"(<tbody>.*?<\\/tbody)").ToString(); completeList = Regex.Replace(completeList, @"\\/", @"/"); MatchCollection journals = Regex.Matches(completeList, "(?<=<tr>).*?(?=</tr>)"); DebugMacro.WriteLine("{0} journal entries found.", journals.Count.ToString()); DebugMacro.WriteLine("Parsing entries ..."); foreach (Match journalAndAbbrev in journals) { string journalTitle = String.Empty; string abbreviation1 = String.Empty; // this one should have full stops string abbreviation2 = String.Empty; // this one shouldn't string abbreviation3; // this one should be any all uppercase acronym after a colon in JournalTitl MatchCollection journalData = Regex.Matches(journalAndAbbrev.ToString(), "(?<=<td>).*?(?=</td>)"); if (journalData.Count < 2) { continue; } abbreviation1 = journalData[0].Value; journalTitle = Regex.Replace(journalData[1].Value, @"\bfur\b", "für"); // generate abbreviation2 by removing full stops from abbreviation1 string[] abbreviation1Words = abbreviation1.Split(' '); for (int i = 0; i < abbreviation1Words.Length; i++) { abbreviation1Words[i] = abbreviation1Words[i].TrimEnd('.'); } abbreviation2 = String.Join(" ", abbreviation1Words); // try to establish Abbreviation3 abbreviation3 = Regex.Match(journalTitle, @"(?:: )[A-Z]{2,6}$").ToString(); Periodical journal = new Periodical(activeProject, journalTitle); if (!String.IsNullOrEmpty(abbreviation1)) { journal.StandardAbbreviation = abbreviation1; } if (!String.IsNullOrEmpty(abbreviation2)) { journal.UserAbbreviation1 = abbreviation2; } if (!String.IsNullOrEmpty(abbreviation3)) { journal.UserAbbreviation2 = abbreviation3; } journalCollection.Add(journal); } DialogResult updateReferences = MessageBox.Show( @"Would you like to update journal information for references in current selection (all references if no selection is active)?", "Citavi Macro", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); // update references that have identical titles if (updateReferences == DialogResult.Yes) { List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); foreach (Reference reference in references) { if (reference.Periodical == null) { continue; } if (journalCollection.Any(item => item.Name == reference.Periodical.Name) && !String.IsNullOrEmpty(reference.Periodical.Name)) { reference.Periodical = journalCollection.Where(item => item.Name == reference.Periodical.Name).FirstOrDefault(); refCounter++; } } activeProject.Periodicals.AddRange(journalCollection); } else { activeProject.Periodicals.AddRange(journalCollection); } } catch (Exception e) { Cursor.Current = Cursors.Default; DebugMacro.WriteLine("An error occurred importing the journal data: " + e.ToString()); return; } finally { Cursor.Current = Cursors.Default; var saveOptions = new DesktopDatabaseSaveOptions(); saveOptions.ForceMultiUserUpdate = true; activeProject.Save(saveOptions); Cursor.Current = Cursors.Default; if (journalCollection != null) { MessageBox.Show(journalCollection.Count.ToString() + " journal(s) were imported (not counting duplicates)\n " + refCounter.ToString() + " reference(s) were changed.", "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); journalCollection = null; } } } //end Main()
public static void Main() { if (Program.ProjectShells.Count == 0) { return; //no project open } Project activeProject = Program.ActiveProjectShell.Project; string fileName = null; List <Periodical> journalCollection = new List <Periodical>(); try { // Dialog zum Auswählen des gewünschten Ordners einblenden OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text Dateien (*.txt)|*.txt|Alle Dateien (*.*)|*.*"; openFileDialog.Title = "Wählen Sie die zu importierende Zeitschriften-Datei aus (Text-Format)"; if (openFileDialog.ShowDialog() == DialogResult.OK) { fileName = openFileDialog.FileName; } else { return; } //Hourglass or other wait cursor Cursor.Current = Cursors.WaitCursor; Encoding enc = GetFileEncoding(fileName); StreamReader streamReader = new StreamReader(fileName, enc); string journalList = streamReader.ReadToEnd(); streamReader.Close(); //We check for certain non-printable chars to ensure we are dealing with a "text file" Regex testRegex = new Regex("[\x00-\x1f-[\t\n\r]]", RegexOptions.CultureInvariant | RegexOptions.Compiled); if (testRegex.IsMatch(journalList)) { //this is most likely not a textfile Cursor.Current = Cursors.Default; MessageBox.Show("Diese Datei enthält ungültige Zeichen und kann daher nicht importiert werden.", "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } Regex matchRegex = new Regex( @"^(?<FullName>[^#=;|\t\n]+?)(?: *[#=;|\t] *(?<Abbreviation1>[^#=;|\t\n]*))?(?: *[#=;|\t] *(?<Abbreviation2>[^#=;|\t\n]*))?(?: *[#=;|\t] *(?<Abbreviation3>[^#=;|\t\n]*))?(?: *[#=;|\t] *(?<ISSN>[^#=;|\t\n]*))??$", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.Multiline //IMPORTANT! ); //// Capture all matches in the journalList MatchCollection ms = matchRegex.Matches(journalList); //MessageBox.Show("Insgesamt sind " + ms.Count.ToString() + " Treffer gefunden worden."); foreach (Match m in ms) { if (string.IsNullOrEmpty(m.Groups["FullName"].Value)) { continue; } Periodical journal = new Periodical(activeProject, m.Groups["FullName"].Value); if (!string.IsNullOrEmpty(m.Groups["Abbreviation1"].Value)) { journal.StandardAbbreviation = m.Groups["Abbreviation1"].Value; } if (!string.IsNullOrEmpty(m.Groups["Abbreviation2"].Value)) { journal.UserAbbreviation1 = m.Groups["Abbreviation2"].Value; } if (!string.IsNullOrEmpty(m.Groups["Abbreviation3"].Value)) { journal.UserAbbreviation2 = m.Groups["Abbreviation3"].Value; } string sISSN = ""; if (!string.IsNullOrEmpty(m.Groups["ISSN"].Value)) { sISSN = m.Groups["ISSN"].Value; if (IsValidISSN(sISSN)) { journal.Issn = sISSN; } } journalCollection.Add(journal); } activeProject.Periodicals.AddRange(journalCollection); //activeProject.Journals.AddRangeBound(journalCollection); } catch (Exception exception) { Cursor.Current = Cursors.Default; MessageBox.Show(exception.ToString(), "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { var saveOptions = new DesktopDatabaseSaveOptions(); saveOptions.ForceMultiUserUpdate = true; activeProject.Save(saveOptions); Cursor.Current = Cursors.Default; if (journalCollection != null) { MessageBox.Show("Es wurde(n) " + journalCollection.Count.ToString() + " Zeitschrift(en) eingelesen. \n(Dubletten wurden unterdrückt.)", "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); journalCollection = null; } } } //end Main()