Images from picasaweb on Rails - ruby-on-rails

I need work with images from picasaweb on Rails (add, delete, view). I tried some gems (picasa, etc). But gem and json return only albums, images array is empty.

Use this gem with example:
require "picasa"
# get some photos in an album
client = Picasa::Client.new(user_id: "your-gmail-account#gmail.com", access_token: "oauth-access-token")
albums = client.album.list.entries
album = albums.find { |album| album.title == "New Album" }
photos = client.album.show(album.id).entries
photos.each { |photo| puts photo.title }
https://github.com/morgoth/picasa
https://github.com/morgoth/picasa/blob/master/examples/get_some_photos.rb

Related

Products Filter by title in rails app + shopify_app gem

I have tried mostly everything. Checked all docs and stack questions and shopify community like:
Shopify API how to do a search query with like
https://community.shopify.com/c/Shopify-APIs-SDKs/Shopify-api-search-products-by-title/td-p/341866
How to search products by title using Shopify product search API?
https://github.com/Shopify/shopify_app
https://community.shopify.com/c/Shopify-APIs-SDKs/Search-product-from-title-handle-and-description/td-p/469156
and found out that
#search = "2018";
#products = ShopifyAPI::Product.find(:all, params: { limit: 10,title:#search })
but this is returning empty array although I have may records containing this in title. https://prnt.sc/sx37o6
I want to get records according to #search
I have tried Product.search too but it causes: undefined method `search' for ShopifyAPI::Product:Class
Using the RestAPI I failed doing filtering (with wildcards for example) as well. But with the GraphQL-API the search functionalities (see here https://shopify.dev/concepts/about-apis/search-syntax) are pretty solid.
This is an example including auth, filtering by title including wildcard-support (for part matching) and mapping results to a simpel array of hashes:
#responses = []
shopify_session = ShopifyAPI::Session.temp(
domain: shop.shopify_domain,
token: shop.shopify_token,
api_version: ShopifyApp.configuration.api_version
) do
client = ShopifyAPI::GraphQL.client
ql_query = <<-GRAPHQL
{
products(first: 10, query: "title:*#{query}*") {
edges {
node {
id
title
handle
}
}
}
}
GRAPHQL
query_result = client.query(client.parse(ql_query))
query_result.data.products.edges.each do |result|
#responses << {
id: result.node.id,
title: result.node.title,
handle: result.node.handle
}
end
end
#responses

seed cloudinary photos for rails app

)
I have some cloudinary's photos and I would like to make a seed (in my rails app).
I'm using Carrierwave.
In my seed, I try to put the cloudinary's url image :
Course.create! ({
photo: "fismpnq3zma80dc2ovjt.jpg"
)}
But it's don't work. What can I do ?
If I ask by console :
pry(main)> c = Course.first
....
pry(main)> c.photo
#cache_id=nil,
#file=nil,
#filename=nil,
....
#model=#<Way:0x00007f80e7a3a9c0
photo:nil,
....
#mounted_as=:photo,
#versions=nil>
In order to use the create method, the parameters need to be verified (by signature) before the SDK will save them.
For example:
resource_type = "image"
type = "upload"
version = 1234567890
public_id = "fismpnq3zma80dc2ovjt"
format = "jpg"
signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id,
:version=>version}, Cloudinary.config.api_secret)
photo = "#{resource_type}/#{type}/v#{version}/#{public_id}.#{format}##
{signature}"
Course.create!({ photo: photo )}
Please let me know if it works for you.
--Yakir

save json block of code into nested model ruby rails

I get a hash of results from get_connection with koala gem which looks something like this
=> [{"album"=>{"created_time"=>"2011-05-07T23:06:33+0000", "name"=>"Timeline Photos",
"id"=>"10150170707957371"}, "images"=>[{"height"=>1150, "source"=>"https://scontent.xx.fbcdn.net/v/t31.0-8/24173690_10155086191327371_1463889654041392146_o.jpg?oh=8adec503c6066dc20d1be5d71262a03e&oe=5AFEED4A",
"width"=>2048}, {"album"=>{"created_time"=>"2011-05-07T23:06:33+0000", "name"=>"Timeline Photos",
"id"=>"10150170707957371"}, "images"=>[{"height"=>1188, "source"=>"https://scontent.xx.fbcdn.net/v/t31.0-8/24302317_10155086179077371_4000719398736973936_o.jpg?oh=6eba399a4067b847cb38ef245e687321&oe=5AFC195A",
"width"=>2639}]}]
I was looking to save photos like rails does with the params hash but realized it only creates a gallery for each photo with a do block .
def photos(user)
photos = user.facebook.get_connection("me", "photos?fields=album,images,event,height,width,link,place&type=uploaded")
photos.each do |photo|
params = { gallery: {
name: 'Facebook Pictures', pictures_attributes: [
{ fb_album_created_time: photo['album']['created_time'],
fb_album_name: photo['album']['name'],
fb_album_id: photo['album']['id'],
fb_source: photo['images'][0]['source'],
fb_height: photo['images'][0]['height'],
fb_width: photo['images'][0]['width'],
fb_link: photo['link'],
fb_pic_id: photo['id'] }
]
}
}
gallery = user.galleries.new(params[:gallery])
gallery.save!
end
end
How can I execute my nested picture attributes so they save to one gallery. Even a gem which could help me. I was looking at the rails/jbuider gem but not sure if its easier without a gem?
Fixed this with Jbuilder.
def to_jbuilder
Jbuilder.new
end
def photos(u)
json = to_jbuilder
first_page_photos = u.facebook.get_connection("me", "photos?fields=album,images,event,height,width,link,place&type=uploaded")
photos = json.photo first_page_photos do |photo|
json.fb_album_created_time photo['album']['created_time']
json.fb_album_name photo['album']['name']
json.fb_album_id photo['album']['id']
json.fb_source photo['images'][0]['source']
json.fb_height photo['images'][0]['height']
json.fb_width photo['images'][0]['width']
json.fb_link photo['link']
json.fb_pic_id photo['id']
end
params = { gallery: { name: 'Facebook Pictures', pictures_attributes: photos }}
gallery = u.galleries.new(params[:gallery])
gallery.save!
end

Rails - Reducing queries using includes

This is my controller:
#trainings = Training.includes(:courses).order(:id, 'courses.order_id ASC')
In my view, for each training I need to loop for 3 times and check if has a course with column order_id with these values:
#trainings.each do |training|
for course_order in (1..3) do
this_course = training.courses.find_by(order_id: course_order)
# this_course.image(:resized) #i can print the paperclip image
end
end
The code above executes so many queries, so I tried using select method:
#trainings.each do |training|
for course_order in (1..3) do
this_course = training.courses.select { |course| course.order_id = course_order }
# this_course.image(:resized) #I cannot print paperclip image, because the result is an Array, so doesn't know the method "image"
end
end
So I have just one query, but I cannot call the image method from my Model, because the result is an Array object.
I know I can use:
training.courses.each do |course|
# course.image(:resized)
end
and I will get just one query, but I must loop 3 times, so when I dont have the row I can print a placeholder image, like:
Example image http://www.onrails.com.br/order_id.jpg
Your
this_course = training.courses.find_by(order_id: course_order)
returns first record that matches conditions, but
this_course = training.courses.select { |course| course.order_id == course_order }
selects all courses that match condition. If you want to find only the first one, use .detect instead of .select:
this_course = training.courses.detect { |course| course.order_id == course_order }
Also you have a typo. = is for assignment, == is for comparing.

How do I pull the correct image URL from this Wikipedia table?

I built a scraper to pull all the information out of a Wikipedia table and upload it to my database. All was good until I realized I was pulling the wrong URL on images, and I wanted the actual image URL "http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Baconbutty.jpg" and not the "/wiki/File:Baconbutty.jpg" it was apt to give me. Here is my code so far:
def initialize
#url = "http://en.wikipedia.org/wiki/List_of_sandwiches"
#nodes = Nokogiri::HTML(open(#url))
end
def summary
sammich_data = #nodes
sammiches = sammich_data.css('div.mw-content-ltr table.wikitable tr')
sammich_data.search('sup').remove
sammich_hashes = sammiches.map {|x|
if content = x.css('td')[0]
name = content.text
end
if content = x.css('td a.image').map {|link| link ['href']}
image =content[0]
end
if content = x.css('td')[2]
origin = content.text
end
if content = x.css('td')[3]
description =content.text
end
My issue is with this line:
if content = x.css('td a.image').map {|link| link ['href']}
image =content[0]
If I go to td a.image img, it just gives me a null entry.
Any suggestions?
Here's how I'd do it (if I was to scrape Wikipedia, which I wouldn't because they do have an API for this stuff):
require 'nokogiri'
require 'open-uri'
require 'pp'
doc = Nokogiri::HTML(open("http://en.wikipedia.org/wiki/List_of_sandwiches"))
sammich_hashes = doc.css('table.wikitable tr').map { |tr|
name, image, origin, description = tr.css('td,th')
name, origin, description = [name, origin, description].map{ |n| n && n.text ? n.text : nil }
image = image.at('img')['src'] rescue nil
{
name: name,
origin: origin,
description: description,
image: image
}
}
pp sammich_hashes
Which outputs:
[
{:name=>"Name", :origin=>"Origin", :description=>"Description", :image=>nil},
{
:name=>"Bacon",
:origin=>"United Kingdom",
:description=>"Often served with ketchup or brown sauce",
:image=>"//upload.wikimedia.org/wikipedia/commons/thumb/3/38/Baconbutty.jpg/120px-Baconbutty.jpg"
},
... [lots removed] ...
{
:name=>"Zapiekanka",
:origin=>"Poland",
:description=>"A halved baguette or other bread usually topped with mushrooms and cheese, ham or other meats, and vegetables",
:image=>"//upload.wikimedia.org/wikipedia/commons/thumb/1/12/Zapiekanka_3..jpg/120px-Zapiekanka_3..jpg"
}
]
If an image isn't available, the field will be set to nil in the returned hashes.
You could use the srcset attribute of the imgelement, split it and keep one of the available resized images.
if content = x.at_css('td a.image img')
image =content['srcset'].split(' 1.5x,').first

Resources