static Task FindDois(List <LocationContainer> containers, IProgress <PercentageAndTextProgressInfo> progress, CancellationToken cancellationToken) { var supporter = new ReferenceIdentifierSupport(); var counter = 0; return(Task.Run(() => { foreach (var container in containers) { cancellationToken.ThrowIfCancellationRequested(); counter++; var percentage = counter * 100 / containers.Count; if (!string.IsNullOrEmpty(container.Location.Reference.Doi)) { // Another location of the same reference has already added the DOI progress.ReportSafe(container.Location.ToString(), percentage); continue; } var matches = supporter.FindIdentifierInFile(container.Path, 5, ReferenceIdentifierType.Doi, false); if (matches.Count == 0) { progress.ReportSafe(container.Location.ToString(), percentage); continue; } var match = matches.ElementAt(0); container.Location.Reference.Doi = match.ToString(); progress.ReportSafe(container.Location.ToString(), percentage); } })); }
public static void Main() { //**************************************************************************************************************** // UPDATE BIBLIOGRAPHIC DATA FROM PMID OR DOI-SEARCH // Version 2.1 -- 2015-01-12 // // This macro iterates through the references in a selection ("filter"). // If they have a DOI or PMID, it downloads bibliographical data and owverwrites the reference's data. // PMID is given priority over DOI, i.e. if both are present, data will be loaded from PubMed. // // // EDIT HERE // Variables to be changed by user bool overwriteAbstract = false; // if true, existing Abstract will be replaced bool overwriteTableOfContents = false; // if true, existing TOC will be replaced bool overwriteKeywords = false; // if true, existing Keywords will be replaced bool clearNotes = true; // if true, Notes field will be emptied // DO NOT EDIT BELOW THIS LINE // **************************************************************************************************************** if (!Program.ProjectShells.Any()) { return; //no project open } if (IsBackupAvailable() == false) { return; //user wants to backup his/her project first } int counter = 0; try { List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); var referencesWithDoi = references.Where(reference => !string.IsNullOrEmpty(reference.PubMedId) | !string.IsNullOrEmpty(reference.Doi)).ToList(); SwissAcademic.Citavi.Project activeProject = Program.ActiveProjectShell.Project; var identifierSupport = new ReferenceIdentifierSupport(); foreach (Reference reference in referencesWithDoi) { // Look up PMID. If that fails, look up DOI. If no data is found, move on. var lookedUpReference = identifierSupport.FindReference(activeProject, new ReferenceIdentifier() { Type = ReferenceIdentifierType.PubMedId, Value = reference.PubMedId }, true, null); if (lookedUpReference == null) { lookedUpReference = identifierSupport.FindReference(activeProject, new ReferenceIdentifier() { Type = ReferenceIdentifierType.Doi, Value = reference.Doi }, true, null); } if (lookedUpReference == null) { continue; } //merge reference & lookedUpReference, overwriting bibliographic data of the former List <ReferencePropertyId> omitData = new List <ReferencePropertyId>(); omitData.Add(ReferencePropertyId.CoverPath); omitData.Add(ReferencePropertyId.Locations); if (!overwriteAbstract) { omitData.Add(ReferencePropertyId.Abstract); } if (!overwriteTableOfContents) { omitData.Add(ReferencePropertyId.TableOfContents); } if (!overwriteKeywords) { omitData.Add(ReferencePropertyId.Keywords); } reference.MergeReference(lookedUpReference, true, omitData); counter++; if (!string.IsNullOrEmpty(reference.Notes) && clearNotes) { reference.Notes = string.Empty; //empty notes field } if (activeProject.Engine.Settings.BibTeXCitationKey.IsTeXEnabled) { reference.BibTeXKey = activeProject.BibTeXKeyAssistant.GenerateKey(reference); } if (activeProject.Engine.Settings.BibTeXCitationKey.IsCitationKeyEnabled) { reference.CitationKey = activeProject.CitationKeyAssistant.GenerateKey(reference); } } } //end try finally { MessageBox.Show(string.Format("Macro has finished execution.\r\n{0} references were updated.", counter.ToString()), "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); } //end finally } //end main()
public static void Main() { //This macro iterates through all linked PDF files and checks for a DOI inside the file. //If the reference that the file is linked to does not have a DOI already, it is added to the record. if (!Program.ProjectShells.Any()) { return; //no project open } if (IsBackupAvailable() == false) { return; //user wants to backup his/her project first } int counter = 0; try { SwissAcademic.Citavi.Project activeProject = Program.ActiveProjectShell.Project; string citaviFilesPath = activeProject.GetFolderPath(CitaviFolder.CitaviFiles); var fileLocations = activeProject.AllLocations.Where(location => location.LocationType == LocationType.ElectronicAddress && ( location.AddressUri.AddressInfo == ElectronicAddressInfo.CitaviFiles || location.AddressUri.AddressInfo == ElectronicAddressInfo.AbsoluteFileUri || location.AddressUri.AddressInfo == ElectronicAddressInfo.RelativeFileUri ) ).ToList(); var supporter = new ReferenceIdentifierSupport(); foreach (Location location in fileLocations) { if (location.Reference == null) { continue; } if (!string.IsNullOrEmpty(location.Reference.Doi)) { continue; //if DOI is already available, we do not scan the PDF } string path = string.Empty; switch (location.AddressUri.AddressInfo) { case ElectronicAddressInfo.CitaviFiles: path = Path.Combine(citaviFilesPath, location.Address); //DebugMacro.WriteLine("CitaviFiles: " + path); break; case ElectronicAddressInfo.RelativeFileUri: path = location.AddressUri.AbsoluteUri.LocalPath; //DebugMacro.WriteLine("RelativeFileUri: " + path); break; case ElectronicAddressInfo.AbsoluteFileUri: path = location.Address; //DebugMacro.WriteLine("AbsoluteFileUri: " + path); break; default: continue; } if (string.IsNullOrEmpty(path)) { continue; } if (!File.Exists(path)) { continue; } if (!Path.GetExtension(path).Equals(".pdf", StringComparison.OrdinalIgnoreCase)) { continue; } var matches = supporter.FindIdentifierInFile(path, ReferenceIdentifierType.Doi, false); if (matches.Count == 0) { continue; } var match = matches.ElementAt(0); if (string.IsNullOrEmpty(location.Reference.Doi)) { //DebugMacro.WriteLine(match.ToString()); location.Reference.Doi = match.ToString(); counter++; } } } //end try finally { MessageBox.Show(string.Format("Macro has finished execution.\r\n{0} changes were made.", counter.ToString()), "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); } //end finally } //end main()
static async Task <IEnumerable <Reference> > RunAsync(Tuple <Project, List <Reference>, MacroSettings> tuple, IProgress <PercentageAndTextProgressInfo> progress, CancellationToken cancellationToken) { var identifierSupport = new ReferenceIdentifierSupport(); var references = tuple.Item2; var project = tuple.Item1; var settings = tuple.Item3; var mergedReferences = new List <Reference>(); foreach (var reference in references) { var lookedUpReference = await identifierSupport.FindReferenceAsync(project, new ReferenceIdentifier() { Type = ReferenceIdentifierType.PubMedId, Value = reference.PubMedId }, CancellationToken.None); if (lookedUpReference == null) { progress.ReportSafe(100 / references.Count * references.IndexOf(reference)); continue; } var omitData = new List <ReferencePropertyId> { ReferencePropertyId.CoverPath, ReferencePropertyId.Locations }; if (reference.Periodical != null) { omitData.Add(ReferencePropertyId.Periodical); } if (!settings.OverwriteAbstract) { omitData.Add(ReferencePropertyId.Abstract); } if (!settings.OverwriteTableOfContents) { omitData.Add(ReferencePropertyId.TableOfContents); } if (!settings.OverwriteKeywords) { omitData.Add(ReferencePropertyId.Keywords); } reference.MergeReference(lookedUpReference, true, omitData); if (settings.ClearNotes) { reference.Notes = string.Empty; } if (project.Engine.Settings.BibTeXCitationKey.IsTeXEnabled) { reference.BibTeXKey = project.BibTeXKeyAssistant.GenerateKey(reference); } if (project.Engine.Settings.BibTeXCitationKey.IsCitationKeyEnabled) { reference.CitationKey = project.CitationKeyAssistant.GenerateKey(reference); } mergedReferences.Add(reference); progress.ReportSafe(100 / references.Count * references.IndexOf(reference)); } return(mergedReferences); }
static async Task <IEnumerable <Reference> > RunAsync(Tuple <Project, List <Reference>, List <Reference>, MacroSettings> tuple, IProgress <PercentageAndTextProgressInfo> progress, CancellationToken cancellationToken) { var project = tuple.Item1; var referencesWithPmid = tuple.Item2; var referencesWithDoi = tuple.Item3; var settings = tuple.Item4; var mergedReferences = new List <Reference>(); var count = referencesWithDoi.Count + referencesWithPmid.Count; var identifierSupport = new ReferenceIdentifierSupport(project); // PMID for (int i = 0; i < referencesWithPmid.Count; i++) { var reference = referencesWithPmid[i]; cancellationToken.ThrowIfCancellationRequested(); var lookedUpReference = await identifierSupport.FindReferenceAsync(project, new ReferenceIdentifier() { Type = ReferenceIdentifierType.PubMedId, Value = reference.PubMedId }, cancellationToken); if (lookedUpReference == null) { progress.ReportSafe(Convert.ToInt32(100.00 / count * i)); continue; } var omitData = new List <ReferencePropertyId> { ReferencePropertyId.CoverPath, ReferencePropertyId.Locations }; if (reference.Periodical != null) { omitData.Add(ReferencePropertyId.Periodical); } if (!settings.OverwriteAbstract) { omitData.Add(ReferencePropertyId.Abstract); } if (!settings.OverwriteTableOfContents) { omitData.Add(ReferencePropertyId.TableOfContents); } if (!settings.OverwriteKeywords) { omitData.Add(ReferencePropertyId.Keywords); } reference.MergeReference(lookedUpReference, true, omitData); if (settings.ClearNotes) { reference.Notes = string.Empty; } if (project.Engine.Settings.BibTeXCitationKey.IsTeXEnabled) { reference.BibTeXKey = project.BibTeXKeyAssistant.GenerateKey(reference); } if (project.Engine.Settings.BibTeXCitationKey.IsCitationKeyEnabled) { reference.CitationKey = project.CitationKeyAssistant.GenerateKey(reference); } mergedReferences.Add(reference); progress.ReportSafe(Convert.ToInt32(100.00 / count * i)); } // DOI for (int i = 0; i < referencesWithDoi.Count; i++) { var reference = referencesWithDoi[i]; cancellationToken.ThrowIfCancellationRequested(); var lookedUpReference = await identifierSupport.FindReferenceAsync(project, new ReferenceIdentifier() { Type = ReferenceIdentifierType.Doi, Value = reference.Doi }, cancellationToken); if (lookedUpReference == null) { progress.ReportSafe(Convert.ToInt32(100.00 / count * (i + referencesWithPmid.Count))); continue; } var omitData = new List <ReferencePropertyId> { ReferencePropertyId.CoverPath, ReferencePropertyId.Locations }; if (reference.Periodical != null) { omitData.Add(ReferencePropertyId.Periodical); } if (!settings.OverwriteAbstract) { omitData.Add(ReferencePropertyId.Abstract); } if (!settings.OverwriteTableOfContents) { omitData.Add(ReferencePropertyId.TableOfContents); } if (!settings.OverwriteKeywords) { omitData.Add(ReferencePropertyId.Keywords); } reference.MergeReference(lookedUpReference, true, omitData); if (settings.ClearNotes) { reference.Notes = string.Empty; } if (project.Engine.Settings.BibTeXCitationKey.IsTeXEnabled) { reference.BibTeXKey = project.BibTeXKeyAssistant.GenerateKey(reference); } if (project.Engine.Settings.BibTeXCitationKey.IsCitationKeyEnabled) { reference.CitationKey = project.CitationKeyAssistant.GenerateKey(reference); } mergedReferences.Add(reference); progress.ReportSafe(Convert.ToInt32(100.00 / count * (i + referencesWithPmid.Count))); } return(mergedReferences); }
public static async void Main() { //**************************************************************************************************************** // UPDATE BIBLIOGRAPHIC DATA FROM PMID OR DOI-SEARCH // Version 2.4 -- 2019-12-06 // -- updated to work with Citavi 6 (only version 6.3.15 or newer!) // // This macro iterates through the references in a selection ("filter"). // If they have a PMID or DOI or ISBN, it downloads bibliographical data and owverwrites the reference's data. // // PMID is given priority over DOI , i.e. if both are present, data will be loaded from PubMed, then // Crossref. then the selected catalogues. // // // EDIT HERE // Variables to be changed by user bool overwriteAbstract = false; // if true, existing Abstract will be replaced bool overwriteTableOfContents = false; // if true, existing TOC will be replaced bool overwriteKeywords = false; // if true, existing Keywords will be replaced bool clearNotes = true; // if true, Notes field will be emptied bool useIsbn = true; // if true, the ISBN field will also be used to query data int wait = 4; // timeout in seconds // DO NOT EDIT BELOW THIS LINE // **************************************************************************************************************** if (!Program.ProjectShells.Any()) { return; //no project open } if (IsBackupAvailable() == false) { return; //user wants to backup his/her project first } int counter = 0; try { List <Reference> references = Program.ActiveProjectShell.PrimaryMainForm.GetFilteredReferences(); SwissAcademic.Citavi.Project activeProject = Program.ActiveProjectShell.Project; foreach (Reference reference in references) { if (String.IsNullOrEmpty(reference.PubMedId) && String.IsNullOrEmpty(reference.Doi) && String.IsNullOrEmpty(reference.Isbn.ToString())) { continue; } DebugMacro.WriteLine("Checking reference " + reference.ShortTitle); var cts = new CancellationTokenSource(TimeSpan.FromSeconds(wait)); Reference lookedUpReference = null; if (!String.IsNullOrEmpty(reference.PubMedId)) { try { ReferenceIdentifier refIdentPmid = new ReferenceIdentifier() { Value = reference.PubMedId, Type = ReferenceIdentifierType.PubMedId }; var identifierSupport = new ReferenceIdentifierSupport(); lookedUpReference = await identifierSupport.FindReferenceAsync(activeProject, refIdentPmid, cts.Token); } catch (System.OperationCanceledException e) { DebugMacro.WriteLine("Timeout."); continue; } catch (Exception e) { DebugMacro.WriteLine("An error occured: " + e.ToString()); continue; } } else if (!String.IsNullOrEmpty(reference.Doi)) { try { ReferenceIdentifier refIdentDoi = new ReferenceIdentifier() { Value = reference.Doi, Type = ReferenceIdentifierType.Doi }; var identifierSupport = new ReferenceIdentifierSupport(); lookedUpReference = await identifierSupport.FindReferenceAsync(activeProject, refIdentDoi, cts.Token); } catch (System.OperationCanceledException e) { DebugMacro.WriteLine("Timeout."); continue; } catch (Exception e) { DebugMacro.WriteLine("An error occured: " + e.ToString()); continue; } } else if (!String.IsNullOrEmpty(reference.Isbn.ToString()) && useIsbn) { try { ReferenceIdentifier refIdentIsbn = new ReferenceIdentifier() { Value = reference.Isbn, Type = ReferenceIdentifierType.Isbn }; var identifierSupport = new ReferenceIdentifierSupport(); lookedUpReference = await identifierSupport.FindReferenceAsync(activeProject, refIdentIsbn, cts.Token); } catch (System.OperationCanceledException e) { DebugMacro.WriteLine("Timeout."); continue; } catch (Exception e) { DebugMacro.WriteLine("An error occured: " + e.ToString()); continue; } } // if nothing is found, move on if (lookedUpReference == null) { continue; } //merge reference & lookedUpReference, overwriting bibliographic data of the former List <ReferencePropertyId> omitData = new List <ReferencePropertyId>(); omitData.Add(ReferencePropertyId.CoverPath); omitData.Add(ReferencePropertyId.Locations); if (!overwriteAbstract) { omitData.Add(ReferencePropertyId.Abstract); } if (!overwriteTableOfContents) { omitData.Add(ReferencePropertyId.TableOfContents); } if (!overwriteKeywords) { omitData.Add(ReferencePropertyId.Keywords); } reference.MergeReference(lookedUpReference, true, omitData.ToArray()); counter++; if (!string.IsNullOrEmpty(reference.Notes) && clearNotes) { reference.Notes = string.Empty; //empty notes field } if (activeProject.Engine.Settings.BibTeXCitationKey.IsTeXEnabled) { reference.BibTeXKey = activeProject.BibTeXKeyAssistant.GenerateKey(reference); } if (activeProject.Engine.Settings.BibTeXCitationKey.IsCitationKeyEnabled) { reference.CitationKey = activeProject.CitationKeyAssistant.GenerateKey(reference); } } } //end try finally { MessageBox.Show(string.Format("Macro has finished execution.\r\n{0} references were updated.", counter.ToString()), "Citavi", MessageBoxButtons.OK, MessageBoxIcon.Information); } //end finally } //end main()