HTTP_USER_AGENT script - django

Can someone explain this script?
device = {}
ua = request.META.get('HTTP_USER_AGENT', '').lower()
if ua.find("iphone") > 0:
device['iphone'] = "iphone" + re.search("iphone os (\d)", ua).groups(0)[0]
I know it gets the user agent and the first bit searches the string for iphone. but
why > 0?
and what is the second like doing?

I am not a python guy but I am almost sure that .find() returns position where the string "iphone" occurs.
So if it was found, it will be >= 0.

Related

Why does Selenium inserts a backslash before the Hyphen character?

The following Code fails in IE and Firefox. Never had a problem with Chrome.
foundElement = driver.FindElement(By.Id("btn-GDR"));
It says couldn't find the element #btn\-GDR
Why is Selenium inserting a \ before the -?
Firefox 65.0.2 Version
IE 11.0.9600.19301
EDIT: More Info: I've tried using
"btn\x2dGDR" meaning \x2d is the "-" symbol (ASCII in HEX) but it does not solve the problem. It always insert a "\" before it.
As Selenium converts the different Locator Strategies into it's effective CSS selectors as per the switch - cases the values of class name, id, name, tag name, etc are converted through:
cssEscape(value);
The cssEscape(value) is defined as:
private String cssEscape(String using) {
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&#`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
using = "\\" + Integer.toString(30 + Integer.parseInt(using.substring(0,1))) + " " + using.substring(1);
}
return using;
}
Hence you see the - character being escaped by the \ character.
I will answer my own question since I've found the solution.
I added a wait before finding the element.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.
PresenceOfAllElementsLocatedBy(By.Id("btn-GDR")));
Turns out that sometimes the element is not present for some strange reason.. I can see it on screen but it takes 2-3 secs for Selenium to properly being able to interact with it. Yes, the element is always visible, enabled and it does exits. Also, when reporting the options Selenium reports adds the backslash before the hyphen to the output message.
FYI I've found the same case here. It was unanswered.
Similar Problem

implicitly_wait VS. time_sleep() in Selenium 3.0.2 Python 2.7

I'm trying to implement a simple sub dowloader with Selenium on Python 2.7 going on the http://www.yifysubtitles.com website.
I have run into serious troubles with implementing waits either implicit or explicit in Selenium - surely caused by ignorance rather than code.
I eventually opted for the destroy-all time.sleep() solution. The program works but I would like a more programatic solution and a prettier code.
Here is the code with the both implicit and explicit waits:
user_input = raw_input("Enter the movie of interest: ")
profile = SetProfile()
browser = webdriver.Firefox(profile)
# browser.implicitly_wait(30)
WindowSize(400, 400)
browser.get(url)
print "Searching..."
search_bar = browser.find_element_by_id("qSearch")
click_button = browser.find_element_by_class_name("glyphicon")
#wait_1 = WebDriverWait(browser, 50)
#cookie_element = wait_1.until(EC.element_to_be_clickable((By.CLASS_NAME,"cc_btn.cc_btn_accept_all")))
time.sleep(5)
accept_cookies_btn = browser.find_element_by_class_name("cc_btn.cc_btn_accept_all")
search_bar.clear()
accept_cookies_btn.click()
search_bar.send_keys(user_input)
search_bar.send_keys(Keys.RETURN)
time.sleep(10)
#wait_2 = WebDriverWait(browser, 50)
#result_element = wait_2.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.media-heading")))
result = browser.find_element_by_class_name("media-heading")
result.click()
print "Movie found!"
time.sleep(10)
req = requests.get(browser.current_url)
soup = BeautifulSoup(req.text)
link_to_sub = ResultParser(soup)
print "Choosing the best eng. subtitle..."
browser.get(url+link_to_sub[1])
download_button = browser.find_element_by_class_name("btn-icon")
download_button.click()
time.sleep(10)
browser.quit()
print "Enjoy!"
I have commented out the waits (explicit or implicit) I tried to use. Indeed, If I use waits instead time.sleep, Selenium will throw at me an ElementNotVisibleException.
Thus, am I doing something wrong with the waits?
Regarding the implicit waits, Selenium always throws at me ElementNotVisibleException: Message: (With message empty) when I run the program, whatever the wait length (tried 5, 30 and 100!).
When I use explicit waits, similarly, Selenium either throw ElementNotVisibleException or seems to use the DOM from the preceding page. Indeed, in the last case the wait is after clicking on the result, but I get the heading of the previous page...
As I said, when I use time.sleep(), Selenium finds the elements without any trouble.
A long message for a simple question: why my waits seem to not work?

