def open(url, keyword): driver = webdriver.Chrome() driver.implicitly_wait(20) driver.get(url) locator = (By.ID, 'queryExpr-str') try: WebDriverWait(driver, 20, 0, 5).until(EC.presence_of_all_elements_located(locator)) except WebDriverException: mailbot.send_mail('*****@*****.**', '专利爬取出错,出错位置:#01') driver.close() finally: search = driver.find_element_by_id("queryExpr-str") search.send_keys(keyword[0]) driver.find_element_by_id("btnSearchHome").click() locator = (By.ID, 'idpageSize') try: WebDriverWait(driver, 20, 0.5).until(EC.presence_of_all_elements_located(locator)) except WebDriverException: mailbot.send_mail('*****@*****.**', '专利爬取出错,出错位置:#02') driver.close() finally: select = Select(driver.find_element_by_id('idpageSize')) select.select_by_value('50') time.sleep(5) driver.find_element_by_xpath( '//*[@id="idItem0"]/div/div[1]/h3/p[1]/span[2]').click() time.sleep(3) driver.switch_to.window(driver.window_handles[1]) i = 1 while i < 3: locator = (By.XPATH, '//*[@id="divPatentList"]/ul/li[50]') try: WebDriverWait(driver, 20, 0.5).until( EC.presence_of_all_elements_located(locator)) except WebDriverException: # mailbot.send_mail('*****@*****.**', '专利爬取出错,出错位置:#03') print(driver.page_source) print(WebDriverException.msg) driver.close() finally: test = driver.find_element_by_xpath( '//*[@id="divPatentList"]/ul/li[50]/p[1]/span') print(test.text) while test.text != str(i * 50) + '.': time.sleep(5) test = driver.find_element_by_xpath( '//*[@id="divPatentList"]/ul/li[50]/p[1]/span') print('page-%d' % i) item = driver.find_element_by_class_name('bd') get_info(item.find_elements_by_tag_name('li'), driver, keyword) driver.find_element_by_xpath( '//*[@id="divPatentList"]/div/a[2]').click() i = i + 1 driver.close()
def run_test(): mobi_file = mailbot.get_file('http://archiveofourown.org/works/829218?view_adult=true') print mobi_file print "now testing MIME packing" mime = mailbot.pack_MIME(mail_to, 'http://archiveofourown.org/works/829218?view_adult=true') print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'now sending...' mailbot.send_mail(mime, mail_to) print 'done!'
def run_test(): mobi_file = mailbot.get_file( 'http://archiveofourown.org/works/829218?view_adult=true') print mobi_file print "now testing MIME packing" mime = mailbot.pack_MIME( mail_to, 'http://archiveofourown.org/works/829218?view_adult=true') print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'now sending...' mailbot.send_mail(mime, mail_to) print 'done!'
def auto_ticket(request): #you thought you were going to break things by not having an e-mail? ha! if not request.user.email: return redirect('account/email') if request.method == 'POST': game = AutoCode() #!!! #let's talk about security sometime...(...later)(...never) up = UserProfile.objects.get(user = request.user) if 'ticket_random' in request.POST: if up.ticket_decrement(1): if not game.code_rand(request.user.email): #this wouldn't be necessary if I didn't do these 'function evaluates t/f' things up.ticket_increment(1) return HttpResponse('Oops<br>You have been refunded.') else: return HttpResponse('not enough tickets: '+str(up.ticket_count)) elif 'ticket_selection' in request.POST: gsf = GameSelectForm(request.POST) if gsf.is_valid(): if up.ticket_decrement(2): if not game.code_select(request.user.email, gsf.cleaned_data['gameselect'].id): up.ticket_increment(2) return HttpResponse('Oops<br>You have been refunded.') else: return HttpResponse('not enough tickets: '+str(up.ticket_count)) else: return HttpResponse('invalid choice') #TODO print game print str(game.code) attach_code = game subject = 'Here is your requested code!' if attach_code: url_get = game.get_url_get() url_return = game.get_url_return() #hahaha, oh wow what is this mess message = '<br>' + 'Here is a code for ' + str(game.game) +'.<br> To redeem it, go to '+url_get+'<br>If you would like to return it to us, go to '+url_return else: message = '<br>Unfortunately, we are out of codes for '+str(game.game) + '.' print message donation_mail = mailbot.pack_MIME(message, request.user.email, subject) mailbot.send_mail(donation_mail, request.user.email) diagnostic = str(game.game.id) return HttpResponse('Check your email for your gift of '+str(game.game)+ '<br>'+diagnostic) return HttpResponse('huh?')
def bid_form(request): if request.method == 'POST': bid_form = BidForm(request.POST) auction = Auction.objects.get(uuid = request.GET.get('auction_id')) #structural validation if bid_form.is_valid(): #logical validation if not bid_form.is_kosher(request.GET.get('auction_id')): return HttpResponse('your bid is no longer valid, try again') bid_id = bid_form.save_bid(request.GET.get('auction_id')) #mail time! I should stop using this #one of these days, google is going to completely deprecate this #laaaaazy message = "Hey " + unicode(bid_form.cleaned_data['name']) + ",<p>You've submitted a bid for " + unicode(auction.item_name) + " for the amount of " + unicode(bid_form.cleaned_data['amount']) + ".<p>Your bid page can be found <a href='" + unicode(LAZY_TLD) +"/silent_auction/bid/" + unicode(bid_id) + "'>here</a>.<p>Thanks for bidding and good luck!" mail = mailbot.pack_MIME(message, bid_form.cleaned_data['email'], 'Your bid for ' + unicode(auction.item_name)) print mail mailbot.send_mail(mail, bid_form.cleaned_data['email']) return redirect('/silent_auction/bid/' + unicode(bid_id)) else: return HttpResponse('your bid is structurally <i>wrong</i>, somehow') else: auction_id = request.GET.get('auction_id') if auction_id == None: return redirect('/silent_auction/') auction = get_object_or_404(Auction.objects.filter(uuid = auction_id)) #auto fill if logged in, saving precious seconds! if request.user.is_authenticated(): form = BidForm(initial = { 'amount': auction.top_bid + 1, 'name' : request.user.username, 'email' : request.user.email, }) else: form = BidForm(initial = { 'amount': auction.top_bid + 1, }) context = { 'form' : form, 'auction' : auction, 'auction_id' : auction_id, } return render(request, 'silent_auction/bid_form.html', context)
def handle(self, *args, **options): my_settings = Settings.objects.all()[0] queryset = Code.objects.filter(uuid_assigned = True).exclude(uuid_claimed = True) print datetime.datetime.utcnow() for code in queryset: if code.uuid_expired.replace(tzinfo = None) > datetime.datetime.utcnow() and code.uuid_expired.replace(tzinfo = None)- datetime.timedelta(1) < datetime.datetime.utcnow(): print 'warning issued for ' + str(code) message = 'Your code for ' + str(code.game.game) + ' is expiring in one day.<br> Go to ' + my_settings.global_url + 'get?code='+code.uuid +' to redeem it.' mailbot.send_mail(mailbot.pack_MIME(message, code.assigned, 'Your code is expiring'), code.assigned) if code.uuid_expired.replace(tzinfo = None) < datetime.datetime.utcnow(): print 'resetting ' + str(code) code.uuid_reset() code.game.update_count() print '~~~~~'
def get_detail(info,keyword): #在这里写数据库 #info是一个字典,里面有如下字段'title': 专利的标题 'apply_time': 专利申请时间 'announce_time': 公示时间 'owner': 专利申请人 sql = "INSERT INTO Application(Title,ApplyTime,AnnounceTime,Owner,Category) VALUES ('%s','%s','%s','%s','%s')" % (info["title"],info['apply_time'],info['announce_time'],info['owner'],keyword[0]) global lin_num lin_num = lin_num + 1 try: # 执行sql语句 cursor.execute(sql) # 执行sql语句 db.commit() except: # 发生错误时回滚 db.rollback() message = "专利信息插入数据库异常!出错位置:#3 出错文件:fetch.py" mailbot.send_mail('*****@*****.**', message) mailbot.send_mail('*****@*****.**', message) mailbot.send_mail('*****@*****.**', message) print("234567876543212345678765432") sheet.write(lin_num, 0, info['title']) sheet.write(lin_num, 1, info['apply_time']) sheet.write(lin_num, 2, info['announce_time']) sheet.write(lin_num, 3, info['owner']) print("%s %s"%(info['title'], info['apply_time']))
def auto_donate(request): #you thought you were going to break things by not having an e-mail? ha! if not request.user.email: return redirect('account/email') if request.method == 'POST': #TODO: game selection not necessarily hardcoded game_id = DONATION_GIVEAWAY_ID #TODO: donation message based on real donation info #I don't actually have systems for that. This isn't a real marathon site! donation = 'Thank you for your donation of ' + str(random.randint(2,100)) + ' ' + random.choice(['US Dollars', 'Bahraini Dinars', 'Icelandic Krona', 'Pakistani Rupees', 'Qatari Rial','Tanzanian Shillings','South Korean Won','Colombian Pesos','Estonian Kroon']) +'.' game = AutoCode() print game attach_code = game.code_select(request.user.email, game_id) subject = 'Thank you for "donating"' if attach_code: url_get = game.get_url_get() url_return = game.get_url_return() #hahaha, oh wow what is this mess message = donation + '<br>' + 'Here is a code for ' + str(game.game) +'.<br> To redeem it, go to '+url_get+'<br>If you would like to return it to us, go to '+url_return + '<br>This link will expire in 7 days, so don\'t forget to redeem it!' else: message = donation + '<br>Unfortunately, we are out of codes for '+str(game.game) + '.' print message donation_mail = mailbot.pack_MIME(message, request.user.email, subject) mailbot.send_mail(donation_mail, request.user.email) update_ticket = UserProfile.objects.get(user = request.user) update_ticket.ticket_increment(1) if update_ticket.donation_update(): print 'yes' context = { 'donation': donation, 'game' : game.game, } return render(request, 'dispenser/auto_donate.html', context) else: return redirect('/dispenser/auto')
def open(url, keyword): driver = webdriver.Chrome() driver.implicitly_wait(20) driver.get(url) locator = (By.ID, 'queryExpr-str') try: WebDriverWait(driver, 20, 0, 5).until(EC.presence_of_all_elements_located(locator)) except WebDriverException: mailbot.send_mail('*****@*****.**', '专利爬取出错,出错位置:#01') driver.close() finally: search = driver.find_element_by_id("queryExpr-str") search.send_keys(keyword) driver.find_element_by_id("btnSearchHome").click() locator = (By.ID, 'idpageSize') try: WebDriverWait(driver, 20, 0.5).until(EC.presence_of_all_elements_located(locator)) except WebDriverException: mailbot.send_mail('*****@*****.**', '专利爬取出错,出错位置:#02') driver.close() finally: select = Select(driver.find_element_by_id('idpageSize')) select.select_by_value('50') i = 1 while i < 3: locator = (By.XPATH, '//*[@id="idItem49"]/div/div[1]/h3/p[1]/span[1]') try: WebDriverWait(driver, 20, 0.5).until(EC.presence_of_all_elements_located(locator)) except WebDriverException: mailbot.send_mail('*****@*****.**', '专利爬取出错,出错位置:#03') driver.close() finally: test = driver.find_element_by_xpath('//*[@id="idItem49"]/div/div[1]/h3/p[1]/span[1]') print(test.text) while test.text != str(i*50): time.sleep(5) test = driver.find_element_by_xpath('//*[@id="idItem49"]/div/div[1]/h3/p[1]/span[1]') print('page-%d'%i) item = driver.find_element_by_id('resultContainer_ab') get_info(item.find_elements_by_class_name('item'),keyword) driver.find_element_by_id('idPageNext').click() i = i + 1 driver.close()
def run_diagnostic(): mail = mailbot.diagnostic('I am the very model of a modern major general',mail_to) mailbot.send_mail(mail, mail_to)
def run_diagnostic(): mail = mailbot.diagnostic('I am the very model of a modern major general', mail_to) mailbot.send_mail(mail, mail_to)