How do I use the link for a Foursquare photo? - foursquare

I am trying to use the foursqaure API via the HTTP requests.
So I send a http request looking similar to this ->
https://api.foursquare.com/v2/venues/explore?ll=40.7,-74&venuePhoto=1&oauth_token=(OAUTH_TOKEN)&v=20151015
I get one massive JSON object with a bunch of data and inside the data I find the photos Prefix and suffix.
My problem is when I try and use the &venuePhoto=1.
I get a prefix and suffix for what seems to be 1 picture in the following form.
prefix: "https://irs1.4sqi.net/img/general/"
suffix: "/63892328_N0A_9qgk0N30lHiqDWIRtNb-0YfO84BIxHc9SP7h4rY.jpg"
I can not seem to get this string to get me the picture.
If I put them together even without one of the "/" slashes it still does not give me a picture.I have already tried a whole bunch of things.
I thought adding this option to the request URL I can void using the photo part of the api and get everything in 1 go.
AM I missing something? Should I be using some other part of the API to get the pictures.?

You need to specify the size of image.
prefix + "100x100" + suffix
e.g. https://irs1.4sqi.net/img/general/100x100/63892328_N0A_9qgk0N30lHiqDWIRtNb-0YfO84BIxHc9SP7h4rY.jpg
See also Foursquare API document
To assemble a resolvable photo URL, take prefix + size + suffix, e.g. https://irs0.4sqi.net/img/general/300x500/2341723_vt1Kr-SfmRmdge-M7b4KNgX2_PHElyVbYL65pMnxEQw.jpg.
size can be one of the following, where XX or YY is one of 36, 100, 300, or 500.
XXxYY
original: the original photo's size
capXX: cap the photo with a width or height of XX (whichever is larger). Scales the other, - smaller dimension proportionally
widthXX: forces the width to be XX and scales the height proportionally
heightYY: forces the height to be YY and scales the width proportionally

Related

Encoding and decoding a unique ID between two systems in nodejs

I am writing a script which synchronizes users between two systems. Lets say the source and target system.
To help with synchronising them I was hoping to store the user ID from the source system as an ID in the target system.
Unfortunately the target system has a max character length for the property I can store this in.
If possible, i would like to avoid creating a new table to persist the relationship.
I can't truncate it as I need to be able to refer back to the user in the source system from the target system.
Is there a way of encoding and decoding the source User ID?
Edit:
The ID in the source system will always follow the structure of:
/^[a-zA-Z0-9]{6}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{3}-[a-zA-Z0-9]{6}$/
For example: as0092-banc-mdn-da1023
There are no restrictions on what can be stored in the target system apart from it having a max character length of 20.
When you originally posted your question, it seemed like you needed a massive conversion, ie squishing a 20 character long ID into something much shorter, that would have been quite a problem. But now after your edit it seems quite easy:
You have 20 characters available in the target system, but the ID in the source system only has 19 variable characters plus 3 dashes on fixed positions.
So when converting from source to target just remove the dashes. This will give you a string of length 19, which will perfectly fit into your 20 character wide field on the target system. And as the dashes are at fixed positions, they don't carry any semantics, ie removing them won't tamper with uniqueness of your ID.
And when converting from target back to source, you know where in your ID the dashes have to go, ie after the 6th, the 10th and the 13th character of your squished ID. So just put them back in there at the right places, and you have your ID format of the source system back ...
let
original = 'as0092-banc-mdn-da1023', //this is the key from your example
target = original.replace(/-/g, ""), //this removes all dashes
source = target.slice(0, 6) + "-" //this takes certain portions of your squished ID
+ target.slice(6, 10) + "-" //and inserts dashes between them
+ target.slice(10, 13) + "-"
+ target.slice(13)
console.log("original ", original, " length", original.length);
//the mapped ID fits perfectly into your target system's length restriction
console.log("target ", target, " length", target.length);
//the reversed mapping is equal to your original ID
console.log("source ", source, " length", source.length);
console.log("test eq ", source === original);
Of course there are other possibilities, to remove and readd the dashes, but I think you get the idea behind the code.

Long sentence shortener to generate a readable file name

