Watir Scroll For All Comments - watir

I'm trying to scroll until there are no more contents on a Youtube video. I'm testing a counter versus the current number of comments displayed.
mycount = 0
1.upto(20) do
thiscount = browser.div(id: "contents").divs(id: "comment-content").size
puts "#{ mycount } : #{ thiscount }"
break if mycount == thiscount
mycount = thiscount
browser.driver.execute_script("window.scrollBy(0,1000)")
sleep 10
end
After the first pagedown, the count of comments should increase. It isn't. I've entered a sleep 10 for the comments to load and the count of those comments to also update. It doesn't update. I keep getting 20 : 20 so it breaks and leaves this iteration after a single iteration.
I'm not sure why that valuation isn't updating. How can I fix this so that it can get to the end of the comments?

I ran into 2 problems when running your script:
browser.div(id: "contents").divs(id: "comment-content") returned nothing. There were many "contents" divs, with the first one not including any comments. I removed this locator.
Scrolling by 1000 is not enough to get to the bottom of the page. Therefore the loading of additional comments does not get triggered.
From the following script:
scroll_by = 1000000000000
browser.goto('https://www.youtube.com/watch?v=u9DPBxiZZfo&ab_channel=America%27sTestKitchen')
browser.driver.execute_script("window.scrollBy(0,#{scroll_by})")
sleep 15
mycount = 0
1.upto(20) do
thiscount = browser.divs(id: "comment-content").size
puts "#{ mycount } : #{ thiscount }"
break if mycount == thiscount
mycount = thiscount
browser.driver.execute_script("window.scrollBy(0,#{scroll_by})")
sleep 10
end
You can see that having a larger scroll gives the expected results:
0 : 20
20 : 40
40 : 60
60 : 80
80 : 100
100 : 120
120 : 140
140 : 160
160 : 180
180 : 185
185 : 185
In contrast, setting the scroll_by to just "1000" did not trigger the comments. The output was just:
0 : 0

This works for me:
total = 0
while true
before = browser.divs(id: "comment-content").count
browser.div(id: "columns").scroll.to :bottom
sleep 10 # or sleep 5
total = browser.divs(id: "comment-content").count
puts "There are #{total} comments for now"
break if before == total
end
puts "There are #{total} comments in total"
instead while true you can use 20.times do but that would only count up to ~400 comments.

Related

Extract the onset of pulses in praat

I am currently wirting a script to calculte VOT of consonants in English
Tier 3 : phoneme
Tier 4 : CV
The goal is to detect in the interval of Tier 4 the onset of the pulses. Here is a part of the code :
thisSound$ = selected$("Sound")
thisTextGrid$ = selected$("TextGrid")
select TextGrid 'thisTextGrid$'
numberOfCV = Get number of intervals: 4
appendInfoLine: "There are ", numberOfCV, " intervals."
for i from 1 to numberOfCV
appendInfoLine: i
select TextGrid 'thisTextGrid$'
thisCV$ = Get label of interval: 4, i
appendInfoLine: thisCV$
When I try the command "pulse listing" it doesn't work...
Can someone help me with that ?
Many thanks !

Convert user input to time which changes boolean value for the duration entered?

I'm working on this side project game to grasp python better. I'm trying to have the user enter the amount of time the character has to spend busy, then not allow the user to do the same thing until they have completed the original time entered. I have tried a few methods with varying error results from my noob ways. (timestamps, converting input to int and time in different spots, timeDelta)
def Gold_mining():
while P.notMining:
print('Welcome to the Crystal mines kid.\nYou will be paid in gold for your labour,\nif lucky you may get some skill points or bonus finds...\nGoodluck in there.')
print('How long do you wish to enter for?')
time_mining = int(input("10 Gold Per hour. Max 8 hours --> "))
if time_mining > 0 and time_mining <= 8:
time_started = current_time
print(f'You will spend {time_mining} hours digging in the mines.')
P.gold += time_mining * 10
print(P.gold)
P.notMining = False
End_Time = (current_time + timedelta(hours = 2))
print(f'{End_Time} time you exit the mines...')
elif time_mining > 8:
print("You can't possibly mine for that long kid, go back and think about it.")
else:
print('Invalid')
After the set amount of time i would like for it to change the bool value back to false so that you can mine again.
"Crystal Mining" is mapped to a different key for testing so my output says "Inventory" but would say "Crystal Mining" when it works properly and currently looks like this:
*** Page One ***
Intro Page
02:15:05
1 Character Stats
2 Rename Character
3 Inventory
4 Change Element
5 Menu
6 Exit
Num: 3
Welcome to the Crystal mines kid.
You will be paid in gold for your labour,
if lucky you may get some skill points or bonus finds...
Goodluck in there.
How long do you wish to enter for?
10 Gold Per hour. Max 8 hours --> 1
You will spend 1 hours digging in the mines.
60
Traceback (most recent call last):
File "H:\Python ideas\input_as_always.py", line 176, in <module>
intro.pageInput()
File "H:\Python ideas\input_as_always.py", line 45, in pageInput
self.pageOptions[pInput]['entry']()
File "H:\Python ideas\input_as_always.py", line 134, in Gold_mining
End_Time = (current_time + timedelta(hours = 2))
TypeError: can only concatenate str (not "datetime.timedelta") to str

