How can i send time after time 12am vb6 - vb6

I have coded it, but it doesn't do what i have coded. Here is my code looks like:
Private Sub timer_Timer()
dateoutput.Caption = Format(Now, "General date")
If theTime >= #12:00:00 AM# And theTime <= #11:59:59 AM# Then
welcome.Caption = "Good Morning"
ElseIf theTime >= #11:59:59 AM# And theTime <= #6:00:00 PM# Then
welcome.Caption = "Good Evening"
ElseIf theTime >= #6:00:00 PM# And theTime <= #11:59:59 PM# Then
welcome.Caption = "Good Night"
End If
End Sub

I think the problem is that you are comparing something that has a date component to something that doesn't, so you'll never get it to work the way you want.
This is better:
Private Sub Timer_Timer()
Dim theTime As String
theTime = TimeValue(Now)
dateOutput.Caption = Format(Now, "General date")
If theTime >= #12:00:00 AM# And theTime < #12:00:00 PM# Then
welcome.Caption = "Good Morning"
ElseIf theTime >= #12:00:00 PM# And theTime < #6:00:00 PM# Then
welcome.Caption = "Good Evening"
ElseIf theTime >= #6:00:00 PM# And theTime <= #11:59:59 PM# Then
welcome.Caption = "Good Night"
End If
End Sub
Also note I've changed the values used for the check ranges slightly.
Edit It doesn't like checking "< #12:00:00 AM#" so it needs to be "< #11:59:59 PM#"

you have a little error in 12:00:00 AM => PM
Private Sub timer_Timer()
dateoutput.Caption = Format(Now, "General date")
If theTime >= #12:00:00 PM# And theTime <= #11:59:59 AM# Then
welcome.Caption = "Good Morning"
ElseIf theTime >= #11:59:59 AM# And theTime <= #6:00:00 PM# Then
welcome.Caption = "Good Evening"
ElseIf theTime >= #6:00:00 PM# And theTime <= #11:59:59 PM# Then
welcome.Caption = "Good Night"
End If
End Sub

Related

Ruby Rock, Paper, Scissors

Im having an issue with the score updater for this rock, paper, scissors game I have to create for my ruby class. The purpose of this assignment is to have the user input a choice, the computer makes a random choice as well using "rand", and whoever wins the round has a score updated that is displayed at the end of each round. The problem im running into is that the counter only updates using the first if statement. The program works up to the point that it is able to randomly select the computer choice and it saves the users response as well. So all the code is fine up to where the score updater is used. Any assistance would be highly appreciated.
human_score = 0.0
comp_score = 0.0
while true
print "Lets play rock,paper,scissors", "\n"
print "Do you choose rock, paper, or scissors?", "\n"
choice = STDIN.gets.chomp
comp = rand(3)
if comp == 1
comp_choice = "rock"
elsif comp == 2
comp_choice = "paper"
else
comp_choice = "scissors"
end
print "Computer picked: ", comp_choice, "\n"
print "You picked: ", choice, "\n"
if human_choice = "rock" && comp_choice = "rock"
comp_score += 0.5
human_score += 0.5
print "The result is a tie", "\n"
elsif human_choice = "rock" && comp_choice = "paper"
comp_score += 1
print "Computer wins", "\n"
elsif human_choice = "rock" && comp_choice = "scissors"
human_score += 1
print "You win", "\n"
elsif human_choice = "paper" && comp_choice = "rock"
human_score += 1
print "You win", "\n"
elsif human_choice = "paper" && comp_choice = "paper"
comp_score += 0.5
human_score += 0.5
print "Its a tie", "\n"
elsif human_choice = "paper" && comp_choice = "scissors"
comp_score += 1
print "Computer wins", "\n"
elsif human_choice = "scissors" && comp_choice = "rock"
comp_score += 1
print "Computer wins"
elsif human_choice = "scissors" && comp_choice = "paper"
human_score += 1
print "You win"
else human_choice = "scissors" && comp_choice = "scissors"
human_score += 0.5
comp_score += 0.5
print "Its a tie"
end
print "The computers score is: ", comp_score, "\n"
print "Your score is: ", human_score, "\n"
end
You are reassigning the value of your variables instead of checking for equality.
Your version:
if human_choice = "rock" && comp_choice = "rock"
This sets the value of human_choice to "rock" and same with comp_choice. It will always be true.
Correct version:
if human_choice == "rock" && comp_choice == "rock"
Also, you are assigning the value read from the keyboard to choice
choice = STDIN.gets.chomp
and you are using human_choice to validate the cases.
Meditate on this:
human_score = 0.0
comp_score = 0.0
while true
puts "Lets play rock, paper, scissors"
puts "Do you choose rock, paper, or scissors?"
human_choice = STDIN.gets.chomp
comp_choice = %w[scissors rock paper][rand(3)]
puts "Computer picked: #{ comp_choice }"
puts "You picked: #{ human_choice }"
msg = case [human_choice, comp_choice]
when ['rock', 'rock'], ['paper', 'paper'], ['scissors', 'scissors']
comp_score += 0.5
human_score += 0.5
'The result is a tie'
when ['rock', 'scissors'], ['paper', 'rock'], ['scissors', 'paper']
human_score += 1
'You win'
when ['paper', 'scissors'], ['scissors', 'rock'], ['rock', 'paper']
comp_score += 1
'Computer wins'
end
puts msg
puts "The computer's score is: #{ comp_score }"
puts "Your score is: #{ human_score }"
end
It's written using idiomatic Ruby.

How to have the computer randomize statements with in a vbs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I been working on a .vbs for a modified version of the Paper, Rock, and Scissors game but have had nothing but error after error. Please be gentle on the the amount of but hurt as i am still very new to the language of .vbs here is what i have so far.
Right now as it stands the code looks as follows:
`'***********************************************************************
'Script Name: RockPaperScissorsLizardSpock.vbs
'Author: Chuck.Norris
'Created: 03/03/15
'Description: This script prompts the user to Play a game.
'***********************************************************************
'Formally declare variables used by the script before trying to use them
Option Explicit
Dim objWshShell, strAnswer, strCardImage, strResults, GetRandomNumber, intGetRandomNumber, strCounter
strCounter = 1
Do while (strCounter <= 5)
strCounter = strCounter + 1
'Create an instance of the WScript object in order to later use the popup method
Set objWshShell = Wscript.CreateObject("Wscript.Shell")
strResults = "None"
'Display the rules of the game
strAnswer = InputBox("Please type Rock, Paper, Scissors, Lizard, Spock." & _
vbCrLf & vbCrLf & "Rules:" & vbCrLf & vbCrLf & _
"1. Guess the same thing as the computer to tie." & vbCrLf & _
"2. Paper covers rock and wins." & vbCrLf & _
"3. Rock crushes lizard and wins." & vbCrLf & _
"4. Lizard poisons Spock and wins." & vbCrLf & _
"5. Spock smashes Scissors and wins." & vbCrLf & _
"6. Scissors decapitates Lizard and wins." & vbCrLf & _
"7. Lizard eats paper and wins." & vbCrLf & _
"8. Paper disproves Spock and wins." & vbCrLf & _
"9. Spock vaporizes Rock and wins." & vbCrLf & _
"10. And as it always has...Rock crushes scissors and wins." & vbCrLf, "Let's Play a Game!")
'Time for the computer to randomly pick a choice
Randomize
GetRandomNumber = Round(FormatNumber(Int((5 * Rnd) + 1)))
intGetRandomNumber = GetRandomNumber
If GetRandomNumber = 5 Then strCardImage = "Lizard"
If GetRandomNumber = 4 Then strCardImage = "Spock"
If GetRandomNumber = 3 Then strCardImage = "Rock"
If GetRandomNumber = 2 Then strCardImage = "Scissors"
If GetRandomNumber = 1 Then strCardImage = "Paper"
'When you select rock
If strAnswer = "rock" Then
If intGetRandomNumber = 5 Then strResults = "You Win"
If intGetRandomNumber = 4 Then strResults = "You Lose"
If intGetRandomNumber = 3 Then strResults = "Tie"
If intGetRandomNumber = 2 Then strResults = "You Win"
If intGetRandomNumber = 1 Then strResults = "You Lose"
End If
'When you select scissors
If strAnswer = "scissors" Then
If intGetRandomNumber = 5 Then strResults = "You Win"
If intGetRandomNumber = 4 Then strResults = "You win"
If intGetRandomNumber = 3 Then strResults = "You Lose"
If intGetRandomNumber = 2 Then strResults = "Tie"
If intGetRandomNumber = 1 Then strResults = "You Lose"
End If
'When you select paper
If strAnswer = "paper" Then
If intGetRandomNumber = 5 Then strResults = "You Lose"
If intGetRandomNumber = 4 Then strResults = "You Lose"
If intGetRandomNumber = 3 Then strResults = "You Win"
If intGetRandomNumber = 2 Then strResults = "You Win"
If intGetRandomNumber = 1 Then strResults = "Tie"
End If
'When you select spock
If strAnswer = "spock" Then
If intGetRandomNumber = 5 Then strResults = "You Lose"
If intGetRandomNumber = 4 Then strResults = "Tie"
If intGetRandomNumber = 3 Then strResults = "You Win"
If intGetRandomNumber = 2 Then strResults = "You Win"
If intGetRandomNumber = 1 Then strResults = "You Lose"
End If
'When you select lizard
If strAnswer = "lizard" Then
If intGetRandomNumber = 5 Then strResults = "Tie"
If intGetRandomNumber = 4 Then strResults = "You Lose"
If intGetRandomNumber = 3 Then strResults = "You Lose"
If intGetRandomNumber = 2 Then strResults = "You Win"
If intGetRandomNumber = 1 Then strResults = "You Win"
End If
If strResults = "None" Then
objWshShell.Popup "Sorry. Your answer was not recognized. " & _
"Please type rock, paper, scissors, lizard, or spock in lower case letters."
WScript.Quit
End if
'Display the game's results so that the user can see if he or she won.
objWshShell.Popup "You picked: " & Space(12) & strAnswer & vbCrLf & _
vbCrLf & "Computer picked: " & space(12) & strCardImage & vbCrLf & _
vbCrLf & "=================" & vbCrLf & vbCrLf & "Results: " & _
strResults
Loop`
Script is working and i have learned in the process keep moving forward.
(1) Adding a double quote to
vbCrLf & =================" & vbCrLf & vbCrLf & "Results: " & _
to get
vbCrLf & "=================" & vbCrLf & vbCrLf & "Results: " & _
makes your script syntactically correct.
(2) Putting an Option Explicit at the top will force you to Dim the variables GetRandomNumber and intGetRandomNumber, and point out the (repeated) typo strRults for strResults.
(3) Your
intGetRandomNumber = GetRandomNumber = Round(FormatNumber(Int((5 * Rnd) + 1)))
does not assign the random number to GetRandomNumber and intGetRandomNumber but compares (empty) GetRandomNumber to that number and assigns the boolean result (False) to intGetRandomNumber. For just now use
GetRandomNumber = Round(FormatNumber(Int((5 * Rnd) + 1)))
intGetRandomNumber = GetRandomNumber
Evidence:
>> Dim a, b
>> b = a = 1
>> WScript.Echo TypeName(a)
>> WScript.Echo TypeName(b), CStr(b)
>>
Empty
Boolean False
That should give you a 'working' script.
P.S. What can be done to/with VBScript is determined by the Current Set of Knowledge and Skills (CSKS) of the programmer, not by the editor.
Update wrt comment/last edit:
Your last edit still contains
intGetRandomNumber = GetRandomNumber = Round(FormatNumber(Int((5 * Rnd) + 1)))
I told you why this can't possibly work and that you need to replace this by
GetRandomNumber = Round(FormatNumber(Int((5 * Rnd) + 1)))
intGetRandomNumber = GetRandomNumber