I'm looking for a way to shorten a sentence (a text of few lines) to produce a "readable" (not too long) file name.
The application scenario is a chatbot where user can submit a media, say a video, with some paired description text (a caption). The application would assign to the video a readable file name, to retrieve afterward the video by his file name.
Imagine a video paired with a more or less long text description of the scene, like by example:
const videoDescription = 'beautiful yellow flowers on foreground, with a background with countryside meadows and many cows'
How could I shorten the description above with a "suitable" short file name?
Ok, I could just give the sentence as a name, maybe something a bit sanitized, like:
const videoFileName = 'beautiful_yellow_flowers_on_foreground_with_a_background_with_countryside_meadows_and_many_cows.MP4'
but in that way I could exceed the 255 limit of file name size (e.g. on Linux)
Any idea for a shortener algo?
Maybe I could build the shortened filename with word abbreviations?
Maybe I could remove from sentence articles, prepositions, etc.?
BTW, a minor issue: I'm working with Italian language, so a bit of chars sanitize is required to produce good filenames.
Last but not least, I'd looking for JavaScript/Node.js code
You can check if the length is larger than 255 and shorten if necessary. You should also check for duplicates and append -1, -2 and so on if necessary.
let filename='some_flowers_on_foreground_with_a_background_with_countryside_meadows_and_few_cows.MP4'
if(filename.length>255)
filename=filename.slice(0,255-4)+'.MP4'

JavaFX proximity detection not working

I have been trying to figure out how to get two nodes to sense when they are close to each other and then snap together but can't make it work correctly. Basically, I have an AnchorPane that I am dropping new Nodes onto. The new nodes are also anchor panes with several other components on them. When I drop the Node I save anchor points along the outer edge. Then, when I drag another Node next to it, the sides will light up indicating the other node is in range.
I am attempting to make a node that is being dragged next to another node snap to that node. I cannot seem to get the coordinates to translate correctly between each other and I am just ending up with random placement and edge detection.
Here is my code where I am saving the anchor points for the nodes:
double kromaDeviceWidth = kromaDevice.getBoundsInParent().getWidth();
double kromaDeviceHeight = kromaDevice.getBoundsInParent().getHeight();
//This x,y represents the top left corner of the node
double kromaDeviceX = kromaDevice.localToParent(0.0, 0.0).getX();
double kromaDeviceY = kromaDevice.localToParent(0.0, 0.0).getY();
kromaDevice.setTopAnchorPoint(new double[]{kromaDeviceX + kromaDeviceWidth / 2, kromaDeviceY});
kromaDevice.setRightAnchorPoint(new double[]{kromaDeviceX + kromaDeviceWidth, kromaDeviceY + kromaDeviceHeight / 2});
kromaDevice.setBottomAnchorPoint(new double[]{kromaDeviceX + kromaDeviceWidth / 2, kromaDeviceY + kromaDeviceHeight});
kromaDevice.setLeftAnchorPoint(new double[]{kromaDeviceX, kromaDeviceY + kromaDeviceHeight / 2});
The code is identical for when I initially drop a new node and when I am dragging the node. Then, I compare the two node's anchor positions to tell if they are within range:
if (Math.abs(bottomAnchorX - topAnchorPointX) <= ANCHOR_DISTANCE && Math.abs(bottomAnchorY - topAnchorY) <= ANCHOR_DISTANCE) {
....show correct edge highlight
}
I simplified the above if statement as I am using arrays to store and recall the anchor points.
Here is an image of what I am seeing:
You can see the slight yellow highlight when I drag one node over the other when it is offset. It should detect the other node when it is in the position in the second image. My next issue is trying to get them to snap to the right coordinates.
droppedKromaDevice.setLayoutX(parentKromaDevice.getLayoutX());
droppedKromaDevice.setLayoutY(parentKromaDevice.getLayoutY() - droppedKromaDevice.getBoundsInParent().getHeight());
I tried the above with both getLayoutX() and localToParent(0,0).getX() and they produce the same result. If I place two nodes that are exactly the same size than it actually works but if the are different sizes at all than it places them offset from each other. If I subtract the height from the y it should matter the size.
Please help. I have been trying to get this to work right for 3 days now and have tried everything I can think of.
Update:
I figured out my proximity issue. The layout for the new node was not being set right. I tried doing a Platform.runLater before I saved the anchor points of the new node but that had no impact. I fixed it by setting the anchor points for all of the nodes in the pane when I click on a node to drag it. That saved the anchor points correctly.
This however did not fix my issue of nodes of different sizes not laying out in the pane correctly. Here is a screenshot of two nodes of the same size snapping together correctly and two nodes of different sizes not snapping correctly. This makes no sense as the math should be the same.
Here is the code to set the layout for the dropped node relative to the other node:
droppedKromaDevice.setLayoutX(parentKromaDevice.getLayoutX());
droppedKromaDevice.setLayoutY(parentKromaDevice.getLayoutY() - droppedKromaDevice.getBoundsInParent().getHeight());
I found the solution to my two problems.
First, when I was creating new nodes and dropping them on the panel the bounds were not being evaluated correctly so my anchor points were off. I just changed it so when I click on a node to drag it around I loop through all of the other nodes on the panel and build their anchors instead of when I first drop/create it.
Second, in order to get the snap positioning to work accurately I had to base their layout on the delta between the opposite anchor points and not on the bounds of the node. Basically, I get the current x/y of the node I am dropping and than move it using the delta between the dropped node and the node I need to snap it to. The code below is what I used. 0 represents the x coordinate and 1 represents the y coordinate in the array
droppedKromaDevice.setLayoutX(droppedKromaDevice.getLayoutX() - droppedKromaDevice.getBottomAnchorPoint()[0] + parentKromaDevice.getTopAnchorPoint()[0]);
droppedKromaDevice.setLayoutY(droppedKromaDevice.getLayoutY() - droppedKromaDevice.getBottomAnchorPoint()[1] + parentKromaDevice.getTopAnchorPoint()[1]);

Python - CSV Module, Getting Information From a File

Here is the situation:
The first problem I'm having is with obtaining information from a CSV file. The purpose of the code I'm writing is to get a bunch of information on ZCTAs (zip codes), for a number of different cohorts (there are six currently being used, but the code is meant to be flexible to have any number of cohorts). One file contains the population, by cohort, for each ZCTA. Another file has the number of 'cases' (cases of cancer observed) for each cohort, for each ZCTA. Another file has the crude rate for each cohort, for the state of Iowa (the focus of this research), for the rate at which one can 'expect' to see the number of people who have cancer, for a population, by cohort. There are a couple of other files, but these are the focus, as this is where my issue is exhibited.
What my code does, initially, is to read the population file and get the population of each cohort by ZCTA. Each ZCTA, and the information, is stored in a list, which is then stored in a list of lists (nested), containing all of the ZCTAs. The code then gets the crude rate. Then, the crude rate is taken times the appropriate cohort, for each ZCTA and summed with all of the other cohorts within each ZCTA, to get the total number of people we can EXPECT to see having cancer, for each ZCTA. The population is also summed up. This information is stored in a another list, as well as a list containing all of the ZCTAs. This information will be the focus (The list of all of the ZCTAs, which each contain the total population and the total number of expected cases).
So, the problem is that I then need to take this newly acquired list and get the number of OBSERVED cases, for each cohort, sum those together, append it to the appropriate ZCTA and write it to a new file. I have code implemented that does this fine, EXCEPT that the bottom 22 or so ZCTAs don't get the number of observed cases. I don't know if it is the code, or what, but it works for all of the other 906, but doesn't get the bottom 22.
The reader will find sample data for the files I've discussed (the observed case file, and the output file) at: Gist
Here is the code I'm using:
`expectedcsv = open('ExpectedCases.csv', 'w', newline= '')
expectedwriter = csv.writer(expectedcsv, delimiter = ',')
expectedHeader = ['zcta', 'expected', 'pop', 'observed']
thecasesreader = csv.reader(thecasescsv, delimiter = ',')
for zcta in zctaPop:
caseCounter = 0
thecasescsv = open('NewCaseFile.csv', 'r', newline = '')
thecasesreader = csv.reader(thecasescsv, delimiter = ',')
for case in thecasesreader:
if case[0] == zcta[0]:
for i in range(3, len(case)):
caseCounter += int(case[i])
zcta.append(caseCounter)
expectedwriter.writerow(zcta)
expectedcsv.close()
thecasescsv.close()`
Something else I would also like to bring up is that later on in the code, the actual purpose for all of this, is to create an SMR filter, for each grid point. The grid points are somewhat arbitrary they have been placed (via coordinates) over the entire state of Iowa. The SMR is the number of observed divided by the number of expected cases. The threshold, that is, how many expected cases for a particular filter, is set by the user. So, if a user wants a filter created on 150 expected cases (for each grid point), the code goes through each ZCTA, summing up the expected cases until greater than 150 are found. The distance to this last ZCTA is the 'radius' of the filter.
To do this, I built a distance matrix (the distance from each grid point to every ZCTA) and then sorted it, nearest to furthest. Because of the size of the file (2300 X 930), I have to read this file line by line and get all of the information from other files. So, starting with the nearest ZCTA, I get the population, expected cases, and observed cases (the problem with this file was discussed above) and add these each to their respective counter (one for population, one for observed and one for expected). Then it goes to the next closest ZCTA and does the same, until the the threshold is exceeded.
The problem here is that I couldn't use the CSV Module to read these files, as I was already reading from another file and the index would be lost. So, I had to use just the regular filename.read(), which then required some interesting use of maketrans and .translate. I'm not sure its efficient or works great. Everything seems to be fine, but without the above problem being fixed, it's impossible to tell. I have included the code below, but was wondering if anybody had any better ideas/suggestions?
`expectedCSV = open('ExpectedCases.csv', 'r', newline = '')
table = str.maketrans('\r', ' ')
content = expectedCSV.read()
expectedCSV.close()
content = content.translate(table)
content = content.split(sep = '\n')
newContent = []
for item in content:
newContent.append((item.split(sep= ',')))
content = ' '
for item in newContent:
if item[0] == currentZcta:
expectedTotal += (float(item[1]))
totalPop += (float(item[2]))
totalObservedCount += (float(item[3]))`
Also, I couldn't figure out how to color the methods blue and the variables red, as some of the more awesome users of this site do. I would be very much interested in learning how to do that for future posts.
If anybody needs more info or anything clarified to help answer/formulate a solution, please, by all means, ask! Thanks for taking the time to read!
So, I ended up "solving" this by computing the observed along with the expected and population, by opening the file for each ZCTA computed. This did not really solve the issue I was dealing with, but rather found a way around it. I'm somewhat disappointed that more people didn't view and/or respond to this. If someone comes up with an answer to the actual problem, by all means, post it here. -Mike