Printing list in different columns

I am quite new to Python and I am now struggling with printing my list in columns. It prints my lists in one columns only but I want it printed under 4 different titles. I know am missing something but can't seem to figure it out. Any advice would be really appreciated!
def createMyList():
myAgegroup = ['20 - 39','40 - 59','60 - 79']
mygroupTitle = ['Age','Underweight','Healthy','Overweight',]
myStatistics = [['Less than 21%','21 - 33','Greater than 33%',],['Less than 23%','23 - 35','Greater than 35%',],['Less than 25%','25 - 38','Greater than 38%',]]
printmyLists(myAgegroup,mygroupTitle,myStatistics)
return
def printmyLists(myAgegroup,mygroupTitle,myStatistics):
print(': Age : Underweight : Healthy : Overweight :')
for count in range(0, len(myAgegroup)):
print(myAgegroup[count])
for count in range(0, len(mygroupTitle)):
print(mygroupTitle[count])
for count in range(0, len(myStatistics)):
print(myStatistics[0][count])
return
createMyList()
To print data in nice columns is nice to know Format Specification Mini-Languag (doc). Also, to group data together, look at zip() builtin function (doc).
Example:
def createMyList():
myAgegroup = ['20 - 39','40 - 59','60 - 79']
mygroupTitle = ['Age', 'Underweight','Healthy','Overweight',]
myStatistics = [['Less than 21%','21 - 33','Greater than 33%',],['Less than 23%','23 - 35','Greater than 35%',],['Less than 25%','25 - 38','Greater than 38%',]]
printmyLists(myAgegroup,mygroupTitle,myStatistics)
def printmyLists(myAgegroup,mygroupTitle,myStatistics):
# print the header:
for title in mygroupTitle:
print('{:^20}'.format(title), end='')
print()
# print the columns:
for age, stats in zip(myAgegroup, myStatistics):
print('{:^20}'.format(age), end='')
for stat in stats:
print('{:^20}'.format(stat), end='')
print()
createMyList()
Prints:
Age Underweight Healthy Overweight
20 - 39 Less than 21% 21 - 33 Greater than 33%
40 - 59 Less than 23% 23 - 35 Greater than 35%
60 - 79 Less than 25% 25 - 38 Greater than 38%

Redrawing gl2 isigraphs using event handler

