Ruby-mp3info album artwork - ruby

I have this gem working such that I can change the id3 data for a given song. However I need to also be able to add album artwork to the song. I have the artwork at a given URL. How do I go about this?
Mp3Info.open(file.path) do |mp3|
mp3.tag.title = title
mp3.tag.artist = artist
end

It seems ruby-mp3info only supports text frames at the moment, see here: https://github.com/moumar/ruby-mp3info/blob/v0.7.1/lib/mp3info/id3v2.rb#L319
Using taglib-ruby, it would work like this:
require 'taglib'
require 'open-uri'
picture_data = open(picture_url).read
TagLib::MPEG::File.open(file.path) do |file|
tag = file.id3v2_tag
pic = TagLib::ID3v2::AttachedPictureFrame.new
pic.picture = picture_data
pic.mime_type = "image/jpeg"
pic.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
tag.add_frame(pic)
file.save
end

If you are not stuck with mp3Info gem, try using id3Lib, http://id3lib-ruby.rubyforge.org/. From my experience, that one's better.
not sure about this, but try reading the file and setting it directly to
mp3.tag2.APIC

Using ruby-mp3info you can add artwork:
From the documentation:
file = File.new('input_img','rb')
Mp3Info.open '1.mp3' do |m|
m.tag2.add_picture(file.read)
end

Related

Scroll automation appium