Height of tab (JTabbedPane) does not change

As the title says, the height of my tabs is not increasing as it should, my code looks like this:
JTabbedPane jtp = new JTabbedPane();
JLabel iconInTab = new JLabel(new ImageIcon("myImage.png"));
iconInTab.setPreferredSize(new Dimension(100,80)); // is the size of my Image, I've also try to do this using getSize
jtp.addTab(null,new JPanel());
jtp.setTabComponentAt(0,iconInTab);
I've also try this using html but it did not work either:
jtp.addTab("<html><p><p><p></html>",new ImageIcon("myImage.png"),new JPanel());
with the first code the problem is not the change of the size horizontally (the width change correctly), the problem is only on the height, with the second code, if I add multiple lines inside the html code, the text appear incomplete (just show the middle line) (also the width behaves as expected, the problem is the height). . .
why is this happening? or how could I get this done?
Note: S.O.: Mac OS X 10.8.1
Solved!!! The problem was that the default UI over MAC OS X (com.apple.laf.AquaTabbedPaneContrastUI), you only need to change it to the basicTabbedPaneUI (or the one of your preference), in my particular case I need to extend this class (it was a pain in the *, because what I wanted was really complex) to get the look & feel that I was expecting, if you have the same trouble just do this before adding your tabs:
myTabbedPane.setUI(new BasicTabbedPaneUI());
Note: Checking the default UI of your TabbedPane, may solve many different problems.

Resources