Visual Basic Tic Tac Toe error

hey everyone i'm trying to create ttt and it works but when i do a test run and i make a player win, the last character doesn't show. if it's supposed to be 3 X's or O's aligned, the computer will recognize the winning move and declare a winner but btnclicked.text s still = nothing. here's my code, i have no idea how to fix it and because of that, the if statements that would recognize a draw and produce an output an't be evaluated.
Private Sub btnMoveMade_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
btn00.Click, btn01.Click, btn02.Click, btn10.Click, btn11.Click, btn12.Click, btn20.Click,
btn21.Click, btn22.Click
Dim btnSquareClicked As Button = sender
Dim player1 As String = Me.txtName1.Text
Dim player2 As String = Me.txtname2.Text
Static chrTTT(2, 2) As Char
Static player As String = "X"
If btnSquareClicked.Text <> Nothing Then
MessageBox.Show("Invalid Move.")
Else
Dim index As String
index = btnSquareClicked.Tag
Dim x As Integer = Val(index.Chars(0))
Dim y As Integer = Val(index.Chars(1))
Call StoreMove(x, y, player, chrTTT)
If IsWinner(chrTTT) Then
player = player1
MessageBox.Show(player & "!, Congratulations, You Won!")
btnNewGame.Visible = True
ElseIf IsWinner2(chrTTT) Then
player = player2
MessageBox.Show(player & "!, Congratulations, You won!")
btnNewGame.Visible = True
ElseIf btn00.Text <> Nothing And btn01.Text <> Nothing And btn02.Text <> Nothing And btn10.Text <> Nothing And btn11.Text <> Nothing _
And btn12.Text <> Nothing And btn20.Text <> Nothing And btn21.Text <> Nothing _
And btn22.Text <> Nothing And IsWinner(chrTTT) = False And IsWinner2(chrTTT) = False Then
MessageBox.Show("Aww, it's a draw")
Else
If player = "X" Then
player = "O"
btnSquareClicked.Text = "X"
Else
player = "X"
btnSquareClicked.Text = "O"
End If
End If
End If
End Sub
Sub StoreMove(ByVal x As Integer, ByVal y As Integer, ByVal player As Char, ByRef TTT(,) As Char)
TTT(x, y) = player
End Sub
Function IsWinner(ByRef TTT(,) As Char) As Boolean
For row As Integer = 0 To 2
If TTT(row, 0) = TTT(row, 1) And TTT(row, 1) = TTT(row, 2) And TTT(row, 0) = "X" Then
Return True
End If
Next row
For col As Integer = 0 To 2
If TTT(0, col) = TTT(1, col) And TTT(1, col) = TTT(2, col) And TTT(0, col) = "X" Then
Return True
End If
Next col
If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) And TTT(0, 0) = "X" Then
Return True
End If
If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) And TTT(0, 2) = "X" Then
Return True
End If
Dim movesLeft As Boolean = False
For row As Integer = 0 To 2
For col As Integer = 0 To 2
If TTT(row, col) = Nothing Then
movesLeft = True
End If
Next col
Next row
If Not movesLeft Then
Return True
End If
Return False
End Function
Function IsWinner2(ByRef TTT(,) As Char) As Boolean
For row As Integer = 0 To 2
If TTT(row, 0) = TTT(row, 1) And TTT(row, 1) = TTT(row, 2) And TTT(row, 0) = "O" Then
Return True
End If
Next row
For col As Integer = 0 To 2
If TTT(0, col) = TTT(1, col) And TTT(1, col) = TTT(2, col) And TTT(0, col) = "O" Then
Return True
End If
Next col
If TTT(0, 0) = TTT(1, 1) And TTT(1, 1) = TTT(2, 2) And TTT(0, 0) = "O" Then
Return True
End If
If TTT(0, 2) = TTT(1, 1) And TTT(1, 1) = TTT(2, 0) And TTT(0, 2) = "O" Then
Return True
End If
Dim movesLeft As Boolean = False
For row As Integer = 0 To 2
For col As Integer = 0 To 2
If TTT(row, col) = Nothing Then
movesLeft = True
End If
Next col
Next row
If Not movesLeft Then
Return True
End If
Return False
End Function
End Class
Set the Button Text before you do anything else:
If btnSquareClicked.Text <> Nothing Then
MessageBox.Show("Invalid Move.")
Else
btnSquareClicked.Text = player
' ... rest of the code ...
End If
thanks a lot, i just tried it and it worked, but the the message box that is suppose to show when it's a draw till doesn't come up, instead it still tells me that one of the players won
ElseIf btn00.Text <> Nothing And btn01.Text <> Nothing And btn02.Text <> Nothing And btn10.Text <> Nothing And btn11.Text <> Nothing _
And btn12.Text <> Nothing And btn20.Text <> Nothing And btn21.Text <> Nothing _
And btn22.Text <> Nothing And IsWinner(chrTTT) = False And IsWinner2(chrTTT) = False Then
MessageBox.Show("Aww, it's a draw")
that's the section dedicated to checking for a draw.