Custom gradle plugin on TeamCity different behavior then local

I'm facing a thing that is confusing me. I have this groovy code:
def static extractTaskIdsFromLine(String orderName, String line) {
print("comment line = \"" + line + "\"")
def pattern = ~/${orderName}-[0-9]+/
return line != null ? line.findAll(pattern) : new ArrayList<>()
}
On my local machine everything looks fine and all tests pass. But with the same conditions this code on Team City with the same input gives no one matches. Both encodings and input parameters are the same.
Do you have any ideas?

How to trace a users path on a map?

I am developing an iOS app using Rubymotion.
In this app I want to be able to show the user on a map exactly which way he or she have driven or walked. I also want to save this path on the server.
How can I trace this patch in iOS?
All input and hints about a solution is welcome.
i guess your app needs a timer:
count = 0
timer = EM.add_periodic_timer 1.0 do
count = count + 1
puts "Great scott!"
(count < 10) || EM.cancel_timer(timer)
end
and then find the current location:
BW::Location.get do |result|
p "From Lat #{result[:from].latitude}, Long #{result[:from].longitude}"
p "To Lat #{result[:to].latitude}, Long #{result[:to].longitude}"
end
all this is BubbleWrap: https://github.com/rubymotion/BubbleWrap

Resolving incidents (closing cases) in CRM4 through webservices?

I'm trying to resolve/close Dynamics CRM4 cases/incidents through webservices.
A single SetStateIncidentRequest is not enough and returns a Server was unable to process request error message. I think it has something to do with active workflows that trigger on case's attribute changes. I don't know if there's anything else preventing the request to work.
Since it is possible to close those cases through the GUI, I guess there's a "correct" set of steps to follow in order to achieve it through CrmService; unfortunately, I've been googleing it for a while without finding what I want. Could anybody help me, please?
To resolve a case in CRM (in VB.NET), I do the following:
Try
Dim activity As New incidentresolution
Dim closeRequest As New CloseIncidentRequest
Dim closeResponse As New CloseIncidentResponse
Dim strErrors As String = String.Empty()
activity.incidentid = New Lookup
activity.incidentid.type = EntityName.incident.ToString
activity.incidentid.Value = //[GUID OF INCIDENT]
activity.ownerid = New Owner
activity.ownerid.type = EntityName.systemuser.ToString
activity.ownerid.Value = //[GUID OF USER PERFORMING ACTION]
activity.statecode = New IncidentResolutionStateInfo
activity.statecode.Value = 1 //Resolved
activity.statuscode = New Status
activity.statuscode.Value = 5 //Problem Solved
closeRequest.IncidentResolution = activity
closeRequest.Status = 5 //Problem Solved
// IF REQUIRED:
activity.timespent = New CrmNumber
activity.timespent.Value = //[INTEGER REPRESENTING No. OF MIN SPENT ON CASE]
closeResponse = objCrm.Execute(closeRequest)
Catch ex As System.Web.Services.Protocols.SoapException
Dim root As XmlElement = ex.Detail
strErrors = strErrors & vbCrLf & vbCrLf & root.ChildNodes(0).ChildNodes(3).InnerText
Return False
End Try
Here's a tip - catch the SoapException and examine the Detail.OuterXML property and you will get a more detailed error message. It's possible you're not building your request correctly.
Indeed, I didn't know that there exists a CloseIncidentRequest class to use with the CrmService.Execute() method. Most probably the SetStateIncidentRequeset won't work because it's expected that incident resolutions are created that way. Pity that names for classes and actions aren't used consistently (case/incident, resolution/closing)...