server: add import LCSC feature
This commit is contained in:
parent
35a865e2db
commit
2ef85a2008
|
@ -11,6 +11,8 @@ Distributors also need to be managed directly in the database.
|
||||||
Since this does change very often, it isn't too much of a hassle.
|
Since this does change very often, it isn't too much of a hassle.
|
||||||
An example is in `populate.sql`, also with some other entries.
|
An example is in `populate.sql`, also with some other entries.
|
||||||
|
|
||||||
|
To import an LCSC part, simply go to the `/import/lcsc/Cxxxx` page and the part will be added to the database.
|
||||||
|
|
||||||
goals
|
goals
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
|
49
server.rb
49
server.rb
|
@ -15,6 +15,8 @@ require 'set'
|
||||||
require 'mysql2'
|
require 'mysql2'
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'sinatra'
|
require 'sinatra'
|
||||||
|
require 'uri'
|
||||||
|
require 'net/http'
|
||||||
|
|
||||||
# allow dumping crashes in browser
|
# allow dumping crashes in browser
|
||||||
DEBUG = false
|
DEBUG = false
|
||||||
|
@ -365,3 +367,50 @@ post '/part' do
|
||||||
end
|
end
|
||||||
return 200
|
return 200
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get '/import/lcsc/:lcsc' do
|
||||||
|
halt 401 unless params['lcsc'] and params['lcsc'] =~ /^C\d+$/i
|
||||||
|
uri = URI("https://wmsc.lcsc.com/wmsc/product/detail?productCode=#{params['lcsc']}")
|
||||||
|
res = Net::HTTP.get_response(uri)
|
||||||
|
halt 401, "could not get part" unless res.is_a?(Net::HTTPSuccess)
|
||||||
|
json = JSON.parse(res.body)
|
||||||
|
#puts json
|
||||||
|
halt 401, "part not found" unless 200 == json["code"] and json["result"]
|
||||||
|
result = json["result"]
|
||||||
|
part = {}
|
||||||
|
part["name"] = result["productModel"]
|
||||||
|
part["mpn"] = result["productModel"]
|
||||||
|
part["description"] = result["productDescEn"]
|
||||||
|
part["details"] = result["productIntroEn"]
|
||||||
|
part["manufacturer"] = result["brandNameEn"]
|
||||||
|
part["package"] = result["encapStandard"] # also includes pin count
|
||||||
|
part["distributors"] = {"LCSC" => result["productCode"]}
|
||||||
|
part["attachments"] = result["productImages"]
|
||||||
|
part["datasheet"] = result["pdfUrl"]
|
||||||
|
existing = get_part_by_name(part["name"])
|
||||||
|
halt 401, "part name already exists" if existing
|
||||||
|
puts part
|
||||||
|
begin
|
||||||
|
add_part(part)
|
||||||
|
rescue StandardError => e
|
||||||
|
halt 401, e.message
|
||||||
|
end
|
||||||
|
i = 0
|
||||||
|
(part["attachments"] + [part["datasheet"]]).each do |attachement|
|
||||||
|
file = attachement.split("/")[-1]
|
||||||
|
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"]
|
||||||
|
path = "#{dir}/#{i}_#{file}"
|
||||||
|
i += 1
|
||||||
|
unless File.file?(path) then
|
||||||
|
uri = URI(attachement)
|
||||||
|
res = Net::HTTP.get_response(uri)
|
||||||
|
if (res.is_a?(Net::HTTPSuccess)) then
|
||||||
|
Dir.mkdir(dir) unless File.directory?(dir)
|
||||||
|
File.open(path, "wb") do |f|
|
||||||
|
f.write res.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 200
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue