I would like to build a small web service that gets a URL as input, such as:
www.example.com/timestamp/
and concatenates the Unix Timestamp to it:
www.example.com/timestamp/15090908240.html
That's my first even attempt to do something like that, so I don't even know if I tag this properly.
Any advice?
I'm not sure what you want to achieve here but
if I had to do that, i will probably do something like:
get('/timestamp', function() {
Redirect( 'http://example.com/timestamp'.time() );
}
Assuming that you are only dealing with strings, you can just concatenate your strings together.
url = url + timestamp + ".html";
make sure to parse your timestamp as string from long, I'm assuming here that the timestamp you are using is UNIX time, aka Epoch time.
Related
I want to pass into a variable, the language of the user.
But, my client can't/didn't pass this information trough datalayer. So, the unique solution I've is to use the URL Path.
Indeed - The structure is:
http://www.website.be/en/subcategory/subsubcategory
I want to extract "en" information
No idea to get this - I check on Stack, on google, some people talk about regex, other ones about CustomJS, but, no result on my specific setup.
Do you have an idea how to proceed on this point ?
Many thanks !!
Ludo
Make sure the built in {{Page Path}} variable is enabled. Create a custom Javascript variable.
function() {
var parts = {{Page Path}}.split("/");
return parts[1];
}
This splits the path by the path delimiter "/" and gives you an array with the parts. Since the page path has a leading slash (I think), the first part is empty, so you return the second one (since array indexing starts with 0 the second array element has the index 1).
This might need a bit of refinement (for pages that do not start with a language signifier, if any), but that's the basic idea.
Regex is an alternative (via the regex table variable), but the above solution is a little easier to implement.
Recently I've been trying to get data from our Bigtable tables using python. I'm able to connect and authenticate with the api, and I can get some sample data, but when I go to add a simple rowkey regex filter I get an empty data set, even though I know there should be data there.
All the rowkeys have a format like this:
XY_1234567_Z
where X and Y are capital letters A-Z and Z is a number 0-9. The _1234567_ is the constant that I provide. So basically I need to get all rows where rowkey is everything that contains _1234567_ for example.
This is the regex I use:
^.._1234567_.$
And this an example of my current code:
...
tbl = instance.table(tableID)
regex = ("^.._" + str(rowID) + "_.$").encode()
fltr = RowKeyRegexFilter(regex)
row_data = tbl.read_rows(filter_=fltr)
print(row_data.rows)
row_data.rows always ends up being an empty dict. I've tried removing encode() and just sending a string, and I've also tried a different regex to be more specific like this "([A-Z][A-Z])_" + str(rowID) + "_([0-9])" which still didn't work. If I try to do row_data.consume_next(), it hangs for a while and eventually gives me a StopIteration error. I've also tested the regex with regex101 and that seems to be fine, so I'm not sure where the issue is.
Looks like you've already figured it out, but please see the documentation for the python Data API [1, 2]. Calling row_data.consume_next() will fetch the next ReadRowsResponse in the stream and store it in row_data.rows. consume_next() will raise a StopIteration exception when there are no more results to consume. Alternatively, you can call row_data.consume_all() to consume all of the results from the stream (up to an optional limit).
[1] https://googleapis.dev/python/bigtable/latest/data-api.html?highlight=consume_next#stream-many-rows-from-a-table
[2] https://gcloud-python-bigtable.readthedocs.io/en/data-api-complete/row-data.html#gcloud_bigtable.row_data.PartialRowsData
It seems all I needed to do was row_data.consume_next() to get the set of data I requested. Stupid mistake. The row_data object is initially an empty dict but is populated once consume_next() reads the next item in the stream. This brought up a new issue where consume_next() would hang if read_rows() didn't find any match, but this is for another question.
I have this link www.xxx.yy/yyy/zzzzzz/xyz-z-yzy-/93797038 and I want to take the number 93797038 in order to pass it into another link.
For example: I want afterwards something like www.m.xxx.yy/93797038 which is the same page as before but in its mobile version.
In general, I know that I have to type www.xxx.yy/(.*) for extracting anything following the in the main url and then I group the result with www.m.xxx.yy/%1 which redirects to the same page but in the mobile version.
Any ideas how to do it?
EDIT: The link www.xxx.yy/yyy/zzzzzz/xyz-z-yzy-/93797038 is automated. The part that is the same each time is only the www.xxx.yy . Every time the system runs produces different urls. I want each time to take the number from those urls, e.g. the 93797038 in my case.
\/(\d+?)$ will get the trailing digits after the final /.
Why you want regex? You can use
string str = #"www.xxx.yy/yyy/zzzzzz/xyz-z-yzy-/93797038";
string digit = str.Split('/').Last();
instead.
It's a bit late, but I'm disappointed in myself for not coming up with something more elegant. Anyone have a better way to do this...
When you pass an OAuth code to Facebook, it response with a query string containing access_token and expires values.
access_token=121843224510409|2.V_ei_d_rbJt5iS9Jfjk8_A__.3600.1273741200-569255561|TxQrqFKhiXm40VXVE1OBUtZc3Ks.&expires=4554
Although if you request permission for offline access, there's no expires and the string looks like this:
access_token=121843224510409|2.V_ei_d_rbJt5iS9Jfjk8_A__.3600.1273741200-569255561|TxQrqFKhiXm40VXVE1OBUtZc3Ks.
I attempted to write a regex that would suffice for either condition. No dice. So I ended up with some really ugly Ruby:
s = s.split("=")
#oauth = {}
if s.length == 3
#oauth[:access_token] = s[1][0, s[1].length - 8]
#oauth[:expires] = s[2]
else
#oauth[:access_token] = s[1]
end
I know there must be a better way!
Split on the & symbol first, and then split each of the results on =? That's the method that can cope with the order changing, since it parses each key-value pair individually.
Alternatively, a regex that should work would be...
/access_token=(.*?)(?:&expires=(.*))/
If the format is strict, then you use this regex:
access_token=([^&]+)(?:&expires=(.*))?
Then access_token value is in \1, and expires, if there's any, will be in \2.
Query string parsing usually involves these steps:
If it is a complete URL, take the part after the first ?
split the rest on &
for each of the resulting name=value pairs, split them on =
URL-decode to the value and the name (!)
stuff the result into a data structure of your liking
Using regex is possible, but makes invalid assumptions about the state of URL-encoding the string is in and fails if the order of the query string changes, which is perfectly allowed and therefore cannot be ruled out. Better to encapsulate the above in a little parsing function or use one of the existing URL-handling libraries of your platform.
Usually I use streams for formatting stuff however in this case ?I don't know the format until runtime.
I want to be able to take something like the following format string:
Hello {0}! Your last login was on {1,date:dd/mm/yy}.
...and feed in the variables "Fire Lancer" and 1247859223, and end up with the following formatted string:
Hello Fire Lancer! Your last login was on 17/07/09.
In other languages I use there is built in support for this kind of thing, eg pythons format string method, however in c++ there doesn't seem to be any such functionality, accept the C print methods which are not very safe.
Also this is for a high performance program, so whatever solution I use needs to parse the format string once and store it (eg mayby a Parse method that returns a FormatString object with a Format(string) method), not reparse the string every time the format method is called...
Your format string looks very much like those used in ICU MessageFormat. Did you consider using it?
Boost Formatting does that for you:
http://www.boost.org/doc/libs/1_39_0/libs/format/doc/format.html
Check out this question and answer for examples of usage:
boost::format will do the positional arguments portion, but not the date formatting...