Skip to content

Instantly share code, notes, and snippets.

@datamafia
Last active August 29, 2015 13:57
Show Gist options
  • Save datamafia/9606984 to your computer and use it in GitHub Desktop.
Save datamafia/9606984 to your computer and use it in GitHub Desktop.
Handling and fixing a migrated page batch call error from the Facebook graph API. Tests included. Ver verbose. Supports : http://marc-w.com/editorial/parsing-facebook-gotcha/
#!/usr/bin/env python
# marc-w.com
def fix_facebook_migration(link=False,error_message=False):
'''
Fix a batch call for Facebook page ID information
@param link : string, the failed link
@param error_message : string, the 'error' message string returned from facebook
@return : int on fail, new link on fix.
'''
# validate
if not link or not error_message :
return 1
# split link once
split1 = link.split('?ids=',2) # grab the IDs
left = split1[0]+"=" # save for later
# split link twice
try :
split2 = split1[1].split('&',2)
except :
return 2
right = "&"+split2[1] # save for later
ids = split2[0].split(',') # map to list
try : # cast strings to ints in list
ids = map(int, ids)
except :
# error str type ints expected
return 3
# process error message returned from Facebook
err = error_message.split(' ') # word chunks
# pull out the 2 ints per the formatting of the FB error
# beware that the FB error can change at any point in time
# this could lead to infinite loops amd bad mojo
fix = [] # init
for e in err :
if "." in e : # period going to get you
e = e.split('.', 2)[0] # fixed
try : # when int
fix.append(int(e))
except :
continue
# intersect lists
r = filter(set(ids).__contains__, fix)
# swap bad into for migrated id in list
if len(r) == 1 : # we expect one match
index = ids.index(r[0])
ids.pop(index)
ids.append(fix[1])
else :
# not fixed, log this and investigate
# do not return original link, can lead to infinite loop
# feeding garbage back into system
return 4
# format the fix
ids = ','.join(str(elem) for elem in ids)
# build new link
new_link = left+ids+right # right var may be bad (expired token). Address as needed
# if this is run in realtime the access token should still be valid
# other error handling should pick up on bad token attempts
return new_link
########################
# Unit Test
########################
'''
example error message from Facebook, the message part is sent into our function
{
"error": {
"message": "(#21) Page ID 5701733149 was migrated to page ID 6687752570. Please update your API calls to the new ID",
"type": "OAuthException",
"code": 21
}
}
'''
########################
# Unit Test : Passing
########################
link = 'https://graph.facebook.com/?ids=20531316728,6191007822,5701733149&junk'
error_msg = '(#21) Page ID 5701733149 was migrated to page ID 6687752570. Please update your API calls to the new ID'
new_link = fix_facebook_migration(link, error_msg)
print "Passing Test :"
print 'Error Message : '+error_msg
print 'Link : '+link
print 'Result : '+str(new_link)
print "\r\n"
########################
# Unit Test : Fail 1
########################
link = ''
error_msg = '(#21) Page ID 5701733149 was migrated to page ID 6687752570. Please update your API calls to the new ID'
new_link = fix_facebook_migration(link,error_msg)
print "Failing Test 1 : empty link"
print 'Error Message : '+error_msg
print 'Link : '+link
print 'Result : '+str(new_link)
print "\r\n"
########################
# Unit Test : Fail 2
########################
link = 'sdklfjhg'
error_msg = '(#21) Page ID 5701733149 was migrated to page ID 6687752570. Please update your API calls to the new ID'
new_link = fix_facebook_migration(link,error_msg)
print "Failing Test 2 : bad link data"
print 'Error Message : '+error_msg
print 'Link : '+link
print 'Result : '+str(new_link)
print "\r\n"
########################
# Unit Test : Fail 3
########################
link = 'sdklfjhg?ids=fsddkjhsdf&'
error_msg = '(#21) Page ID 5701733149 was migrated to page ID 6687752570. Please update your API calls to the new ID'
new_link = fix_facebook_migration(link,error_msg)
print "Failing Test 3 : spoofed link format, bad data"
print 'Error Message : '+error_msg
print 'Link : '+link
print 'Result : '+str(new_link)
print "\r\n"
########################
# Unit Test : Fail 4a
########################
link = 'https://graph.facebook.com/?ids=20531316728,6191007822,999&junk'
error_msg = '(#21) Page ID 5701733149 was migrated to page ID 6687752570. Please update your API calls to the new ID'
new_link = fix_facebook_migration(link,error_msg)
print "Failing Test 4 : edited link, no intersection"
print 'Error Message : '+error_msg
print 'Link : '+link
print 'Result : '+str(new_link)
print "\r\n"
########################
# Unit Test : Fail 4b
########################
link = 'https://graph.facebook.com/?ids=20531316728,6191007822,999&junk'
error_msg = '(#21) Page ID 5701733149 was migrated to page ID Please update your API calls to the new ID'
new_link = fix_facebook_migration(link,error_msg)
print "Failing Test 4b : missing key data from error message"
print 'Error Message : '+error_msg
print 'Link : '+link
print 'Result : '+str(new_link)
print "\r\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment