PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/whit.py

https://bitbucket.org/chef1991/whit
Python | 361 lines | 247 code | 51 blank | 63 comment | 6 complexity | 17c761c7c87910903e4f89132dca0768 MD5 | raw file
  1. from bs4 import BeautifulSoup # HTML handling
  2. from flask import Flask, request, redirect # Routing
  3. import getSummary # A home grown truncation tool
  4. import json # Python's built in JSON library
  5. import twilio.twiml # Handle twilio responses
  6. from twilio.rest import TwilioRestClient # Handle twilio requests
  7. from urllib import urlopen # General Python requests
  8. # Required for Google App Engine's app.yaml
  9. app = Flask(__name__)
  10. -# CrunchBase API Key - Add yours here
  11. -api_key = ''
  12. -
  13. -# Bit.ly API Key - Add yours here
  14. -access_token = ''
  15. # ------------------------------------------------------
  16. # Utility method to get a bitly shortlink from a URL
  17. # ------------------------------------------------------
  18. def getShortlink(url):
  19. # Prep a link to the bitly
  20. shortapi = "https://api-ssl.bitly.com/v3/shorten?access_token=" + access_token + "&longUrl=" + url
  21. # Ask bit.ly for the shortlink
  22. shortLinkResponse = json.loads( urlopen(shortapi).read() )
  23. # Pull the result out of the response dictionary
  24. fullShortLink = shortLinkResponse['data']['url']
  25. # Shorten the shortlink, haha!
  26. shortLinkToDisplay = str(shortLink)[7:15]+str(shortLink)[15:]
  27. # return
  28. return shortLinkToDisplay
  29. # --------------------------
  30. # Replace spaces with %20
  31. # --------------------------
  32. def sanitize(url):
  33. # Break up a URL by spaces
  34. url = url.split(' ');
  35. # Put the %20 in
  36. url = '%20'.join(url)
  37. return str(url)
  38. # -------------------------------------------------------
  39. # Retrieve the contents of a URL and return it as JSON
  40. # -------------------------------------------------------
  41. def JSONFromURL(url):
  42. # Read the URL contents as a string
  43. urlContents = urlopen(url).read()
  44. # Convert the string to JSON
  45. json = json.loads(urlContents)
  46. # Return
  47. return json
  48. # --------------------------------------------------------
  49. # Retrieve the contents of a URL and return the summary
  50. # --------------------------------------------------------
  51. def summaryFromURL(url):
  52. # Grab the JSON
  53. entry = JSONFromURL(url)
  54. # Retrieve the overview
  55. entryText = entry["overview"].get_text()
  56. # Ensure the overview is well formed
  57. overview = BeautifulSoup(entryText)
  58. # Summarize and return the overview
  59. return getSummary(overview)
  60. # -------------------------------------------------------------
  61. # Retrieve the contents of a URL and return the phone number
  62. # -------------------------------------------------------------
  63. def phoneNumberFromURL(url):
  64. # Grab the JSON
  65. entry = JSONFromURL(url)
  66. # Retrieve the overview
  67. number = entry["phone_number"].get_text()
  68. return number
  69. # ------------------------------------------------------
  70. # Requests information about a person from CrunchBase.
  71. #
  72. # queryCrunchBaseForPerson takes two string arguments:
  73. #
  74. # firstName: The person's first name.
  75. # lastName: The person's last name.
  76. # ------------------------------------------------------
  77. def queryCrunchBaseForPerson(firstName, lastName):
  78. # -----------------------------------------
  79. # Retrieve the pernalink from CrunchBase
  80. # -----------------------------------------
  81. # Define a URL to query - Returns a permalink for the relevant username. Also returns a relevant website, if applicable.
  82. summaryURL = 'http://api.crunchbase.com/v/1/people/permalink?first_name=' + firstName + '&last_name=' +lastName + '&api_key=' + api_key
  83. # The URL returns a summary as a string, convert it to a JSON object
  84. summary = JSONFromURL(summaryURL)
  85. # Read out the slug from the dictionary
  86. slug = summary['permalink']
  87. # -----------------------
  88. # Generate a shortlink
  89. # -----------------------
  90. shortLinkToDisplay = getShortlink(summary['crunchbase_url'])
  91. # -----------------------------------
  92. # Retrieve and summarize the entry
  93. # -----------------------------------
  94. # The CrunchBase entry URL
  95. entryURL = 'http://api.crunchbase.com/v/1/person/' + slug + '.js?api_key=' + api_key
  96. # Summarize the overview
  97. entrySummary = summaryFromURL(entryURL)
  98. # ---------
  99. # Return
  100. # ---------
  101. return entrySummary + shortLinkToDisplay
  102. # -------------------------------------------------
  103. # Retrieve a company profile from CrunchBase
  104. # -------------------------------------------------
  105. def queryCrunchBaseForCompanySummary(company):
  106. # -------------------------------------------------
  107. # Retrieve the summary permalink from CrunchBase
  108. # -------------------------------------------------
  109. # Sanitize the name before passing to the CrunchBase API
  110. company = sanitize(company)
  111. # Construct a summary URL
  112. summaryURL = 'http://api.crunchbase.com/v/1/companies/permalink?name=' + company + '&api_key=' + api_key
  113. # Convert the summary to dictionary
  114. summary = JSONFromURL(summaryURL)
  115. # Grab the permalink
  116. slug = summary['permalink']
  117. # -----------------------
  118. # Generate a shortlink
  119. # -----------------------
  120. shortLinkToDisplay = getShortlink(summary['crunchbase_url'])
  121. # -----------------------------------
  122. # Retrieve and summarize the entry
  123. # -----------------------------------
  124. # Construct a URL to the actual CrunchBase Profile
  125. entryURL = 'http://api.crunchbase.com/v/1/company/' + slug + '.js?api_key=' + api_key
  126. # Summarize the overview
  127. entrySummary = summaryFromURL(entryURL)
  128. # ---------
  129. # Return
  130. # ---------
  131. return entrySummary + shortLinkToDisplay
  132. # --------------------------------------------------
  133. # Retrieve a company phone number from CrunchBase
  134. # --------------------------------------------------
  135. def queryCrunchBaseForCompanyNumber(company):
  136. # -------------------------------------------------
  137. # Retrieve the summary permalink from CrunchBase
  138. # -------------------------------------------------
  139. # Sanitize the company
  140. company = sanitize(company)
  141. # summary URL
  142. summaryURL = 'http://api.crunchbase.com/v/1/companies/permalink?name=' + company + '&api_key=' + api_key
  143. # Convert the summary to dictionary
  144. summary = JSONFromURL(summaryURL)
  145. # Grab the permalink slug
  146. slug = summary['permalink']
  147. # -----------------------
  148. # Generate a shortlink
  149. # -----------------------
  150. shortLinkToDisplay = getShortlink(summary['crunchbase_url'])
  151. # -----------------------------------
  152. # Retrieve and summarize the entry
  153. # -----------------------------------
  154. entryURL = 'http://api.crunchbase.com/v/1/company/' + slug + '.js?api_key=' + api_key
  155. # Summarize the overview
  156. number = phoneNumberFromURL(entryURL)
  157. # ---------
  158. # Return
  159. # ---------
  160. return company + ': ' + number
  161. def parseWiki(search):
  162. url = 'http://en.wikipedia.org/w/api.php?format=json&action=opensearch&search=' + search + '&prop=revisions&rvprop=content'
  163. r = urlopen(url).read()
  164. dict1 = json.loads(r)
  165. dict2 = dict1['1'][0]
  166. url2 = 'http://en.wikipedia.org/wiki/' + dict2
  167. '''
  168. #Finding page id:
  169. dict3 = dict2.split(" ")
  170. if dict3.length() > 1:
  171. dict3 = dict2[0]
  172. for i in range(1, company.length()):
  173. dict3 = dict3 + '_' + dict2.split(" ")[i]
  174. url2 = 'http://en.wikipedia.org/wiki/' + dict3
  175. http://en.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=jsonfm
  176. '''
  177. #parse article
  178. p = 'http://en.wikipedia.org/w/api.php?action=parse&prop=text&page=' + dict2 + '&format=json'
  179. p1 = urlopen(p).read()
  180. p2 = json.loads(p1)
  181. p3 = p2['parse']['text']['*']
  182. p4 = BeautifulSoup(p3)
  183. p5 = p4.find_all('p')
  184. p6 = p5[0]
  185. p7 = p6.getText()
  186. #create bitly shortlink
  187. shortapi = "https://api-ssl.bitly.com/v3/shorten?access_token=794e02fd047d7fcc0c44543742d0f471e2f9ebc8&longUrl=" + url2
  188. shortLink = json.loads( urlopen(shortapi).read() )
  189. shortened = shortLink['data']['url']
  190. return getSummary(p7 ) + str(shortened)[7:15]+str(shortened)[15:]
  191. def parseStock(ticker):
  192. url = 'http://dev.markitondemand.com/Api/Quote/json?symbol=' + ticker + '&callback=myFunction'
  193. r = urlopen(url).read()
  194. e = json.loads(r)
  195. return str(e['Data']['Name']) + '[' + str(e['Data']['Symbol']) + ']: ' + 'is priced $'+ str(e['Data']['LastPrice']) + ". Since " + str(e['Data']['Timestamp'])
  196. @app.route("/", methods=['GET', 'POST'])
  197. def hello_monkey():
  198. twilioInput = request.form['Body']
  199. text_body = twilioInput.split(" ")
  200. inputString = text_body[0]
  201. firstLetter = text_body[0][0]
  202. # The command code
  203. firstLetters = text_body[0][:2]
  204. # p for person
  205. # c for company
  206. # n for company number
  207. # w for wiki
  208. # text_body[0] is first name, text_body[1] is last name
  209. if inputString.__len__() > 0:
  210. if firstLetters.lower() == 'p':
  211. try:
  212. twilioOutput = queryCrunchBaseForPerson(text_body[0][2:].lower(), text_body[1].lower())
  213. except:
  214. twilioOutput = "Sorry, no one named " + text_body[0][2:] + " exists in crunchbase."
  215. elif firstLetters.lower() == 'c:':
  216. try:
  217. twilioOutput = queryCrunchBaseForCompanySummary(twilioInput[1:])
  218. except:
  219. twilioOutput = "Sorry, no company named '" + twilioInput[2:] + "' exists in crunchbase."
  220. elif inputString[:3] == 'n:':
  221. try:
  222. twilioOutput = queryCrunchBaseForCompanyNumber(twilioInput[1:])
  223. except:
  224. twilioOutput = "Sorry, no number exists for this company."
  225. elif firstLetters.lower() == 'w:':
  226. try:
  227. twilioOutput = parseWiki(twilioInput[1:])
  228. except:
  229. twilioOutput = "Sorry, no matches came up for your wiki query, please refine your search."
  230. elif firstLetters.lower() == 's:':
  231. try:
  232. twilioOutput = parseStock(inputString[1:])
  233. except:
  234. twilioOutput = "Sorry, no matches came up for your stock query, please refine your search."
  235. elif twilioInput.lower() == 'h:':
  236. twilioOutput = "Send the name of a person, a company, or a stock ticker code. Commands:\np'person', c:'company', s:'Stock', w:'wikipedia'\npBill Gates\nc:Google\nngoogle\ns:aapl\nw:obama"
  237. else:
  238. try:
  239. twilioOutput = parseWiki(twilioInput)
  240. except:
  241. "Please enter a valid wiki search query. Type * for help."
  242. else:
  243. try:
  244. twilioOutput = parseWiki(twilioInput)
  245. except:
  246. "Please enter a valid wiki search query. Type * for help."
  247. '''
  248. account_sid = "ACa1c928ab9eb750ba1fb4ed0953f0f032"
  249. auth_token = "9365adeb7fe7068e0859bb8c89437607"
  250. client = TwilioRestClient(account_sid, auth_token)
  251. from_number = request.values.get('From', None)
  252. message = client.sms.messages.create(to=from_number, from_="+19177913098", body="From the Hackathon:")
  253. '''
  254. resp = twilio.twiml.Response()
  255. resp.sms(twilioOutput)
  256. return str(resp)
  257. if __name__ == "__main__":
  258. app.run(debug=True)