Skip to content

feat: add function that returns cif filenames based on chemical formula #336

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
fix: use api instead of bs4 for data retrieval
  • Loading branch information
yucongalicechen committed May 3, 2025
commit 12443328ff57b8dc3d67773f1cad32f0507f9e88
25 changes: 5 additions & 20 deletions src/diffpy/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import numpy as np
import requests
from bs4 import BeautifulSoup
from scipy.optimize import dual_annealing
from scipy.signal import convolve
from xraydb import material_mu
Expand Down Expand Up @@ -235,31 +234,17 @@ def fetch_cif_filenames(hill_formula):
------
ValueError
If no CIF files are found for the given formula.

Notes
-----
The data is retrieved from the Crystallography Open Database (COD).
If you use COD data in your research,
please acknowledge the COD project as described at
https://www.crystallography.net/cod/acknowledgements.html.
"""
search_url = (
f"https://www.crystallography.net/cod/"
f"result.php?formula={hill_formula}"
)
response = requests.get(search_url)
base_url = "https://www.crystallography.net/cod/result.php"
params = {"formula": hill_formula, "format": "json"}
response = requests.get(base_url, params=params)
if response.status_code != 200:
raise Exception(
f"Failed to retrieve search results. "
f"HTTP status code: {response.status_code}."
)
cif_links = BeautifulSoup(response.text, "html.parser").find_all("a")
cif_filenames = []
for link in cif_links:
href = link.get("href", "")
if href.endswith(".cif"):
filename = href.split("/")[-1]
cif_filenames.append(filename)
data = response.json()
cif_filenames = [str(entry["file"]) + ".cif" for entry in data]
if len(cif_filenames) == 0:
raise ValueError(
f"No CIF files found for the given formula: {hill_formula}. "
Expand Down