susioma/buyer/views.py

78 lines
2.6 KiB
Python

from django.shortcuts import render
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import random
from dbsettings.views import getValue
from buyer.models import Card
import time
from smsauth.views import sendMessage, SELLER
def getCard(allow_buy=False):
unused = Card.objects.filter(delivered=None) # pylint: disable=E1101
try:
card = random.SystemRandom().choice(unused)
except:
if allow_buy:
card = buyCard()
else:
card = None
return card
def buyCard():
browser = webdriver.Firefox()
browser.get("https://www.wkv.com/?action=my_account")
if "Login" in browser.title:
user = browser.find_element_by_name("email")
user.send_keys(getValue("buyer.wkv.email"))
pwd = browser.find_element_by_name("password")
pwd.send_keys(getValue("buyer.wkv.password") + Keys.RETURN)
time.sleep(2)
if "Login" in browser.title:
raise ValueError("Could not log in. Please check if the provided credentials are correct.")
browser.get("https://www.wkv.com/?action=buy_card&pr=1&pf=1")
product = Select(browser.find_element_by_name("product"))
product.select_by_visible_text("paysafecard classic 10€")
browser.find_element_by_class_name("btn").click()
time.sleep(2)
browser.find_element_by_class_name("btn").click()
method = Select(browser.find_elements_by_name("payment_method"))
method.select_by_visible_text("Credit card")
browser.find_element_by_class_name("btn").click()
time.sleep(2)
browser.find_element_by_name("accept_terms").click()
browser.find_element_by_class_name("btn").click()
time.sleep(5)
try:
browser.find_element_by_id("existingCcSelect")
except:
ccnum = browser.find_element_by_name("pan")
ccnum.send_keys(getValue("buyer.creditcard.number"))
expmonth = Select(browser.find_element_by_name("newExpMonth"))
expmonth.select_by_value(str(int(getValue("buyer.creditcard.expmonth"))))
expyear = Select(browser.find_element_by_name("newExpYear"))
expyear.select_by_visible_text(getValue("buyer.creditcard.expyear"))
finally:
cvv = browser.find_element_by_name("cvv")
cvv.send_keys(getValue("buyer.creditcard.cvv") + Keys.RETURN)
time.sleep(15)
return Card()
def sendStatus():
status = len(Card.objects.filter(delivered=None))
return sendMessage("A Paysafecard has been sold. There are %i remaining in stock." % status, SELLER)