SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. This script takes as input a BibTeX library exported from readcube/mekentosj Papers3 and outputs a BibTex library for Zotero to import.
  6. The script preserves your Papers citekeys, adds supplementary files from the Papers3 Library, removes duplicate links to PDFs and removes extraneous *.html and *.webarchive files that are often created by importing articles into Paper from a web browser.
  7.  
  8. __Instructions for use__:
  9.  
  10. * Make sure to have Better BibTeX pre-installed to Zotero if you want to preserve the Papers citekeys.
  11.  
  12. * Export your Papers3 library as a *.bib file.
  13.     Export > BibTeX Library
  14.     Make sure to set the “BibTex Record” option to “Complete”. This will cause papers to include the paths to the main PDF (or whatever) file in the *.bib export
  15.  
  16. * In this script, update the ‘papers_library’ and ‘bibtex_library’ variables with the paths to your Papers3 library and the BibTeX library that you just exported.
  17.     e.g.
  18.     bibtex_library = Path(“~/Desktop/full_library_export.bib") ### Path to Papers BibTex library export
  19.     papers_library = Path(“~/Documents/user’s Library/Library.papers3") ### Path to Papers3 Library
  20.  
  21. * Run this script with python 3.5 or higher
  22.  
  23. * Import the 'zotero_import.bib’ file that gets generated with Zotero.
  24.  
  25. * Be sure to check the 'Import errors found:' file if Zotero generates one (if it exists, it will be in whatever folder you imported the library to; sort by title to find it).
  26.  
  27. __NOTE__:
  28. The Collections groupings are not preserved with this method. This is one way to manually get your Papers3 Collections into Zotero after following the above instructions:
  29.  
  30. * Export each collection as a BibTex library (“Export” set to “Selected Collection” and “BibTex Record” set to “Standard”). This will prevent any file paths from being included in the *.bib file.
  31.  
  32. * Import that *.bib file directly to Zotero with the option to “Place imported collections and items into new collection” selected.
  33.  
  34. * Then merge the duplicate records. That will give you a new collection with links to right papers from your Zotero library.
  35.  
  36. * In this strategy, you have to do that for each one of your Papers3 Collections. Not ideal but maybe tolerable.
  37. """
  38.  
  39. from pathlib import Path
  40. import re
  41.  
  42. ### Update these paths:
  43. bibtex_library = Path("~/Desktop/library.bib").expanduser() ### Path to Papers BibTeX library export
  44. papers_library = Path("~/Documents/daeda's Library/Library.papers3").expanduser() ### Path to Papers3 Library
  45.  
  46.  
  47. out = list()
  48. papers_library_string = str(papers_library) + '/'
  49.  
  50. if papers_library_string[-9:] != '.papers3/':
  51.     raise Exception(f'The variable \'papers_library\' should end in with \'.papers3\' but is rather: \n\t{str(papers_library)}')
  52. if not papers_library.exists():
  53.     raise Exception(f'The path you provided to the Papers3 library does not seem to exist: \n\t{str(papers_library)}')
  54. if not bibtex_library.exists() and bibtex_library.is_file() and bibtex_library.suffix == '.bib':
  55.     raise Exception(f'The path you provided to the BibTex Library file you exported from Papers3 does not seem to exist or is not \'.bib\' file: \n\t{str(bibtex_library)}')
  56.  
  57. with open(bibtex_library, 'r') as btlib:
  58.     for line in btlib:
  59.         if line.startswith('file = {'):
  60.             templine = re.sub(r'^file = {{(.*?)}},?', r'file = {\1},', line, flags = re.M)
  61.             newline = re.sub(r'^file = {(.*?);(\1)},?', r'file = {\1},', templine, flags = re.M)
  62.            
  63.             assert ';' not in newline ### assert there is only one file
  64.            
  65.             result = re.search(r"^file = {.*?:" + papers_library_string + r"(.*?)\.(.*?):(.*?/.*?)},?", newline)
  66.  
  67.             primary_file_path = Path(papers_library_string) / Path(result.group(1)).with_suffix('.'+result.group(2))
  68.            
  69.             supp_files = list()
  70.             for dir_extra in ['Supplemental', 'Media']:
  71.                 supp_dir = primary_file_path.parents[0] / dir_extra
  72.                 if supp_dir.exists():
  73.                     for x in supp_dir.iterdir():
  74.                         if x.is_file() and x.suffix not in ['.html', '.webarchive'] and str(x) != str(primary_file_path):
  75.                             supp_files.append(x)
  76.                    
  77.                     if len(supp_files) > 0:
  78.                         primary_line = re.search(r"(^file = {.*?:" + papers_library_string + r".*?\..*?:application/.*?)},?", newline)
  79.                         newline = primary_line.group(1)
  80.                         for x in supp_files:
  81.                             print(f'adding supplementary file for {x.name}')
  82.                             newline += f';{x.with_suffix("").name + " Supp" + x.suffix}:{x}:application/{x.suffix}'
  83.                         newline += '},\n'
  84.             out.append(newline)
  85.         else:
  86.             out.append(line)
  87.  
  88.  
  89. ### New BibTeX record to import into Zotero
  90. modified_lib = bibtex_library.parents[0] / 'zotero_import.bib'
  91. with open(modified_lib, 'w') as outfile:
  92.     for item in out:
  93.         outfile.write(item)
  94.  
  95. print(f'\n\nScript appears to have completed successfully. You can now import this file into Zotero (make sure Better BibTeX is already installed): \n\t{str(modified_lib)}')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top