157 lines
6.9 KiB
Python
157 lines
6.9 KiB
Python
import os
|
|
import epo_ops
|
|
import xml.etree.ElementTree as ET
|
|
from requests import HTTPError
|
|
from datetime import datetime, timedelta
|
|
|
|
# Middleware
|
|
middlewares = [
|
|
epo_ops.middlewares.Dogpile(),
|
|
epo_ops.middlewares.Throttler(),
|
|
]
|
|
|
|
# Create new EPO Client with given credentials
|
|
client = epo_ops.Client(
|
|
key = "XXX",
|
|
secret = "XXX",
|
|
middlewares=middlewares,
|
|
)
|
|
|
|
# Static Maps for hirachical scraping
|
|
country_codes = ["US", "AL","AP","AR","AT","AU","BA","BE","BG","BR","CA","CH","CL","CN","CO","CR","CS","CU","CY","CZ","DD","DE","DK","DZ","EA","EC","EE","EG","EP","ES","FI","FR","GB","GC","GE","GR","GT","HK","HR","HU","ID","IE","IL","IN","IS","IT","JP","KE","KR","LI","LT","LU","LV","MA","MC","MD","ME","MK","MN","MT","MW","MX","MY","NC","NI","NL","NO","NZ","OA","PA","PE","PH","PL","PT","RO","RS","RU","SE","SG","SI","SK","SM","SU","SV","TJ","TR","TT","TW","UA","US","UY","VN","WO","YU","ZA","ZM","ZW"]
|
|
cpc_codes = {
|
|
"A":["A01", "A21", "A22", "A23", "A24", "A41", "A42", "A43", "A44", "A45", "A46", "A47", "A61", "A62", "A63", "A99"],
|
|
"B":["B01", "B02", "B03", "B04", "B05", "B06", "B07", "B08", "B09", "B21", "B22","B23", "B24", "B25", "B26", "B27", "B28", "B29", "B30", "B31", "B32", "B33", "B41", "B42", "B43", "B44", "B60", "B61", "B62", "B63", "B64","B65", "B66", "B67", "B68", "B81", "B82", "B99"],
|
|
"C":["C01", "C02", "C03", "C04", "C05", "C06", "C07", "C08", "C09", "C10", "C11", "C12", "C13", "C14", "C21", "C22", "C23", "C24", "C25", "C30", "C40", "C99"],
|
|
"D":["D01", "D02", "D03", "D04", "D05", "D06", "D07", "D10", "D21", "D99"],
|
|
"E":["E01", "E02", "E03", "E04", "E05", "E06", "E21", "E99"],
|
|
"F":["F01", "F02", "F03", "F04", "F05", "F15", "F16", "F17", "F21", "F22", "F23", "F24", "F25","F26", "F27", "F28", "F41", "F42", "F99"],
|
|
"G":["G01", "G02", "G03", "G04", "G05", "G06", "G07", "G08", "G09", "G10", "G11", "G12", "G16", "G21", "G99"],
|
|
"H":["H01", "H02", "H03", "H04", "H05", "H99"],
|
|
"Y":["Y01", "Y02", "Y04", "Y10"]
|
|
}
|
|
# Exception indicating too large queries
|
|
class TooManyPatentsError(Exception):
|
|
def __init__(self, message, errors):
|
|
super().__init__(message)
|
|
|
|
self.errors = errors
|
|
|
|
# Get Tag, Kind und Contry Codes from Response
|
|
def parse_response(response):
|
|
documents = []
|
|
search_count = 0
|
|
root = ET.fromstring(response)
|
|
for child in root.iter('*'):
|
|
# Find Searchcount
|
|
if(child.tag == "{http://ops.epo.org}biblio-search"):
|
|
search_count = int(child.attrib['total-result-count'])
|
|
# Find publications
|
|
if(child.tag == "{http://ops.epo.org}publication-reference"):
|
|
doc_nummer, doc_kind, doc_country = ["", "", ""]
|
|
# Parse XML File
|
|
for item in child.iter("*"):
|
|
if(item.tag == "{http://www.epo.org/exchange}doc-number"):
|
|
doc_nummer = item.text
|
|
if(item.tag == "{http://www.epo.org/exchange}kind"):
|
|
doc_kind = item.text
|
|
if(item.tag == "{http://www.epo.org/exchange}country"):
|
|
doc_country = item.text
|
|
# Append all Documents to Document List
|
|
documents.append((doc_country, doc_kind, doc_nummer))
|
|
# Return informaiton from Response (search count and documents)
|
|
return (search_count, documents)
|
|
|
|
# Collect Publication IDs
|
|
def get_publication_ids(pr, cpc, date):
|
|
# Default Range 1:100 (limit of 100 publications per request)
|
|
range = [1, 100]
|
|
# Decide how to filter
|
|
if(cpc != None):
|
|
seary_query = 'pd within "'+date+' '+date+'" and pr='+pr+' and cpc='+cpc
|
|
else:
|
|
seary_query = 'pd within "'+date+' '+date+'" and pr='+pr
|
|
|
|
# Do first request
|
|
try:
|
|
response = client.published_data_search(seary_query, range_begin=range[0], range_end=range[1], constituents=None)
|
|
except HTTPError as error:
|
|
print("Not Found", error)
|
|
return []
|
|
# Parse first query
|
|
search_count, documents = parse_response(response.text)
|
|
|
|
# Decide how many
|
|
if(search_count > 2000):
|
|
raise TooManyPatentsError("Change Query!")
|
|
while(len(documents) < search_count):
|
|
# Do more requests until all all publications are found for query
|
|
range = [len(documents) + 1, (len(documents) + 100)]
|
|
if(range[1] > search_count):
|
|
range[1] = search_count
|
|
response = client.published_data_search(seary_query, range_begin=range[0], range_end=range[1], constituents=None)
|
|
search_count, new_documents = parse_response(response.text)
|
|
# Add all new DocumentIds
|
|
documents.extend(new_documents)
|
|
return documents
|
|
|
|
# Get publication IDs for Date
|
|
def get_publications(date):
|
|
full_publications = []
|
|
# Get patets for ever contry
|
|
for country in country_codes:
|
|
try:
|
|
publications = get_publication_ids(country, None, date)
|
|
full_publications.extend(publications)
|
|
except:
|
|
# Check if more
|
|
for cpc_code, sub_codes in cpc_codes.items():
|
|
try:
|
|
publications = get_publication_ids(country, cpc_code, date)
|
|
full_publications.extend(publications)
|
|
except:
|
|
for sub_code in sub_codes:
|
|
publications = get_publication_ids(country, sub_code, date)
|
|
full_publications.extend(publications)
|
|
|
|
return full_publications
|
|
|
|
# Get full Publications
|
|
def get_full_publications(publication, source_folder):
|
|
# Load Patent Details
|
|
doc_country, doc_kind, doc_nummer = publication
|
|
response = client.published_data( # Retrieve bibliography data
|
|
reference_type = 'publication', # publication, application, priority
|
|
input = epo_ops.models.Docdb(doc_nummer, doc_country, doc_kind), # original, docdb, epodoc
|
|
endpoint = 'biblio', # optional, defaults to biblio in case of published_data
|
|
constituents = [] # optional, list of constituents
|
|
)
|
|
filename = doc_country + "_" + doc_kind + "_" + doc_nummer + ".xml"
|
|
file_path = source_folder + "/" + filename
|
|
if os.path.isfile(file_path) == False:
|
|
f = open(file_path, "a")
|
|
f.write(response.text)
|
|
f.close()
|
|
|
|
# Get Publications from yesterday
|
|
def get_publications_from_yesterday():
|
|
yesterday = datetime.now() - timedelta(1)
|
|
# Get Patent list
|
|
publications = get_publications(datetime.strftime(yesterday, '%Y%m%d'))
|
|
source_folder = "./publications"
|
|
# Create Publications folder
|
|
if not os.path.exists(source_folder):
|
|
os.makedirs(source_folder)
|
|
|
|
source_folder = source_folder + "/" + datetime.strftime(yesterday, '%Y-%m-%d')
|
|
# Create folder for yesterdas publications
|
|
if not os.path.exists(source_folder):
|
|
os.makedirs(source_folder)
|
|
|
|
# Get details for every patent in list
|
|
for publication in publications:
|
|
get_full_publications(publication, source_folder)
|
|
|
|
# Run Script
|
|
if __name__ == '__main__':
|
|
get_publications_from_yesterday() |