def status(request): try: pw = request.GET["pw"] except: return render(request, '404.html', {"is_bot": is_bot(request), "debug": get_DEBUG()}) if(pw == "justdoit"): dic = { "legend": '<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>', "labels": [], "uservisits": [], "botvisits": [], "companiesCrawled": [] } counter = Visits.objects.all().count() counter -= 30 visitas = Visits.objects.all().order_by('-id')[:30] for visit in visitas: dic["labels"].append(str(visit.date)[:10]) dic["uservisits"].append(visit.usersVisits) dic["botvisits"].append(visit.botsVisits) dic["companiesCrawled"].append(visit.companiesCrawled) final_dict = { "legend": dic["legend"], "labels": json.dumps(dic["labels"]), "uservisits": json.dumps(dic["uservisits"]), "botvisits": json.dumps(dic["botvisits"]), "companiesCrawled": json.dumps(dic["companiesCrawled"]), "is_bot": is_bot(request), "debug": get_DEBUG() } return render(request, 'status.html', final_dict) else: return render(request, '404.html', {"is_bot": is_bot(request), "debug": get_DEBUG()})
def error500(request): if("bot" in str(request.META['HTTP_USER_AGENT']).lower()): tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.botsVisits=F('botsVisits')+1 tmp_model.save() else: tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.usersVisits=F('usersVisits')+1 tmp_model.save() random = randint(1, MAX_COMPANIES) random_companies = Companies.objects.filter(id__gt=random, active=True).exclude(state="")[:8] return render(request, 'error500.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies}, status="500")
from django.conf.urls import url from django.contrib import admin from Main.views import * from django.views.generic import TemplateView from Main.handlers.settings import get_DEBUG from django.conf.urls import include, url handler500 = 'Main.views.error500' urlpatterns = [ url(r'^$', index, name="index"), url(r'^(?P<nif>[0-9]+)/$', company, name="company"), url(r'^c/(?P<nif>[0-9]+)/$', redirectCompany, name="redirect"), url(r'^about/$', about, name="about"), url(r'^terms/$', terms, name="terms"), url(r'^contact/$', contact, name="contact"), url(r'^v1/$', v1_req, name="v1"), url(r'^v1/docs/$', docs, name="docs"), url(r'^v1/(?P<endpoint>[a-z]+)/$', api_req, name="api"), url(r'^v1/(?P<endpoint>[a-z]+)/(?P<action>[a-z0-9]+)/$', api_req, name="api"), url(r'^sitemap.xml$', sitemapmain, name="sitemapmain"), url(r'^companies-(?P<id>[0-9]+).xml$', sitemap_companies, name="sitemap_companies"), url(r'^status/$', status, name="status"), url(r'^robots.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text')), ] if get_DEBUG() == True: import debug_toolbar urlpatterns.append(url(r'^__debug__/', include(debug_toolbar.urls)))
def index(request): if("bot" in str(request.META['HTTP_USER_AGENT']).lower()): tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.botsVisits=F('botsVisits')+1 tmp_model.save() else: tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.usersVisits=F('usersVisits')+1 tmp_model.save() a = ''' import xlrd start_time = time.time() tmp_all = [] for fn in os.listdir('/root/workspace/extractor/result3/'): print(fn) book = xlrd.open_workbook("/root/workspace/extractor/result3/" + fn) sh = book.sheet_by_index(0) for line in range(1, sh.nrows): tmp_all.append( { "identifier": str(sh.cell_value(rowx=line, colx=1)), "name": str(sh.cell_value(rowx=line, colx=0)), "cae": str(sh.cell_value(rowx=line, colx=2)), "cae_text": str(sh.cell_value(rowx=line, colx=3)), "state": str(sh.cell_value(rowx=line, colx=5)), "city": str(sh.cell_value(rowx=line, colx=6)), "started_at": str(sh.cell_value(rowx=line, colx=4)), "active": bool(sh.cell_value(rowx=line, colx=7)), } ) shuffle(tmp_all) print("Empresas baralhadas") a = 0 b = 0 total = str(len(tmp_all)) for line in tmp_all: b += 1 try: Companies.objects.create( identifier=line["identifier"], name=line["name"], cae=line["cae"], state=line["state"], city=line["city"], started_at=line["started_at"], active=line["active"] ) CAE.objects.create( pk=line["cae"], text=line["cae_text"] ) except: a = a+1 pass sys.stdout.write("\r" + str(b) + " empresas de " + str(total) + " carregadas") sys.stdout.flush() sys.stdout.write("\r" + str(b) + " empresas de " + total + " carregadas\n") sys.stdout.flush() print("Não foi possivel carregar " + str(a) + " empresas") print("--- %s seconds ---" % (time.time() - start_time))''' random = randint(1, MAX_COMPANIES) random_companies = Companies.objects.filter(id__gt=random, active=True).exclude(state="")[:8] return render(request, 'index.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies})
def contact(request): random = randint(1, MAX_COMPANIES) random_companies = Companies.objects.filter(id__gt=random, active=True).exclude(state="")[:8] if request.method == "POST": try: grecaptcharesponse = request.POST.get("g-recaptcha-response", '') if (grecaptcharesponse.strip() == ''): return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "Captcha é obrigatorio"}) name = request.POST["name"].strip() email = request.POST["email"].strip() message = request.POST["message"].strip() company = request.POST.get("company", '').strip() subject = request.POST["subject"] # Verify if the recaptcha is valid data = urllib.parse.urlencode({"secret": "6LcMFykTAAAAADsrJh8PGYKt_wJlhH67d7HgtKQT", "response": grecaptcharesponse, "remoteip": get_remote_IP(request)}) binary_data = data.encode('utf-8') u = urllib.request.urlopen("https://www.google.com/recaptcha/api/siteverify", binary_data) result = u.read() recaptcha_result = json.loads(result.decode('utf-8')) if recaptcha_result["success"] == False: return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "Captcha não é valido, tente novamente"}) if subject not in ["1", "2", "3"]: return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "Ocorreu um erro, tente novamente"}) if len(name) < 1: return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "O nome é obrigatorio"}) if len(email) < 1: return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "O email é obrigatorio"}) if not re.match(r"[^@]+@[^@]+\.[^@]+", email): return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "O email não é valido"}) if len(message) < 1: return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "A mensagem é obrigatoria"}) if len(name) > 50: name = name[0:50] if len(email) > 50: email = email[0:50] if len(message) > 1000: message = message[0:1000] if len(company) > 50: company = company[0:50] Messages.objects.create( name=name, email=email, message=message, company=company, subject=subject, ip=get_remote_IP(request), useragent=request.META['HTTP_USER_AGENT'] ) t = loader.get_template('emails/contact.html') output = t.render({ "name": name, "email": email, "message": message, "company": company, "subject": subject, "ip": get_remote_IP(request), "useragent": request.META['HTTP_USER_AGENT'] }) msg = EmailMultiAlternatives("GetCompany.Info - " + str(name), output, "*****@*****.**", ["*****@*****.**"]) msg.attach_alternative(output, 'text/html') msg.send(True) return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "success": "Mensagem enviada, aguarde resposta nos proximos dias"}) except: return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies, "error": "Ocorreu um erro, tente novamente"}) if("bot" in str(request.META['HTTP_USER_AGENT']).lower()): tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.botsVisits=F('botsVisits')+1 tmp_model.save() else: tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.usersVisits=F('usersVisits')+1 tmp_model.save() return render(request, 'contact.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "random_companies": random_companies})
def company(request, nif): company = get_object_or_404(Companies, identifier=nif) structured_data = { "@context": "http://schema.org", "@type": "Organization", "legalName": company.name, "taxID": company.identifier, "address": [{ "@type": "PostalAddress", "addressCountry": "PT" }] } if company.website: structured_data.update( { "url": company.website } ) else: structured_data.update( { "url": "https://www.getcompany.info/" + str(company.identifier) + "/" } ) if company.address: structured_data["address"][0].update({ "streetAddress": company.address }) if company.phone: structured_data.update({ "contactPoint": [{ "@type": "ContactPoint", "telephone": "+351" + str(company.phone), "contactType": "customer service" }], "telephone": "+351" + str(company.phone) }) if company.fax: structured_data["contactPoint"][0].update( { "faxNumber": "+351" + str(company.fax) } ) if("bot" in str(request.META['HTTP_USER_AGENT']).lower()): tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.botsVisits=F('botsVisits')+1 tmp_model.save() company.visits_bots += 1 company.save() #random = randint(1, MAX_COMPANIES) #random_companies = Companies.objects.filter(id__gt=random, active=True, cae=company.cae).exclude(state="")[:8] return render(request, 'company-bots.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "company": company, "title": str(company.name + " " + company.identifier + " - GetCompany.info"), "structured_data": json.dumps(structured_data)}) if company.error_crawling == True or company.already_crawled == False: t = threading.Thread(target=Crawl_Company, args=[nif]) t.setDaemon(True) t.start() tmp_model = Visits.objects.get_or_create(date=str(dt.datetime.now())[:10])[0] tmp_model.usersVisits=F('usersVisits')+1 tmp_model.save() company.visits_users += 1 company.save() current_hash = str(uuid.uuid4())[:32] free_id = False max_tries = 10 tries = 0 while not free_id: if tries > max_tries: return 504 tries += 1 if Tokens.objects.filter(id=current_hash).exists(): current_hash = str(uuid.uuid4())[:32] else: free_id = True Tokens.objects.create(pk=current_hash, company_id=company.pk) random = randint(1, MAX_COMPANIES) random_companies = Companies.objects.filter(id__gt=random, active=True, cae=company.cae).exclude(state="")[:8] return render(request, 'company.html', {"is_bot": is_bot(request), "debug": get_DEBUG() , "company": company, "random_companies": random_companies, "token": current_hash, "title": str(company.name + " " + company.identifier + " - GetCompany.info"), "structured_data": json.dumps(structured_data)})
def show_toolbar(request): return get_DEBUG()
import os from Main.handlers.settings import get_DEBUG # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = str(os.environ.get('SECRET_KEY')) # SECURITY WARNING: don't run with debug turned on in production! DEBUG = get_DEBUG() ALLOWED_HOSTS = ['*'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'Main', ]