While loop continues despite conditions not being met?

When I run the game and type either "yes" or "no" at the end, it always reverts back to the start of the while loop at line 41, when the conditions for both that and the containing loop are not met.
replay = true
while replay
#Pre-var
print "What difficulty? (1 for easy, 2 for medium, or 3 for hard): "
difficulty = gets.chomp.to_i
until difficulty == 1 || difficulty == 2 || difficulty == 3 do
print "Please type either 1, 2, or 3: "
difficulty = gets.chomp.to_i
end
#Variables
if difficulty == 1
r = Random.new
number = r.rand(100..1000)
puts "You have 15 guesses. "
print "Guess a number with three digits: "
within_num = 50
elsif difficulty == 2
r = Random.new
number = r.rand(1000..10000)
puts "You have 15 guesses. "
print "Guess a number with four digits: "
within_num = 500
elsif difficulty == 3
r = Random.new
number = r.rand(10000..100000)
puts "You have 15 guesses. "
print "Guess a number with five digits: "
within_num = 5000
end
guess = ""
num_guess = 0
guess_array = Array.new
array_location = 0
count_through = 0
array_print = count_through - 1
replay_inner = true
#Body
puts number
while num_guess <= 14 || replay_inner == true #Keeping as <= 14 as to avoid unnecessarily rewriting code, still gives 15 guesses
guess = gets.chomp.to_i
if guess > number * 2
print "That is more than double the number. Guess again: "
elsif guess < number / 2
print "That is less than half the number. Guess again: "
elsif guess > number && guess < number + within_num #within_num: 50 for easy, 500 for medium, 5000 for hard
print "You are close. That is too big. Guess again: "
elsif guess < number && guess > number - within_num
print "You are close. That is too small. Guess again: "
elsif guess < number
print "That is too small. Guess again: " #Hinting the user to how close they are.
elsif guess > number
print "That is too big. Guess again: "
elsif guess == number
puts "Congragulations! You win!"
print "Your "
print guess_array.length
print " incorrect guesses were: "
if num_guess == 0
sleep(0.5)
print "... Oh."
else
while count_through < num_guess #Loop to relay user's guesses with a delay of 0.5 seconds
print guess_array[count_through]
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
puts "Would you like to play again? (yes/no)"
replay_answer = gets.chomp
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
if replay_answer == "yes" || replay_answer == "y"
replay = true
puts "yes"
elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
replay = false
puts "no"
end
end
end
guess_array.push guess
num_guess += 1
#puts num_guess
#puts guess_array[array_location]
array_location += 1
if num_guess >= 15 && guess != number
puts "Sorry, you lost. "
print "Your "
print guess_array.size
print " guesses were: "
while count_through < num_guess
print guess_array[count_through] #Same as loop above; for when player fails to guess correctly
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
puts "Would you like to play again? (yes/no)"
replay_answer = gets.chomp
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
if replay_answer == "yes" || replay_answer == "y"
replay = true
replay_inner = true
puts "yes"
elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
replay = false
replay_inner = false
puts "no"
end
end
end
I think the condition in the while should be:
numberGuess<=14 && replay_inner == true
The way you and especially your fellow programmers will find this and future bugs is by properly commenting your code, e.g.:
# ask the user about an optional replay
replay_answer = gets.chomp
# only accept yes/y/no/n
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
## recoded this to a case, as I think it's much nicer :)
# determining whether to replay or to stop the loop
case replay_answer
when "yes", "y"
replay = true
puts "yes"
when "no", "n"
replay = false
puts "no"
replay_inner = false
end
I've modified a little your code, you should really use some decent editor with at least syntax errors highlighting. I've changed OR to AND condition in inner loop (comment in code) and added way of breaking from it when guessed number. I've also removed second occurence of code responsible of playing again, remember, DRY yourself.
replay = true
while replay
p replay
#Pre-var
print "What difficulty? (1 for easy, 2 for medium, or 3 for hard): "
difficulty = gets.chomp.to_i
until difficulty == 1 || difficulty == 2 || difficulty == 3 do
print "Please type either 1, 2, or 3: "
difficulty = gets.chomp.to_i
end
#Variables
if difficulty == 1
r = Random.new
number = r.rand(100..1000)
puts "You have 15 guesses. "
print "Guess a number with three digits: "
within_num = 50
elsif difficulty == 2
r = Random.new
number = r.rand(1000..10000)
puts "You have 15 guesses. "
print "Guess a number with four digits: "
within_num = 500
elsif difficulty == 3
r = Random.new
number = r.rand(10000..100000)
puts "You have 15 guesses. "
print "Guess a number with five digits: "
within_num = 5000
end
guess = ""
num_guess = 0
guess_array = Array.new
array_location = 0
count_through = 0
array_print = count_through - 1
replay_inner = true
#Body
puts number
while num_guess <= 14 && replay_inner == true #Keeping as <= 14 as to avoid unnecessarily rewriting code, still gives 15 guesses
guess = gets.chomp.to_i
if guess > number * 2
print "That is more than double the number. Guess again: "
elsif guess < number / 2
print "That is less than half the number. Guess again: "
elsif guess > number && guess < number + within_num #within_num: 50 for easy, 500 for medium, 5000 for hard
print "You are close. That is too big. Guess again: "
elsif guess < number && guess > number - within_num
print "You are close. That is too small. Guess again: "
elsif guess < number
print "That is too small. Guess again: " #Hinting the user to how close they are.
elsif guess > number
print "That is too big. Guess again: "
elsif guess == number
puts "Congragulations! You win!"
print "Your "
print guess_array.length
print " incorrect guesses were: "
if num_guess == 0
sleep(0.5)
print "... Oh."
else
while count_through < num_guess #Loop to relay user's guesses with a delay of 0.5 seconds
print guess_array[count_through]
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
end
replay_inner = false # or just `break`, you have to break somehow from inner loop here
end
guess_array.push guess
num_guess += 1
#puts num_guess
#puts guess_array[array_location]
array_location += 1
if num_guess >= 15 && guess != number
puts "Sorry, you lost. "
print "Your "
print guess_array.size
print " guesses were: "
while count_through < num_guess
print guess_array[count_through] #Same as loop above; for when player fails to guess correctly
if count_through == num_guess - 2
print ", and "
elsif count_through == num_guess - 1
puts ". "
else
print ", "
end
count_through += 1
sleep(0.5)
end
end
end
puts "\nWould you like to play again? (yes/no)"
replay_answer = gets.chomp
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
print "Please answer with yes, y, no, or n: "
replay_answer = gets.chomp
end
if replay_answer == "yes" || replay_answer == "y"
replay = true
replay_inner = true
puts "yes"
elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
replay = false
replay_inner = false
puts "no"
end
end

