Getting instagram post url from media id - api

I have the post media_id in my hands and I'd like to know if there is a way to create a valid url from it.
For instance, if you have a Facebook post id (xxxx_yyyy) on your hands, you can create the following url from it (http://facebook.com/xxxx/posts/yyyy) and directly access the original post.
Is there a way to do this on Instagram? Having media_id (and user_id) in my hands, is it possible to create a single post url?

I had to implement client side javascript to solve this and Seano's answer was invaluable and I was glad they mentioned the use of the BigInteger library however I wanted to provide a complete implementation using the BigInteger library which as it turns out is quite necessary.
I downloaded the BigInteger lib from https://www.npmjs.com/package/big-integer.
Here is the function which works well for me.
function getInstagramUrlFromMediaId(media_id) {
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
var shortenedId = '';
media_id = media_id.substring(0, media_id.indexOf('_'));
while (media_id > 0) {
var remainder = bigInt(media_id).mod(64);
media_id = bigInt(media_id).minus(remainder).divide(64).toString();
shortenedId = alphabet.charAt(remainder) + shortenedId;
}
return 'https://www.instagram.com/p/' + shortenedId + '/';
}
I just want to point out that the usage of toString() when assigning the re-calculated media_id is very important, the value remains a string to ensure the entire number is used (in my case the media_id was 19 characters long). The BigInteger documentation also states this...
Note that Javascript numbers larger than 9007199254740992 and smaller than -9007199254740992 are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings.
Cheers!

I just stumbled across this question, all the solutions are great, but none of them were in a language I use often, so running the code would have been a pain in the ass for me.
Seeing that Python is everywhere, I ported over #seano's answer:
def getInstagramUrlFromMediaId(media_id):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
shortened_id = ''
while media_id > 0:
remainder = media_id % 64
# dual conversion sign gets the right ID for new posts
media_id = (media_id - remainder) // 64;
# remainder should be casted as an integer to avoid a type error.
shortened_id = alphabet[int(remainder)] + shortened_id
return 'https://instagram.com/p/' + shortened_id + '/'

You can also figure it out algorithmically. It can be done in any language, but here is a way via MySQL function:
DROP FUNCTION IF EXISTS url_fragment
DELIMITER |
CREATE FUNCTION url_fragment(PID BIGINT)
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE ALPHA CHAR(64);
DECLARE SHORTCODE VARCHAR(255) DEFAULT "";
DECLARE MEDIAID BIGINT;
DECLARE REMAINDER INT;
SET ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
SET MEDIAID = SUBSTRING_INDEX(PID, "_", 1);
WHILE MEDIAID > 0 DO
SET REMAINDER = MOD(MEDIAID, 64);
SET MEDIAID = (MEDIAID - REMAINDER) / 64;
SET SHORTCODE = CONCAT(SUBSTRING(ALPHA, (REMAINDER + 1), 1), SHORTCODE);
END WHILE;
RETURN SHORTCODE;
END
|
DELIMITER ;

Here's how to derive the URL segment from the media ID using JavaScript:
function getInstagramUrlFromMediaId(media_id) {
media_id = parseInt(media_id.substring(0, media_id.indexOf('_')));
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
var shortenedId = '';
while (media_id > 0) {
var remainder = modulo(media_id % 64);
media_id = (media_id - remainder) / 64;
shortenedId = alphabet.charAt(remainder) + shortenedId;
}
return 'https://www.instagram.com/p/' + shortenedId + '/';
}
Depending on how old the Instagram post is, you may need to use a library like BigInteger to handle the larger IDs.
You can find a breakdown of the encoding between media id and url segment here: http://carrot.is/coding/instagram-ids

The instagram API returns a "shortcode" property. Something simple like this could work for you.
post.Link = "https://www.instagram.com/p/" + media.shortcode;

Yes this would be possible using the following endpoint one of the media endpoints:
Here is a small php script that will get the link based on media_id and user_id:
$media_id = 'media_id';
$user_id = 'user_id';
$client_id = 'instagram_client_id';
$client_secret = 'instagram_client_secret';
$url = "https://api.instagram.com/v1/media/{$media_id}_{$user_id}?client_id=$client_id&client_secret=$client_secret";
$response = file_get_contents($url);
$response = json_decode($response);
if (is_object($response)) {
$link_to_media = $response->data->link;
}
In the above example you should replace the media_id, user_id, client_id, client_secret with their appropriate values. You should also be able to use an access_token instead of client_id and client_secret as in this example.
References
Media Endpoints

I made Nick's answer use the BigInt that ships with Node. I also avoid returning the full url, I just return the shortcode.
function getInstagramShortcodeFromId(media_id) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
let shortenedId = '';
while (media_id > 0) {
const remainder = BigInt(media_id) % BigInt(64);
media_id = ((BigInt(media_id) - BigInt(remainder)) / BigInt(64)).toString();
shortenedId = alphabet.charAt(Number(remainder)) + shortenedId;
}
return shortenedId;
}

Related

Is there a way to get back source code from antlr4ts parse tree after modifications ctx.removeLastChild/ctx.addChild? [duplicate]

I want to keep white space when I call text attribute of token, is there any way to do it?
Here is the situation:
We have the following code
IF L > 40 THEN;
ELSE
IF A = 20 THEN
PUT "HELLO";
In this case, I want to transform it into:
if (!(L>40){
if (A=20)
put "hello";
}
The rule in Antlr is that:
stmt_if_block: IF expr
THEN x=stmt
(ELSE y=stmt)?
{
if ($x.text.equalsIgnoreCase(";"))
{
WriteLn("if(!(" + $expr.text +")){");
WriteLn($stmt.text);
Writeln("}");
}
}
But the result looks like:
if(!(L>40))
{
ifA=20put"hello";
}
The reason is that the white space in $stmt was removed. I was wondering if there is anyway to keep these white space
Thank you so much
Update: If I add
SPACE: [ ] -> channel(HIDDEN);
The space will be preserved, and the result would look like below, many spaces between tokens:
IF SUBSTR(WNAME3,M-1,1) = ')' THEN M = L; ELSE M = L - 1;
This is the C# extension method I use for exactly this purpose:
public static string GetFullText(this ParserRuleContext context)
{
if (context.Start == null || context.Stop == null || context.Start.StartIndex < 0 || context.Stop.StopIndex < 0)
return context.GetText(); // Fallback
return context.Start.InputStream.GetText(Interval.Of(context.Start.StartIndex, context.Stop.StopIndex));
}
Since you're using java, you'll have to translate it, but it should be straightforward - the API is the same.
Explanation: Get the first token, get the last token, and get the text from the input stream between the first char of the first token and the last char of the last token.
#Lucas solution, but in java in case you have troubles in translating:
private String getFullText(ParserRuleContext context) {
if (context.start == null || context.stop == null || context.start.getStartIndex() < 0 || context.stop.getStopIndex() < 0)
return context.getText();
return context.start.getInputStream().getText(Interval.of(context.start.getStartIndex(), context.stop.getStopIndex()));
}
Looks like InputStream is not always updated after removeLastChild/addChild operations. This solution helped me for one grammar, but it doesn't work for another.
Works for this grammar.
Doesn't work for modern groovy grammar (for some reason inputStream.getText contains old text).
I am trying to implement function name replacement like this:
enterPostfixExpression(ctx: PostfixExpressionContext) {
// Get identifierContext from ctx
...
const token = CommonTokenFactory.DEFAULT.createSimple(GroovyParser.Identifier, 'someNewFnName');
const node = new TerminalNode(token);
identifierContext.removeLastChild();
identifierContext.addChild(node);
UPD: I used visitor pattern for the first implementation

coldfusion 9, params not found when using cfscript query

I am getting this error:
Parameter 'user_id AND league_id' not found in the list of parameters specified
I am passing them in, and I can see them in a dump placed inside the function.
here's whats going in...
ac = {
league_id = 1
,user_id = 3
,score1 = 14
,score2 = 10
};
this is the dump that is throwing the error....
writedump( Game.saveGame( argumentcollection = ac ) );
Here is the Game.saveGame function
public any function saveGame( required numeric league_id, required numeric user_id, required numeric score1, required numeric score2 ) {
// writeDump( var=arguments ); // this dump shows league_id & user_id...
var sql = '';
var tmp = '';
var r = '';
var q = new query();
q.setDatasource( this.dsn );
q.addParam(
name = 'league_id'
,value = '#val( arguments.league_id )#'
,cfsqltype = 'CF_SQL_BIGINT'
);
q.addParam(
name = 'user_id'
,value = '#val( arguments.user_id )#'
,cfsqltype = 'CF_SQL_BIGINT'
);
q.addParam(
name = 'score1'
,value = '#val( arguments.score1 )#'
,cfsqltype = 'CF_SQL_SMALLINT'
);
q.addParam(
name = 'score2'
,value = '#val( arguments.score2 )#'
,cfsqltype = 'CF_SQL_SMALLINT'
);
sql = 'INSERT INTO
games
(
user_id
,league_id
,score1
,score2
)
VALUES
(
:user_id
,:league_id
,:score1
,:score2
)
';
tmp = q.execute( sql = sql );
r = tmp.getPrefix( q );
q.clearParams();
return r;
}
Here is some history of the issue -
I am writing this locally and my system is running CF 11.2 - everything works fine... However, I had to host it on a CF 9.02 server, and that is when this error showed up - I cannot recreate it on my system although, I do recall seeing this error before, but for the life of me I cant find how I solved it then....
Any help or insight is appreciated.
Other params
Windows server, MySQL 5.5, Apache 2.2
Adam Wrote:
I can't spot what's wrong with your code, but you could perhaps clean
things up a bit. There's no need to set each "property" on the query
separately: blog.adamcameron.me/2014/01/
Following the ordinal param, instead of the named param, AND passing in the array of params in the condensed format, has solved my issue.
I may play with the name attribute and see if it is that, precisely.. or I may just deal with this as a solution.
Now, to change all my script queries!!!!!
(I wish his article came up on google, when I was looking into the query.cfc syntax. :(
Thanks a bunch Adam!

xpages currency incomplete entry failing

I have a form that accepts currency from several fields and depending on what I enter into one, it calculates others. For this most part the code below works (example being the invoice amount field)
<xp:inputText value="#{FInvoiceDoc.InvoiceAmount}" id="InvoiceAmount">
<xp:this.converter>
<xp:convertNumber type="currency"></xp:convertNumber></xp:this.converter>
<xp:eventHandler event="onchange" submit="false" id="eventHandler5">
<xp:this.script><![CDATA[var rate = XSP.getElementById("#{id:ExchangeRate}").value;
var stAmount = XSP.getElementById("#{id:InvoiceAmount}").value;
var stTvat = XSP.getElementById("#{id:TVAT}").value;
var stTst = XSP.getElementById("#{id:TST}").value;
var stToop = XSP.getElementById("#{id:Toop}").value;
var tmp;
// get the dojo currency code
dojo.require("dojo.currency");
// get the numeric values using parse
amount = dojo.currency.parse(stAmount,{currency:"USD"});
total = rate * amount;
XSP.getElementById("#{id:EstUSAmount}").innerHTML = dojo.currency.format(total,{currency:"USD"});
XSP.getElementById("#{id:InvoiceAmount}").value = dojo.currency.format(amount,{currency:"USD"});
if (amount != 0) {
tvat = dojo.currency.parse(stTvat,{currency:"USD"});
tst = dojo.currency.parse(stTst,{currency:"USD"});
toop = dojo.currency.parse(stToop,{currency:"USD"});
tmp = (tvat / (amount-tvat-tst-toop)) * 100;
XSP.getElementById("#{id:VP}").innerHTML = tmp.toFixed(2);
tmp = (tst / (amount-tvat-tst-toop)) * 100;
XSP.getElementById("#{id:STP}").innerHTML = tmp.toFixed(2);
tmp = (toop / (amount-tvat-tst-toop)) * 100;
XSP.getElementById("#{id:OoPP}").innerHTML = tmp.toFixed(2);
}
]]></xp:this.script>
</xp:eventHandler>
</xp:inputText>
Like I said, for the most part this works, but if I enter only a partial number like
200.2
instead of
200.20
then it fails. Most data entry folks will want to not have to key in the last "0" just so it's legal.
btw, if I enter it in as just 200 with no cents, it's OK.
It's as if the statement;
amount = dojo.currency.parse(stAmount,{currency:"USD"});
requires a 2 digit penny amount or no pennys, but doesn't like just the leading cents digit.
Any way around this?
The currency format is very restrictive and needs the certain number of decimal places for a given currency. As you can see here there can be different number of decimal places depending on currency. For most currencies it needs two decimal places though. But, you can trick the parser. Just try it with standard number of decimal places and if it fails try it with one decimal place again. Your code would look like this then:
var amount = dojo.currency.parse(stAmount,{currency:"USD"});
if (!amount && amount!==0) {
amount = dojo.currency.parse(stAmount,{currency:"USD",places:"1"});
}
This accepts inputs like
12
12.3
12.34
$12
$12.3
$12.34
But it is still very picky. It doesn't accept spaces or more decimal places then currency allows.
If you want more flexibility for your users and need USD currency only I'd go for dojo.number or other number parsing instead and show the "$" outside the input field. Then, you'd be able to accept much more formats and could add functions like rounding.
Yeah, I've played with the converters. Thing is I want the converter to stay "Currency". The data I receive from the notes database is always correct and formats properly. Its the manipulation that I''m having a problem with. Since this is all on the client side, I've created a function that I can reuse throughout the page.
<script>
function isNumeric(n) {
n = parseFloat(n);
return !isNaN(n) || n != n;
}
// We call this if its a currency amount. If they only entered one number after
// the decimal, then we just append a 0. We're expecting a string and we send
// the string back.
function cMask(Amount)
{
if (Amount == "") { return "0.00" }
a = Amount.split(".");
if (a.length == 2)
{
if (a[1] != null)
{
if (a[1].length == 1)
{
Amount = Amount + "0";
}
}
}
// get the dojo currency code
dojo.require("dojo.currency");
b = dojo.currency.parse(Amount,{currency:"USD"});
if (isNumeric(b)) {return Amount} else {return "0.00"}
}
</script>
Then its a matter of just changing the initial variable load line.
var stAmount = cMask(XSP.getElementById("#{id:InvoiceAmount}").value);
Seems to work and I've just taught myself how to create reusable client-side javascript for my page.

Use of a string to get information in an associative array

I've got a function that takes two strings as arguments, and I want to use these arguments to get the data held in associative arrays.
var myVar:Object = {};
myVar.value = 10;
function getStuff(v:String, vl:String){
//...
}
In this case, v = "myVar" and vl = "value".
How do I translate vinto the variable name 'myVar' and v1 into 'value' so that I can access the data?
Sorry if this won't work, for I can only test AS3 here, but please try this:
function getStuff(v:String, vl:String){
return eval(v + "." + vl);
}
eventually
function getStuff(v:String, vl:String){
return eval("_gobal." + v + "." + vl);
}

websql use select in to get rows from an array

in websql we can request a certain row like this:
tx.executeSql('SELECT * FROM tblSettings where id = ?', [id], function(tx, rs){
// do stuff with the resultset.
},
function errorHandler(tx, e){
// do something upon error.
console.warn('SQL Error: ', e);
});
however, I know regular SQL and figured i should be able to request
var arr = [1, 2, 3];
tx.executeSql('SELECT * FROM tblSettings where id in (?)', [arr], function(tx, rs){
// do stuff with the resultset.
},
function errorHandler(tx, e){
// do something upon error.
console.warn('SQL Error: ', e);
});
but that gives us no results, the result is always empty. if i would remove the [arr] into arr, then the sql would get a variable amount of parameters, so i figured it should be [arr]. otherwise it would require us to add a dynamic amount of question marks (as many as there are id's in the array).
so can anyone see what i'm doing wrong?
aparently, there is no other solution, than to manually add a question mark for every item in your array.
this is actually in the specs on w3.org
var q = "";
for each (var i in labels)
q += (q == "" ? "" : ", ") + "?";
// later to be used as such:
t.executeSql('SELECT id FROM docs WHERE label IN (' + q + ')', labels, function (t, d) {
// do stuff with result...
});
more info here: http://www.w3.org/TR/webdatabase/#introduction (at the end of the introduction)
however, at the moment i created a helper function that creates such a string for me
might be better than the above, might not, i haven't done any performance testing.
this is what i use now
var createParamString = function(arr){
return _(arr).map(function(){ return "?"; }).join(',');
}
// when called like this:
createparamString([1,2,3,4,5]); // >> returns ?,?,?,?,?
this however makes use of the underscore.js library we have in our project.
Good answer. It was interesting to read an explanation in the official documentation.
I see this question was answered in 2012. I tried it in Google 37 exactly as it is recommened and this is what I got.
Data on input: (I outlined them with the black pencil)
Chrome complains:
So it accepts as many question signs as many input parameters are given. (Let us pay attention that although array is passed it's treated as one parameter)
Eventually I came up to this solution:
var activeItemIds = [1,2,3];
var q = "";
for (var i=0; i< activeItemIds.length; i++) {
q += '"' + activeItemIds[i] + '", ';
}
q= q.substring(0, q.length - 2);
var query = 'SELECT "id" FROM "products" WHERE "id" IN (' + q + ')';
_db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results1) {
console.log(results1);
debugger;
}, function (a, b) {
console.warn(a);
console.warn(b);
})
})