Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate BibTeX from DOIs #10

Merged
merged 23 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4edeae8
Add py script and action to generate references from dois in readme f…
stefanbringuier May 31, 2023
1a80b99
fix github action
stefanbringuier May 31, 2023
93c117c
again, fix action
stefanbringuier May 31, 2023
ce1a8fc
github action to push .bib to repo
stefanbringuier May 31, 2023
8716c7d
fixed yaml formatting.
stefanbringuier May 31, 2023
8e53a93
fix python dep.
stefanbringuier May 31, 2023
ae8c68e
Commit from GitHub Actions (Generate References)
github-actions[bot] May 31, 2023
45fb4e2
Merge branch 'sgbaird:main' into main
stefanbringuier May 31, 2023
056861b
Commit from GitHub Actions (Generate References)
github-actions[bot] May 31, 2023
c25e6c0
Apply suggestions from code review
stefanbringuier May 31, 2023
0906cec
Update generate_bibtex.py
stefanbringuier May 31, 2023
89946aa
Merge branch 'sgbaird:main' into main
stefanbringuier May 31, 2023
53fdf50
remove tab in yml
stefanbringuier May 31, 2023
6cb8f75
fix weird emacs encoding
stefanbringuier May 31, 2023
5718c69
fixed using regex match result
stefanbringuier May 31, 2023
45313e3
Commit from GitHub Actions (Generate References)
github-actions[bot] May 31, 2023
fe99baa
bump python version
stefanbringuier May 31, 2023
431e2ea
Merge branch 'main' of github.com:stefanbringuier/awesome-self-drivin…
stefanbringuier May 31, 2023
4c712b4
Commit from GitHub Actions (Generate References)
github-actions[bot] May 31, 2023
71b7230
Update .github/workflows/references.yml
sgbaird Jun 1, 2023
870e224
Commit from GitHub Actions (Generate References)
github-actions[bot] Jun 1, 2023
a165eb2
Update .github/workflows/references.yml
sgbaird Jun 1, 2023
aeb23b1
Rm spurious line
sgbaird Jun 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/references.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Generate References
on:
push:
branches:
- main
jobs:
run-script:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
sgbaird marked this conversation as resolved.
Show resolved Hide resolved

- name: Set up Python
uses: actions/setup-python@v2
sgbaird marked this conversation as resolved.
Show resolved Hide resolved
with:
python-version: "3.8"
stefanbringuier marked this conversation as resolved.
Show resolved Hide resolved

- name: Python Deps.
run: |
python -m pip install --upgrade pip
pip install requests

- name: Create BibTeX from readme.md
run: |
cd bibtex
sgbaird marked this conversation as resolved.
Show resolved Hide resolved
python generate_bibtex.py
- name: Push to repo
uses: EndBug/add-and-commit@v9
with:
add: bibtex/references.bib
default_author: github_actions

100 changes: 100 additions & 0 deletions bibtex/generate_bibtex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import os
import urllib.request
import requests
import re

def download_and_write(filepath,outfile="readme.md"):
"""
If we want to download upstream
"""
# Create a file object to write the downloaded file to.
with open(outfile, 'wb') as f:
response = requests.get(fileurl, stream=True)
for chunk in response.iter_content(chunk_size=1024):
f.write(chunk)

def extract_dois(filepath,url=False):
if url:
outfile = download_and_write(filepath)
else:
outfile = filepath

# Regular expression pattern for DOIs
doi_pattern = r'\[.*?\]\((https?://doi\.org/[-._;()/:a-zA-Z0-9]+)\)'
stefanbringuier marked this conversation as resolved.
Show resolved Hide resolved
with open(outfile, 'r') as f:
md_file_content = f.read()
# Find all DOIs in the file
matches = re.findall(doi_pattern, md_file_content, re.IGNORECASE)
unique_dois = list(set(matches))

return unique_dois


def generate_unique_citation_key(citation_key, existing_keys):
"""
NOT TESTED/USED: Generate a unique citation key by appending letters in sequence if necessary.
"""
new_key = citation_key
suffix = ord('a')

while new_key in existing_keys:
new_key = f"{citation_key}{chr(suffix)}"
suffix += 1

return new_key


def update_citation_keys(bibtex_entries, bibtex_entry, citation_keys):
"""
NOT TESTED/USED: Modifies `bibtex_entries` to ensure uniqueness in the list of BibTeX entries.
"""
match = re.search(r'@(\w+){(\w+),', bibtex_entry)
if match:
entry_type, citation_key = match.groups()
# Generate a unique citation key if there are duplicates
if citation_key in citation_keys:
citation_key = generate_unique_citation_key(citation_key, citation_keys)
bibtex_entry = bibtex_entry.replace("@{}{{{}, ".format(entry_type, match.group(2)),
"@{}{{{}, ".format(entry_type, citation_key))

citation_keys.add(citation_key)

return bibtex_entries.append(bibtex_entry)

def download_and_save_bibs(dois,bibname="references.bib"):

#os.makedirs('bibtex', exist_ok=True)

# Header to ask the server to return a BibTeX entry
headers = {
'Accept': 'application/x-bibtex',
}

bibtex_entries = []
citation_keys = set()

for doi in dois:
print(doi)
# Send the GET request
req = urllib.request.Request(doi, headers=headers)
with urllib.request.urlopen(req) as response:
# Decode the BibTeX entry and append it to the list
bibtex_entry = response.read().decode()

# TODO: test/utlize
#update_citation_keys(bibtex_entries, bibtex_entry, citation_keys)

bibtex_entries.append(bibtex_entry)

bibtex_string = "\n".join(bibtex_entries)

with open(bibname, 'w') as f:
f.write(bibtex_string)


if __name__ == '__main__':
# Upstream URL.
#filepath = 'https://raw.githubusercontent.com/sgbaird/awesome-self-driving-labs/main/readme.md'
filepath = "../readme.md"
unique_dois = extract_dois(filepath)
download_and_save_bibs(unique_dois)
Loading