Yahoo authentication

I'm playing around with the new yahoo API. I'd like to scrap some dummy data using the following address
http://query.yahooapis.com/v1/public/yql?q=desc%20social.updates.search&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc
When I run this I get an authenticaion error (Need to be logged into Yahoo) This is fine obviously for me messing around on the internet. However I'd like to call this from a ruby script. Any ideas how I go about authenticating? I can only seem to find some web enabled version.
You might try the Mechanize gem for this. I've used it for other authenticated services
in the past.
I'd also recomment httparty -- It is ridiculously easy to map JSON services with this. Try this:
require 'rubygems'
require 'httparty'
class Yahoo
include HTTParty
# i don't think you need auth for this endpoint -- but if you do, uncomment below and fill it in
#basic_auth 'username', 'password'
format :json
def self.load
self.get 'http://query.yahooapis.com/v1/public/yql', :query => {:q => 'desc social.updates.search', :format => 'json', :diagnostics => true, :env => 'store://datatables.org/alltableswithkeys'}
end
end
puts Yahoo.load
You could try omniauth-yahoo for authorization, but it's seen didn't support get the new token after expired.
Public Function ScanColumns(SheetName As String, thisMany As Double, ShowWhat As String)
e = 0
For a = 1 To thisMany
aa = Application.WorksheetFunction.CountA(Sheets(SheetName).Cells(1, a).EntireColumn)
If aa > 0 Then
r = a
If e = 0 Then
e = a
End If
End If
Next a
If ShowWhat = "MIN" Then
ScanColumns = e
End If
If ShowWhat = "MAX" Then
ScanColumns = r
End If
End Function
Public Function ScanRows(SheetName As String, thisMany As Double, ShowWhat As String)
e = 0
For a = 1 To thisMany
aa = Application.WorksheetFunction.CountA(Sheets(SheetName).Cells(a, 1).EntireRow)
If aa > 0 Then
r = a
If e = 0 Then
e = a
End If
End If
Next a
If ShowWhat = "MIN" Then
ScanRows = e
End If
If ShowWhat = "MAX" Then
ScanRows = r
End If
End Function
Public Function FindInArea(SheetName As String, startRow As String, endRow As String, startCol As String, endCol As String, FindThis As String, ShowWhat As String)
CalendarMonthFormat1 = "Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec,"
CalendarMonthFormat2 = "January, Feburary,March,April,May,June,July,August,September,October,November,December,"
earliestDate = 999999999
latestDate = 0
If Left(FindThis, 7) = "[LENGTH" Then
LengthLook = Replace(FindThis, "[LENGTH", "")
LengthLook = Replace(LengthLook, "]", "")
End If
For a = startRow To endRow
For b = startCol To endCol
ThisCell = Sheets(SheetName).Cells(a, b)
thisCellAddr = Sheets(SheetName).Cells(a, b).Address
If ThisCell = FindThis Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
End If
If FindThis = "[MONTHS1]" Then
If ThisCell <> "" And InStr(LCase(CalendarMonthFormat1), LCase((ThisCell) & ",")) > 0 And Len(ThisCell) = 3 Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
End If
End If
If FindThis = "[MONTHS2]" Then
If ThisCell <> "" And InStr(LCase(CalendarMonthFormat2), LCase((ThisCell) & ",")) > 0 Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
End If
End If
If FindThis = "[DATEFORMAT]" Then
If InStr(ThisCell, "/") > 0 Then
slash_count = 0
For sc = 1 To Len(ThisCell)
If Mid(ThisCell, sc, 1) = "/" Then
slash_count = slash_count + 1
End If
Next sc
If slash_count = 2 Then
On Error Resume Next
D = Day(ThisCell)
M = Month(ThisCell)
Y = Year(ThisCell)
If D > 0 And M > 0 And Y > 0 Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
If earliestDate > DateValue(ThisCell) Then
earliestDate = DateValue(ThisCell)
If Len(D) = 1 Then
D = "0" & D
End If
If Len(M) = 1 Then
M = "0" & M
End If
eDateLocation = thisCellAddr
eDate_Format = D & "-" & M & "-" & Y
End If
If latestDate < DateValue(ThisCell) Then
latestDate = DateValue(ThisCell)
If Len(D) = 1 Then
D = "0" & D
End If
If Len(M) = 1 Then
M = "0" & M
End If
lDateLocation = thisCellAddr
lDate_Format = D & "-" & M & "-" & Y
End If
End If
End If
End If
End If
If Left(FindThis, 7) = "[LENGTH" Then
If Len(ThisCell) = Val(LengthLook) Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
End If
End If
If FindThis = "[DECIMAL]" Then
If InStr((ThisCell), ".") > 0 Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
End If
End If
If FindThis = "[PERC]" Then
If InStr((ThisCell), ".") > 0 And ThisCell <> "" And ThisCell <> 0 And Val(ThisCell) >= -1 And Val(ThisCell) <= 1 Then
addrList = addrList & "[" & thisCellAddr & "]"
rc_list = rc_list & "[" & a & "," & b & "]"
c = c + 1
End If
End If
Next b
Next a
If ShowWhat = "COUNT" Then
FindInArea = c
End If
If ShowWhat = "ADDR" Then
FindInArea = addrList
End If
If ShowWhat = "RC" Then
FindInArea = rc_list
End If
If ShowWhat = "EARLIESTDATE" Then
FindInArea = eDate_Format
End If
If ShowWhat = "EARLIESTDATEADDR" Then
FindInArea = eDateLocation
End If
If ShowWhat = "LATESTDATE" Then
FindInArea = lDate_Format
End If
If ShowWhat = "LATESTDATEADDR" Then
FindInArea = lDateLocation
End If
End Function

Resources