I'm doing an automation in Ruby and Appium on mobile, and I need to access a card that is out of range, and I'm using all possible methods and it just happens error ... Does anyone have a solution? I need to down the Recycler view all the way down
Edit:
code
class AuditoriaController
def initialize
#util = UtilMethods.new
#objects = PageAuditoria.new
main = MenuPrincipal.new
#menus = main.menus
#tela_principal = main.tela_principal
#objects_auditoria = #objects.tela_auditoria
end
def acessar_auditoria
data_sync = find_element(:xpath, #tela_principal[:msg_sincronizacao]).text
data_sync.slice!("Última atualização: ")
t = Time.now
while(t.strftime("%d/%m/%Y %H:%M:%S") != data_sync)
btn_sync = find_element(:xpath, #tela_principal[:view_sync])
btn_sync.click
break;
end
list = find_element(:xpath, #tela_principal[:lista])
list.scrollIntoView()
#menu_auditoria = find_element(:xpath, #menus[:menu_auditoria])
#if(menu_auditoria)
# #util.logger("ACHEI AUDITORIA")
#end
end
First get the element(that is card)
Then add the below code
browser.execute_script('arguments[0].scrollIntoView();', card);
Hope it will help you. If you need any more information just comment here
EDIT:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.execute_script('arguments[0].scrollIntoView();', card)
Here driver is the driver you are using for automation that is appium driver. Here card is the element that you want to get and scroll upto it.
In ruby:
def scroll_to_element(element)
#driverAppium.execute_script('mobile: scroll', name: element.name)
end
This will scroll to the element which you are looking for even if it is offscreen. You should be able to modify for use with Java.
scroll_to_exact(elementname) should do the job for you! This will scroll to through the elements until the specified is visible. This is in Appium_lib for ruby

Goal: scraping URLs, Error Message: No implicit conversion of String into Integer

I'm learning my first coding language (Ruby) and I'm having trouble crawling a link. I'm trying to grab image-URLs and eventually going to save to a CSV. I've looked at many tutorials and a lot of question on here, but none seem to solve my problem.
The problem appears to be in the final line (Line 19).
Error Message: 19:in `[]': no implicit conversion of String into Integer
Any help would be appreciated!
require 'rubygems'
require 'nokogiri'
require 'open-uri'
PAGE_URL = "http://www.bestbuy.com/site/samsung-showcase-27-8-cu-ft-french-door-refrigerator-with-thru-the-door-ice-and-water-stainless-steel/5236091.p?id=1219116001631&skuId=5236091"
page = Nokogiri::HTML(open(PAGE_URL))
link_extract = page.css('div#pdp-content div.image-gallery-main-slide a img[data-index="1"]')
puts link_extract[0]['src']
It looks like a large part of this page is built with javascript.
If you look at the raw html you get you won't see the content you're looking for since youre essentially doing a curl to get the data.
Soo how should you proceed? I'd say you have two options:
The easiest way to get the data you want, is to get the json they're using at #pdp-model-data
Use some kind of web driver that works well with javascript. I tried phantomjs but it looks like that page contains some invalid syntax and it's only pulling the first image.
e.g.
require 'capybara/poltergeist'
url = "http://www.bestbuy.com/site/samsung-showcase-27-8-cu-ft-french-door-refrigerator-with-thru-the-door-ice-and-water-stainless-steel/5236091.p?id=1219116001631&skuId=5236091"
session = Capybara::Session.new(:poltergeist)
session.visit(url)
page = Nokogiri::HTML::DocumentFragment.parse(session.html)(session.html)
page.search("#pdp-content div.image-gallery-main-slide a img")
# => [#<Nokogiri::XML::Element:0x3ffe438d4100 name="img" attributes=[#<Nokogiri::XML::Attr:0x3ffe438d409c name="src" value="http://pisces.bbystatic.com/image2/BestBuy_US/images/products/5236/5236091_sa.jpg;canvasHeight=500;canvasWidth=500">, #<Nokogiri::XML::Attr:0x3ffe438d4088 name="alt" value="Samsung - Showcase 27.8 Cu. Ft. French Door Refrigerator with Thru-the-Door Ice and Water - Stainless-Steel - Larger Front">, #<Nokogiri::XML::Attr:0x3ffe438d4074 name="width" value="500">, #<Nokogiri::XML::Attr:0x3ffe438d4060 name="height" value="500">, #<Nokogiri::XML::Attr:0x3ffe438d404c name="data-index" value="0">, #<Nokogiri::XML::Attr:0x3ffe438d4038 name="style" value="display: block; ">]>]
edit #1:
require 'nokogiri'
require 'open-uri'
require 'json'
url = "http://www.bestbuy.com/site/samsung-showcase-27-8-cu-ft-french-door-refrigerator-with-thru-the-door-ice-and-water-stainless-steel/5236091.p?id=1219116001631&skuId=5236091"
page = Nokogiri::HTML(open(url))
json = page.css('#pdp-model-data').attribute('data-gallery-images').value
gallery = JSON.parse(json, symbolize_names: true)
puts gallery[1][:url]
#=> "http://img.bbystatic.com/BestBuy_US/images/products/5236/5236091cv1a.jpg"

Ruby shoes4 package over 1.5GB

I've made a small CLI script in ruby to manage a small shop for a friend, but then he wanted me to make a GUI for him, so I looked around and found shoes4.
So, I went and download it, created a small test, and run:
./bin/shoes -p swt:jar ./path/to/app.rb
and left it to create the package, then I got a warning from system that I'm running low on disc space, so I went to check the jar file, and it was over 1.5GB and still not done packaging... and the code is very small and basic:
require 'yaml'
Shoes.app do
button "Add client" do
filename = ask_open_file
para File.read(filename)
clients = YAML.load_file(filename)
id = clients[clients.length - 1][0].to_i + 1
name = ask("Enter the client's full name: ")
items = ask("Enter list of items.")
patients[id] = ["ID = "+ id.to_s,"Name = "+ pname,"list of items:\n"+ items]
File.open(filename, 'w') { |f| YAML.dump(clients, f) }
alert ("Added new patient.")
end
button "Exit" do
exit()
end
end
any idea why this small app is more than 1.5GB?? or did I try to package it wrong way??
The packager will include everything in the directory of your shoes script and below.

Retrieve bibtex data from crossref by sending DOI from matlab: translation from ruby

I want to retrieve bibtex data (for building a bibliography) by sending a DOI (Digital Object Identifier) to http://www.crossref.org from within matlab.
The crossref API suggests something like this:
curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842
based on this source.
Another example from here suggests the following in ruby:
open("http://dx.doi.org/10.1038/nrd842","Accept" => "text/bibliography; style=bibtex"){|f| f.each {|line| print line}}
Although I've heard ruby rocks I want to do this in matlab and have no clue how to translate the ruby message or interpret the crossref command.
The following is what I have so far to send a doi to crossref and retrieve data in xml (in variable retdat), but not bibtex, format:
clear
clc
doi = '10.1038/nrd842';
URL_PATTERN = 'http://dx.doi.org/%s';
fetchurl = sprintf(URL_PATTERN,doi);
numinputs = 1;
www = java.net.URL(fetchurl);
is = www.openStream;
%Read stream of data
isr = java.io.InputStreamReader(is);
br = java.io.BufferedReader(isr);
%Parse return data
retdat = [];
next_line = toCharArray(br.readLine)'; %First line contains headings, determine length
%Loop through data
while ischar(next_line)
retdat = [retdat, 13, next_line];
tmp = br.readLine;
try
next_line = toCharArray(tmp)';
if strcmp(next_line,'M END')
next_line = [];
break
end
catch
break;
end
end
%Cleanup java objects
br.close;
isr.close;
is.close;
Help translating the ruby statement to something matlab can send using a script such as that posted to establish the communication with crossref would be greatly appreciated.
Edit:
Additional constraints include backward compatibility of the code (back at least to R14) :>(. Also, no use of ruby, since that solves the problem but is not a "matlab" solution, see here for how to invoke ruby from matlab via system('ruby script.rb').
You can easily edit urlread for what you need. I won't post my modified urlread function code due to copyright.
In urlread, (mine is at C:\Program Files\MATLAB\R2012a\toolbox\matlab\iofun\urlread.m), as the least elegant solution:
Right before "% Read the data from the connection." I added:
urlConnection.setRequestProperty('Accept','text/bibliography; style=bibtex');
The answer from user2034006 lays the path to a solution.
The following script works when urlread is modified:
URL_PATTERN = 'http://dx.doi.org/%s';
doi = '10.1038/nrd842';
fetchurl = sprintf(URL_PATTERN,doi);
method = 'post';
params= {};
[string,status] = urlread(fetchurl,method,params);
The modification in urlread is not identical to the suggestion of user2034006. Things worked when the line
urlConnection.setRequestProperty('Content-Type','application/x-www-form-urlencoded');
in urlread was replaced with
urlConnection.setRequestProperty('Accept','text/bibliography; style=bibtex');

How to check if image is available in ruby?

I guess this is easy, I seem to be missing something simple.
I need to read an image and then display it which i do as follows, I should skip the code if the image is not available.
stu_image ="/admin_num.jpg"
if !stu_image
code for displaying the image
end
But, this is not working. Could somebody help with this please.
Cheers!
Seems to me like you're just saving a string, not the image.
this_dir = File.dirname( __FILE__ )
file = File.expand_path( "admin_num.jpg", this_dir )
Why not just testing if the file exists?
stu_image ="/admin_num.jpg"
FileTest.exists?(RAILS_ROOT + stu_image ) # < rails 3.0
FileTest.exists?(Rails.root + stu_image ) # >= rails 3.0
See File#exists? here:
http://ruby-doc.org/core-1.9.3/File.html#method-c-exists-3F

Resources