Showing posts with label ebay. Show all posts
Showing posts with label ebay. Show all posts

Ebay Stocks Price Alert

Tired of checking stock price value every day? If you are waiting for a stock price to rise to a certain value to sell it, or waiting for the price to drop to a certain value to buy it, here you go, ruby scripting to the rescue.

This script will send you an email when stock price falls off a certain tresholds.

Set your own thresholds changing the "low" & "high" values.
Set your preferred stock symbol changing "EBAY" to "GOOGLE" for example. Is easeally extendable to support several stock symbols. I can post a second script if there's enough demand.

To set it up running every day, use windows Task Scheduler or CRON for linux and mac machines.

It does require the ruby mailfactory gem, use "gem install mailfactory" to get it on your machine, and also add in your email server configuration.


 require 'net/http'
require 'net/smtp'
require 'mailfactory'

def get_stock_quote(symbol)

host = "finance.google.com"
link = "/finance?q="+ symbol.upcase

begin

# Create a new HTTP connection
httpCon = Net::HTTP.new( host, 80 )

# Perform a HEAD request
resp = httpCon.get( link, nil )

value = (resp.body.scan /class="pr"[^>]*([^<]*)/).flatten.to_s.gsub(">", "").to_f

print " current #{symbol} stock price is " + value.to_s + " (from finance.google.com)\n"
return value
end

end


def send_stock_alert(subject="Ebay Stock Alert", message="Value Alert")

begin
mail = MailFactory.new()
mail.to = 'myself@gmail.com'
mail.from = "ebaystock@alerts.com"
mail.subject = subject
mail.html = message

Net::SMTP.start('yourmailserver') do |smtp|
smtp.send_message(mail.to_s(), mail.from, mail.to )
end

rescue StandardError => e
puts "Error sending mail"
raise e
end

end

begin

low = 23.0
high = 27.0
name = "EBAY"
price = get_stock_quote(name)

send_stock_alert("#{name} Stock Alert: #{price}") unless price.between? low, high

end

Ebay Find

A small ruby script that does searches in ebay. Is capable of:
  • introducing some misspells in order to find those misspelled items that might be a great bargain(because nobody else is able to find them)
  • and also is capable of searching more than one ebay store.
Prints out to console the search results. Of course because is in ruby, is easy to modify and play around :)

Quick and dirty pulled together, but posting here in case someone else finds it useful.

Use like this:
for multiple keyword search:
ruby ebayfind.rb "Ibanez guitar"

for single keyword search:
ruby ebayfind.rb Ibanez

Code:


require 'net/http'
require 'rexml/document'
require 'active_support'


class Misspell

 def self.a(a_text="word", options=["skip_letter", "double_letters",
   "reverse_letters","skip_spaces","missed_key","inserted_key" ])

    ## Sets up the params    params = Hash.new
   params["user_input"] = a_text
   options.each do |opt|
     params[opt] = opt
   end

    #executes a call    res = Net::HTTP.post_form(URI.parse('http://tools.seobook.com/spelling/keywords-typos.cgi'), params)

    #cleans and formats results    res.body.gsub("\n", ',').scan(/<textarea rows[^>]*(.*)<\/textarea>/).flatten.to_s.gsub(">", "").split(',')
 end
end



if ARGV.length == 0
 puts "#{$0}: You must enter at least one argument."
 exit
end

search_str = [ARGV[0]]

puts "Finding: #{search_str}"
output = ""

#bay_stores = {"1"=>"US", "3"=>"GB", "77"=>"DE", "71"=>"FR", "186"=>"ES", "146"=>"NL"}bay_stores = {"3"=>"GB", "77"=>"DE", "71"=>"FR", "186"=>"ES", "146"=>"NL"}

puts "Creating the misspells..."
search_str << Misspell.a(search_str, ["skip_letter", "double_letters", "reverse_letters","skip_spaces","missed_key","inserted_key" ])
search_str.flatten!
puts "Search item with added misspells: #{search_str.join(",")}"

#Iterate through each Ebay storebay_stores.each_key do |siteid|

  # Iterate through each  search_str.each do |query_string|

    # Put together an eBay parameter string    ebay_params = {'callname'                     =>'FindItemsAdvanced',
     'appid'                       =>'TODO:_YOUR_OWN_EBAY_API_ID',
     'version'                     =>'553',
     'responseencoding'            =>'XML',
     'siteid'                      =>siteid,
     'MessageID                   '=>'',
     'BidCountMax                 '=>'',
     'BidCountMin                 '=>'',
     'CategoryHistogramMaxChildren'=>'',
     'CategoryHistogramMaxParents '=>'',
     'CategoryID                  '=>'',
     'CharityID                   '=>'',
     'Condition                   '=>'',
     'Currency                    '=>'',
     'DescriptionSearch           '=>'',
     'EndTimeFrom                 '=>'',
     'EndTimeTo                   '=>'',
     'ExcludeFlag                 '=>'',
     'FeedbackScoreMax            '=>'',
     'FeedbackScoreMin            '=>'',
     'GroupMaxEntries             '=>'',
     'GroupsMax                   '=>'',
     'IncludeSelector             '=>'',
     'ItemsAvailableTo            '=>'',
     'ItemsLocatedIn              '=>'',
     'ItemSort                    '=>'',
     'ItemType                    '=>'AllItemTypes',
     'MaxDistance                 '=>'',
     'MaxEntries                  '=>'',
     'ModTimeFrom                 '=>'',
     'PageNumber                  '=>'',
     'PaymentMethod               '=>'PayPal',
     'PostalCode                  '=>'',
     'PreferredLocation           '=>'',
     'PriceMax                    '=>'',
     'PriceMin                    '=>'',
     'ProductID                   '=>'',
     'Quantity                    '=>'',
     'QuantityOperator            '=>'',
     'QueryKeywords               '=> (query_string.gsub(' ', '%20')),
     'SearchFlag                  '=>'',
     'SellerBusinessType          '=>'',
     'SellerID                    '=>'',
     'SellerIDExclude             '=>'',
     'SortOrder                   '=>'',
     'StoreName                   '=>'',
     'StoreSearch                 '=>''
   }.map { |key,value| "#{key.strip}=#{value}" unless value.empty? }.join("&").squeeze('&')

    # Ask eBay what it knows about our query_string    ebay_response = Net::HTTP.get_response('open.api.ebay.com', '/shopping?' << ebay_params)

   xml = REXML::Document.new(ebay_response.body)

    # Get basic information    response3 =  Hash.from_xml(xml.to_s)

   xml.root.elements.each("/FindItemsAdvancedResponse/SearchResult/ItemArray/Item") do |element|
     item =  Hash.from_xml(element.to_s)
     puts ""
     puts ">> Searching for: #{query_string}, in #{bay_stores[siteid]}, got #{response3["FindItemsAdvancedResponse"]["TotalItems"]} results"
     puts item['Item']['Title']
     puts item['Item']['EndTime']
     puts item['Item']['ConvertedCurrentPrice']
     puts item['Item']['GalleryURL']
     puts item['Item']['ListingType']
     puts item['Item']['Condition']
     puts item['Item']['ViewItemURLForNaturalSearch']
   end
 end
end