/ece301.py

https://github.com/kuruoujou/Course-Note-Grabber · Python · 174 lines · 152 code · 13 blank · 9 comment · 49 complexity · 93813a240d4d046650269d25823e3060 MD5 · raw file

  1. #!/usr/bin/python
  2. import os
  3. os.putenv("DISPLAY",":0.0")
  4. from BeautifulSoup import BeautifulSoup
  5. import re
  6. from urllib2 import urlopen
  7. from urllib import urlretrieve
  8. import string
  9. import sys
  10. import pygtk
  11. import pynotify
  12. home = "https://engineering.purdue.edu/~chihw/11ECE301F/11ECE301F.html"
  13. docs = "https://engineering.purdue.edu/~chihw/11ECE301F/co_desc.html"
  14. notes = "https://engineering.purdue.edu/~chihw/11ECE301F/co_lec.html"
  15. homework = "https://engineering.purdue.edu/~chihw/11ECE301F/co_hw.html"
  16. exams = "https://engineering.purdue.edu/~chihw/11ECE301F/com/sample_exams.html"
  17. formulas = "https://engineering.purdue.edu/~chihw/11ECE301F/com/formula_tables.html"
  18. base = "/home/kuroshi/Documents/School/ECE 301/"
  19. examFile = "Exams"
  20. docsFile = "Documents"
  21. lecFile = "Lecture Notes"
  22. hwFile = "Homework"
  23. fmFile = "Forumula tables"
  24. seFile = "Sample Exams"
  25. def main(base):
  26. #Sets up libnotify
  27. pynotify.init("Basics")
  28. exists = 0
  29. #Downloads past and upcoming exam info...
  30. page = urlopen(home)
  31. soup = BeautifulSoup(page)
  32. links = soup.findAll(href=re.compile('.*?\.pdf'))
  33. eicount = 0
  34. for link in links:
  35. ei = str(link).encode('ascii', 'ignore').split("\"")[1]
  36. filename = ei.split("/")[-1]
  37. for curEi in os.listdir(base + examFile):
  38. if curEi == filename:
  39. exists = 1
  40. if exists == 0:
  41. urlretrieve(home + ei, base + examFile + "/" + filename)
  42. eicount = eicount+1
  43. if eicount == 1:
  44. eil = filename
  45. exists = 0
  46. #Downloads Course Docs
  47. page = urlopen(docs)
  48. soup = BeautifulSoup(page)
  49. links = soup.findAll(href=re.compile('.*?\.pdf|.*?\.html?'))
  50. dcount = 0
  51. for link in links:
  52. doc = str(link).encode('ascii', 'ignore').split("\"")[1]
  53. filename = doc.split("/")[-1]
  54. for curDoc in os.listdir(base + docsFile):
  55. if curDoc == filename:
  56. exists = 1
  57. if exists == 0:
  58. urlretrieve(docs + doc, base + docsFile + "/" + filename)
  59. dcount = dcount+1
  60. if dcount == 1:
  61. docl = filename
  62. exists = 0
  63. #Downloads Lecture Notes
  64. page = urlopen(notes)
  65. soup = BeautifulSoup(page)
  66. links = soup.findAll(href=re.compile('.*?\.pdf|.*?\.html?'))
  67. ncount = 0
  68. for link in links:
  69. note = str(link).encode('ascii', 'ignore').split("\"")[1]
  70. filename = note.split("/")[-1]
  71. for curNote in os.listdir(base + lecFile):
  72. if curNote == filename:
  73. exists = 1
  74. if exists == 0:
  75. urlretrieve(notes + note, base + lecFile + "/" + filename)
  76. ncount = ncount+1
  77. if ncount == 1:
  78. notel = filename
  79. exists = 0
  80. #Downloads Homeworks, Projects, and Solutions
  81. page = urlopen(homework)
  82. soup = BeautifulSoup(page)
  83. links = soup.findAll(href=re.compile('.*?\.pdf|.*?\.html?'))
  84. hcount = 0
  85. for link in links:
  86. hw = str(link).encode('ascii', 'ignore').split("\"")[1]
  87. filename = hw.split("/")[-1]
  88. for curHw in os.listdir(base + hwFile):
  89. if curHw == filename:
  90. exists = 1
  91. if exists == 0:
  92. urlretrieve(homework + hw, base + hwFile + "/" + filename)
  93. hcount = hcount+1
  94. if hcount == 1:
  95. hwl = filename
  96. exists = 0
  97. #Downloads Sample Exams
  98. page = urlopen(exams)
  99. soup = BeautifulSoup(page)
  100. links = soup.findAll(href=re.compile('.*?\.pdf|.*?\.html?'))
  101. secount = 0
  102. for link in links:
  103. sexam = str(link).encode('ascii', 'ignore').split("\"")[1]
  104. filename = sexam.split("/")[-1]
  105. for curSexam in os.listdir(base + seFile):
  106. if curSexam == filename:
  107. exists = 1
  108. if exists == 0:
  109. urlretrieve(exams + sexam, base + seFile + "/" + filename)
  110. secount = secount+1
  111. if secount == 1:
  112. sel = filename
  113. exists = 0
  114. #Downloads Formula Tables
  115. page = urlopen(formulas)
  116. soup = BeautifulSoup(page)
  117. links = soup.findAll(href=re.compile('.*?\.pdf|.*?\.html?'))
  118. fcount = 0
  119. for link in links:
  120. fm = str(link).encode('ascii', 'ignore').split("\"")[1]
  121. filename = fm.split("/")[-1]
  122. for curFm in os.listdir(base + fmFile):
  123. if curFm == filename:
  124. exists = 1
  125. if exists == 0:
  126. urlretrieve(formulas + fm, base + fmFile + "/" + filename)
  127. fcount = fcount+1
  128. if fcount == 1:
  129. forml = filename
  130. exists = 0
  131. #Sends our notifications...
  132. notifyString = ""
  133. if eicount == 1:
  134. notifyString = notifyString + "\nExam information document " + eil + "."
  135. elif eicount > 1:
  136. notifyString = notifyString + "\n" + str(eicount) + " exam information documents."
  137. if dcount == 1:
  138. notifyString = notifyString + "\nCourse document " + docl + "."
  139. elif dcount > 1:
  140. notifyString = notifyString + "\n" + str(dcount) + " course documents."
  141. if ncount == 1:
  142. notifyString = notifyString + "\nLecture note document " + notel + "."
  143. elif ncount > 1:
  144. notifyString = notifyString + "\n" + str(ncount) + " lecture note documents."
  145. if hcount == 1:
  146. notifyString = notifyString + "\nHomework document " + hwl + "."
  147. elif hcount > 1:
  148. notifyString = notifyString + "\n" + str(hcount) + " homework documents."
  149. if secount == 1:
  150. notifyString = notifyString + "\nSample exam file " + sel + "."
  151. elif secount > 1:
  152. notifyString = notifyString + "\n" + str(secount) + " sample exam files."
  153. if fcount == 1:
  154. notifyString = notifyString + "\nFormula file " + forml + "."
  155. elif fcount > 1:
  156. notifyString = notifyString + "\n" + str(fcount) + " formula files."
  157. if eicount > 0 or dcount > 0 or ncount > 0 or hcount > 0 or secount > 0 or fcount > 0:
  158. n = pynotify.Notification("Downloaded ECE 301 Documents", notifyString)
  159. n.show()
  160. if __name__ == "__main__":
  161. main(base)