I've been playing around with the table & gl2 demos, and have come across a "domain error: chkgl2" multiple times.
What I'm trying to do is to modify an isigraph after its initialized on program run, by selecting / un-selecting a checkbox.
I have the following code:
demo_run=: 3 : 0
require 'gl2'
coinsert 'jgl2'
wd 'pc test closeok escclose'
wd 'pn "Tester"'
wd 'cc gs isigraph'
wd 'cc cb checkbox; cn "Test GUI"'
wd 'set cb value 0'
wd 'pmove 400 10 400 200'
wd 'pshow'
)
demo_cb_button=: 3 : 0
demo_gs_paint''
)
demo_gs_paint=: 3 : 0
'w h'=: glqwh''
glfill 0 0 0 255
glrgb 255 223 0
glpen 2 1
if. cb do.
for_i. 250*i. >. h%250 do.
gllines 0,i,w,i
end.
end.
)
demo_run''
What I'm expecting to happen is the checkbox to update the isigraph. What I'm getting is a "domain error: chkgl2" on line "glfill 0 0 0 255".
Any help would be appreciated!
A couple of things that I spotted:
1) you are calling your parent 'test' but you are giving the verbs a 'demo' prefix. You can do this for the verb demo_run, but if you want the children of the form to react you will need to refer to them in the form of test_cb_button
2) cb in your test is actually the character '1' and not the integer 1, so in order for it to work as a conditional you need to change it to an integer type. I used Do (".) to make this change (". cb)
3) I am not sure that your code 250*i. >. h%250 is doing what you expect, so I replaced it with i. >.h%15 and then changed the gllines command to gllines 0,i,w,i*15 for a more interesting effect.
4) You are using isigraph which automatically triggers the painting of the screen and I wonder if the fact that you are clicking a button which triggers drawing of a screen to change the button image while also explicitly requiring the painting of the screen using the test_gs_paint verb is causing the glfill to be run twice. The second time it may be in a context that creates a domain error. To be honest, I don't know. What I do know is that when I use isidraw instead and trigger my own paint command using glpaint '' (which isidraw requires) that things seem to work.
5) I have added a second version that works with isigraph and it looks as if the issue is that by calling the paint verb directly with test_gs_paint that you were indeed calling it twice. By changing the action to glpaint '', the form does rerun the painting of the entire form once and this causes test_gs_paint to update as well. This time though it does it in a more coordinated fashion. My second version reflects the change. Other corrections I have left in place.
My isidraw version of the code follows:
demo_run=: 3 : 0
require 'gl2'
coinsert 'jgl2'
wd 'pc test closeok escclose'
wd 'pn "Tester"'
wd 'cc gs isidraw' NB. changed from isigraph
wd 'cc cb checkbox; cn "Test GUI"'
wd 'set cb value 0'
wd 'pmove 400 10 400 200'
wd 'pshow'
)
test_cb_button=: 3 : 0
test_gs_paint''
)
test_gs_paint=: 3 : 0
'w h'=: glqwh''
glfill 0 0 0 255
glrgb 255 223 0
glpen 2 1
if. (".cb) do. NB. needed to test on an integer and not a character value
for_i. i. >.h%15 do. NB. changed to give a more noticeable effect
gllines 0,i,w,i*15
end.
end.
glpaint'' NB. added to accommodate isidraw requirement of explicit painting
)
demo_run''
My isigraph version of the form:
demo_run=: 3 : 0
require 'gl2'
coinsert 'jgl2'
wd 'pc test closeok escclose'
wd 'pn "Tester"'
wd 'cc gs isigraph'
wd 'cc cb checkbox; cn "Test GUI"'
wd 'set cb value 0'
wd 'pmove 400 10 400 200'
wd 'pshow'
)
test_cb_button=: 3 : 0
glpaint '' NB. changed from test_gs_paint ''
)
test_gs_paint=: 3 : 0
'w h'=: glqwh''
glfill 0 0 0 255
glrgb 255 223 0
glpen 2 1
if. (".cb) do.
for_i. i. >.h%15 do.
gllines 0,i,w,i*15
end.
end.
)
demo_run''

Tweepy cursor .pages() with api.search_users returning same page again and again

auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
user_objs = []
name = "phungsuk wangdu"
id_strs = {}
page_no = 0
try:
for page in tweepy.Cursor(api.search_users, name).pages(3):
dup_count = 0
print("******* Page", str(page_no))
print("Length of page", len(page))
user_objs.extend(page)
for user_obj in page:
id_str = user_obj._json['id_str']
if id_str in id_strs:
# print("Duplicate for:", id_str, "from page number:", id_strs[id_str])
dup_count += 1
else:
# print(id_str)
id_strs[id_str] = page_no
time.sleep(1)
print("Duplicates in page", str(page_no), str(dup_count))
page_no += 1
except Exception as ex:
print(ex)
With the above code, I am trying to get the search results for users using tweepy(Python 3.5.2, tweepy 3.5.0) cursor. The results are being duplicated with the pages parameter being passed. Is it the right way to query the search_users using the tweepy cursor? I am getting results for the above code with the following pattern:
1. for low search results(name = "phungsuk wangdu") (There are actually 9 results returned for manual search on twitter website):
******* Page 0
Length of page 2
Duplicates in page 0 0
******* Page 1
Length of page 2
Duplicates in page 1 2
******* Page 2
Length of page 2
Duplicates in page 2 2
******* Page 3
Length of page 2
Duplicates in page 3 2
2. for high search results (name = "jon snow")
******* Page 0
Length of page 20
Duplicates in page 0 0
******* Page 1
Length of page 20
Duplicates in page 1 20
******* Page 2
Length of page 20
Duplicates in page 2 0
******* Page 3
Length of page 20
Duplicates in page 3 0
Try adding this attribute to the Cursor; it should reduce the duplicates.
q= <your query> +" -filter:retweets"
There are two issues here.
Tweepy's pageiterator for cursor starts pagenumber from 0 while python's page number starts from 1.
Python returns results from the last available page for page numbers that are greater than available results.
I made a pull request to tweepy with both the fixes.

Resources