Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. import cookielib
  2. import urllib
  3. import urllib2
  4. import time
  5. import re
  6. import sys
  7. from HTMLParser import HTMLParser
  8.  
  9. # Handles HTML data
  10. class MLStripper(HTMLParser):
  11.     def __init__(self):
  12.         self.reset()
  13.         self.fed = []
  14.     def handle_data(self, d):
  15.         self.fed.append(d)
  16.     def get_data(self):
  17.         return ''.join(self.fed)
  18.  
  19. # Gets string between two other strings in a string, stringception :D
  20. def find_between( s, first, last ):
  21.     try:
  22.         start = s.index( first ) + len( first )
  23.         end = s.index( last, start )
  24.         return s[start:end]
  25.     except ValueError:
  26.         print "error"
  27.  
  28. # Gets rid of HTML tags, we need plain, legible english!
  29. def strip_tags(html):
  30.     s = MLStripper()
  31.     s.feed(html)
  32.     return s.get_data()
  33.  
  34. # Searches the html source of the RT page for a specific section that holds the description
  35. def getDescription(data):
  36.     messageBody = find_between(str(data),'<div class="message-stanza plain-text-white-space">','</div>')
  37.     if('<span' in messageBody):
  38.         start = 0;
  39.         end = messageBody.index('<span')
  40.         return strip_tags(messageBody[start:end])
  41.     return strip_tags(messageBody)
  42.  
  43. # Searches the html source of the RT page for a specific section that holds the requestor's name
  44. def getContact(data):
  45.     noHtml = strip_tags(data)
  46.     requestors = find_between(noHtml,'Requestors:','Cc:')
  47.     requestors = requestors.splitlines()
  48.     requestors = filter(lambda x:  not re.match(r'^\s*$', x), requestors)
  49.     return requestors
  50.  
  51. # IDs of the URL parameters in Salesforce
  52. subjectID = "&cas14="
  53. contactNameID = "&cas3="
  54. descriptionID="&cas15="
  55. accountNameID="&cas4="
  56.  
  57. # The PHP code runs this script with an argument, which is from the user's input of RT URL
  58. url = sys.argv[1] # this will be the RT URL
  59. #url="https://rt.corexchange.com/Ticket/Display.html?id=231338"
  60.  
  61. # Pretend this script is a browser so I can get some cookies!
  62. values = {'user' : '-snip-',
  63.           'pass' : '-snip-' }
  64. data = urllib.urlencode(values)
  65. cookies = cookielib.CookieJar()
  66. opener = urllib2.build_opener(
  67.     urllib2.HTTPRedirectHandler(),
  68.     urllib2.HTTPHandler(debuglevel=0),
  69.     urllib2.HTTPSHandler(debuglevel=0),
  70.     urllib2.HTTPCookieProcessor(cookies))
  71. response = opener.open(url, data)
  72. the_page = response.read()
  73. http_headers = response.info()      
  74.  
  75. # Prepares the info to be in a URL (no spaces, special characters, etc)
  76. final_subject = urllib.quote_plus(strip_tags(find_between(str(the_page),'<title>','</title>')))
  77. final_desc = urllib.quote_plus(getDescription(the_page))
  78. final_contact = urllib.quote_plus(getContact(the_page)[0])
  79. baseURL='https://na4.salesforce.com/500/e?retURL=%2F500%2Fo&RecordType=01260000000DZlO&ent=Case'
  80. addToURL='&CF00N60000001iwk8="8600%20Harry%20Hines%20Blvd/Fl-1/Ste-200/Rm-Data"&00N60000001iwkf=Maintenance%20Request&00N60000002AGjT=zColo&00N60000002DNPX=zColo&cas11=Customer'
  81. addToURL += subjectID + final_subject
  82. addToURL += contactNameID + final_contact
  83. addToURL += descriptionID + final_desc
  84. finalURL = baseURL + addToURL
  85.  
  86. # This is the DATA that is passed back to PHP
  87. print find_between(str(the_page),'<title>','</title>') + "DELIMITER"
  88. print getDescription(the_page) + "DELIMITER"
  89. print getContact(the_page)[0] + "DELIMITER"
  90. print finalURL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement