Adsense

Thursday, February 26, 2015

GORKHAPATRA (LOKSEWA SAHAYOGI)

गाेरखापत्र लाेकसेवा सहयाेगी वस्तुगत प्रश्नाेत्तर
(Script To Download the Objective questions published on Gorkhapatra)

   Gorkhapatra Newspaper has been publishing the Objective Questions helpful for preparing Loksewa Exam on every Wednesday. Most of the students miss these questions and when they try to find, it's a bit bite in ass. A friend of mine asked me to download all the available objective questions So I have written a Python script to download all the Gorkhapatra Newspaper page:2 available on the server, where the objective questions are posted on every Wednesday. Hope this will be helpful.
  1. # -*- coding: utf-8 -*-
  2. import mechanize
  3. from bs4 import BeautifulSoup
  4. import fileinput
  5. import cookielib
  6. import re
  7. from datetime import *
  8. import datetime
  9. import os
  10. import urllib
  11. #------------------------------------------------------
  12. def processing(t):
  13.     #extract the content of between pageno":"2" and pageno":"3"
  14.     all_txt= re.search('"pageno":"2"(.*)"pageno":"3"',t)
  15.     text1= all_txt.group(1)
  16.     t=str(text1)
  17.     u=str(text1)
  18.     all_txt= re.search('"buttons"(.*)"box_id":"1"',t)
  19.     if(all_txt):
  20.         text2=all_txt.group(1)
  21.         t=str(text2)
  22.         all_txt= re.search('%s\\\/(.*).jpg"'% fname,t)
  23.         text2=all_txt.group(1)
  24.     else:
  25.         print '\n'
  26.         all_txt= re.search('%s\\\/(.*).jpg"'% fname,t)
  27.         text2=all_txt.group(1)
  28.    
  29.     t=str(text2)
  30.     download= 'http://www.gorkhapatraonline.com/uploads/epaper/'+ str(weekdays[i])+'/'+t +'.jpg'
  31.     print download
  32.     f = open('c:\\quiz_gorkha\\%s.jpg' %fname,'wb')
  33.     f.write(urllib.urlopen(download).read())
  34.     f.close()
  35.    
  36. #------------------------------------------------
  37. def head1():
  38.     print ' \t ******** Created By Dipendra Shrestha ********'
  39. #------------------------------------------------
  40. def heading(by,bm,bd,ey,em,ed):
  41.     head1()
  42.     print '\n\n processing data from ' +str(by)+'-'+str(bm)+'-'+str(bd) +' to '+ str(ey)+'-'+str(em)+'-'+str(ed)
  43. #------------------------------------------------
  44. br = mechanize.Browser()
  45. print '\n\nEnter the Begining Date from which you would like do download gorkhapatra quiz \n'
  46. by=input('Enter year (YYY)')
  47. bm=input('Enter month (MM)')
  48. bd=input('Enter day (DD)')
  49. os.system('cls')
  50. print '\n\nEnter the Ending Date upto which you would like do download gorkhapatra quiz \n'
  51. ey=input('Enter year (YYY)')
  52. em=input('Enter month (MM)')
  53. ed=input('Enter day (DD)')
  54. os.system('cls')
  55. #------------------------------------------------
  56. heading(by,bm,bd,ey,em,ed)
  57. sdate=datetime.date(by,bm,bd)
  58. edate=datetime.date(ey,em,ed)
  59. weekdays=[sdate+datetime.timedelta(days=i) for i in range((edate-sdate).days+1) if(sdate+datetime.timedelta(days=i)).weekday() in (2,2)]
  60. for i in range(len(weekdays)):
  61.     print weekdays[i]
  62.     fname=str(weekdays[i])
  63.     # Getting data from http://www.gorkhapatraonline.com/epaper/getdata/gorkhapatra?time=2015-02-25
  64.     bt=br.open('http://www.gorkhapatraonline.com/epaper/getdata/gorkhapatra?time=%s' %fname).read()
  65.     t=str(bt)
  66.     nodata= re.search('larger',t)
  67.     if(nodata):
  68.         processing(t)       
  69.     else:
  70.         print 'Data not found for the date '+fname +'\n'