Created
June 7, 2012 23:23
-
-
Save tonosaman/2892315 to your computer and use it in GitHub Desktop.
SalesForce Report CSV downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from mechanize import Browser | |
from urllib import quote | |
import re | |
def fetch_sf_csv(report_id): | |
login_uri = "https://login.salesforce.com/" | |
username = "example@salesforce.com" | |
password = "<your-password>" | |
b = Browser() | |
b.set_handle_robots(False) | |
b.open(login_uri + '/?un=' + username + '&pw=' + password + '&startURL=' + quote(report_id) + '?export=1&enc=UTF-8&xf=csv') | |
link = b.find_link(url_regex=re.compile("^https://[^/]*salesforce.com/" + report_id)) | |
return b.follow_link(link).read() | |
def fetch_sf_opportunities_csv(): | |
return fetch_sf_csv("00OE0000000Gu0p") | |
if __name__ == "__main__": | |
print fetch_sf_opportunities_csv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting! Extremely useful.
I believe you have an extra / in the URI
https://login.salesforce.com//?un=
vs.
https://login.salesforce.com/?un=
After taking trailing . from the login_uri, I was able to use the script.
Also, I had to use Python 2, and urlllib2