Automating Tinder?! Perks Of Being A Programmer

Sabin
by Sabin 

It may seem hilarious, but I built a bot to use my Tinder. You might wonder what my main motivation was. I just wanted to impress my next match by saying, ‘Yeah, my bot matched us; wanna see my bot?’ *wink.

Note: Use this line at your own risk. If you want to be called a creep, you are in the right place.

Hey, Hey, no one has called me a creep, alright? It was my friend, okay lol.

Okay, in all seriousness, this bot was just a fun little experimentation for me. But if you are one of those busy heads who do not find the time to go through all the tinder profiles and swipe left and right, this bot is just the product for you!

Drum Roll

**Ladies And Gentlemen, presenting to you the future of dating; T-Man, your tinder wing-man.**

Let’s get technical now. In this article, I will explain all the bits and bytes of an amazing bot that will definitely get you a new match.

Automating Tinder Gurzu

Like any other article, I will follow the divide and conquer for separating and solving problems.

Problems

  1. Communicating With Android
  2. Controlling Device Programmatically
  3. Capturing The Images
  4. Detecting Person Within That Image
  5. Classifying The Image
  6. Making Decisions

Communicating With Android

I just have one word i.e ADB. For anyone who is wondering what the hell on earth is ADB, relax I got your back. ADB stands for Android Debug Bridge which is mostly used by (as you have guessed it YES!) programmers or developers. It lets you establish a TCP connection via USB or remote(depending on your devices). It consists of a client and server on the host PC, where the server connects to the daemon on the android device.

You might also enjoy reading: Make your own AI powered game

Controlling Device Programmatically

ADB is just a mechanism to communicate but the main challenge is to control the android device programmatically. Since python is my bread and butter I just need an adb client library. So, I came across a well-documented library namely pure-python-adb which will be our go-to for communication.

Capturing The Images

Now, we have the power to control the device. We can just take a screenshot after forcing the device to open tinder when we run the script. After the screenshot is captured within the device, it is pulled via adb to our local device (PC).

Detecting Person Within That Image

Detecting a person within an image is a fairly easy task as we will be using OpenCV’s haarcascade for frontal face detection.

Classifying The Image

In this project we will be just classifying the gender of the person and the age. The catch is to swipe right for all the females detected within the age limit. You don’ wanna swipe right to all the images, or do you? *win . Here we will be using a small caffe model

Making Decisions

After we have classified the images we need a mechanism to swipe right or swipe left. Again adb comes in handy.

Code

You can find all the implementation on my personal GitHub . T-Man

Requirements

  1. python 3.9
  2. adb

For debian system do

sudo apt install adb -y

Setting Up Project

  • clone the repo
git clone https://github.com/dcostersabin/T-Man.git
  • go inside T-Man
cd T-Man
  • Install requirements
pip install -r requirements.txt

Running

python main.py --PIN ****<your phones pin code>

Explanation

I will not go line by line for each file because many of them are self-explanatory but I will provide a reference to where the code is for the parts that I will be explaining.

Initializing The ADB Client

TClient.py -> TClient

class TClient(Client):

We create a class by inheriting all the properties of the Client class. Since the default config is all we need.

Client.py (Provided By The Installed Library)

class Client(Host):
    def __init__(self, host='127.0.0.1', port=5037):
        self.host = host
        self.port = port
.....

TClient.py -> TClient

def __show_connected_devices(self):
    print(f'Total Connected Devices {len(self.devices())}\n')
    for idx, device in enumerate(self.devices()):
        print(f'Device #{idx + 1}\'s Serial: {device.serial}')

def __select_main_device(self):
    self.main_device = self.devices()[0] if len(self.devices()) > 0 else self.__close()
    print(f'\nSelecting Main Device as {self.main_device.serial}')

__show_connected_devices lists out all the devices connected to the PC via adb

__select_main_device set’s the first device as our main device to be used

So after we have initialized our working device, now we need to unlock the phone. The code to unlock the phone can be found in Unlocker.py

class Unlock:

    def __init__(self, device: Device, passwd: str):
        self.main_device = device
        self.passwd = passwd
        self.status = True
        self.__check_status()
        self.__unlock()

    def __check_status(self):
        _command = "dumpsys power | grep 'mWakefulness='"
        status = str(self.main_device.shell(_command).split('=')[1].strip())
        self.status = True if status == "Asleep" or status == "Dozing" else False

    def __unlock(self):
        _command = f'input keyevent POWER && input swipe 600 600 0 0 ' \
                   f'&& input text {self.passwd} && input keyevent 66'
        if self.status:
            self.main_device.shell(_command)

Unlocker.py contains a simple class namely Unlock which takes the device and password as its parameter. Firstly, it checks if the phone is awake or asleep.

If the phone is asleep, it wakes up the phone by mimicking a key event to unlock the phone. Then it swipes up, types the password for us, and presses enter programmatically.

After we have unlocked the device we need to check if Tinder is present or not. If it is present we need to command our device to open the app or else redirect the device to the play store where they can install Tinder.

Check.py -> TinderCheck

class TinderCheck:

    def __init__(self, device: Device):
        self.main_device = device
        self.exists = False
        self.__check_tinder()

    def __check_tinder(self):
        if self.main_device.is_installed('com.tinder'):
            print('*********** Opening Tinder ***********')
            self.__open_tinder()note: Please don’t try this at home or anywhere , get a life and make a real girlfriend.
        else:
            print('************ Tinder Not Found ************ \n')
            self.__install_tinder()

    def __install_tinder(self):
        _command = "am start -a android.intent.action.VIEW -d 'market://details?id=com.tinder'"
        self.main_device.shell(_command)
        print("___________ Redirecting To Tinder On Play Store ___________")

    def __open_tinder(self):
        _command = "monkey -p com.tinder -c android.intent.category.LAUNCHER 1"
        self.main_device.shell(_command)
        self.exists = True

As previously mentioned, it opens the tinder app if it is present on the device.

Now, Let us assume that we are inside the app and we need to capture the image of what’s on the screen. It’s done with the help of a separate class namely Capture.

class Capture:

    def __init__(self, device: Device):
        self.main_device = device
        self.base_dir = Path(__file__).resolve().parent.parent
        self.__create_dir()

    def __create_dir(self):
        if os.path.exists(f'{self.base_dir}/temp'):
            return
        print(f'Creating temp Directory In {self.base_dir}')
        os.mkdir(f'{self.base_dir}/temp')

    def capture(self):
        _command = "screencap -p sdcard/tinder_check.png"
        self.main_device.shell(_command)
        self.__save()

    def __save(self):
        self.main_device.pull('/sdcard/tinder_check.png', f'{self.base_dir}/temp/temp.png')
        self.__resize()

    def __resize(self):
        img = cv2.imread(f'{self.base_dir}/temp/temp.png')
        height, width = img.shape[0], img.shape[1]
        height_cutoff = height // 6
        crop_image = img[height_cutoff: height - height_cutoff, :]
        cv2.imwrite(f'{self.base_dir}/temp/temp.png', crop_image)

In this class firstly we create a temporary directory to save the capture image if not present. Then, we command the device to take a screenshot and store it as tinder_check.png. After it has captured the image we pull that image namely tinder_check.png to our local device(PC) via adb’s pull method. After we have pulled the image stored on android device to our local device, we then perform some preprocessing i.e we crop the image to exclude the top part so that our face in tinder app is not detected.

We are half way there now, all we need to do is to classify the image and make decisions. The detection and classification part can be found in AI.py file.

AI.py -> Model

def __predict(self, image, rectangles):
    for (x, y, w, h) in rectangles:
        w = w + 100
        h = h + 100
        face_img = image[y:y + h, x:x + w].copy()
        cv2.imwrite(f'{self.base_dir}/temp/a.png', face_img)
        blob = cv2.dnn.blobFromImage(face_img, 1, (227, 227), MODEL_MEAN_VALUES, swapRB=False)
        self.gender_net.setInput(blob)

        gender_preds = self.gender_net.forward()
        gender = gender_list[gender_preds[0].argmax()]
        self.detected_genders.append(gender)

        self.age_net.setInput(blob)
        age_predict = self.age_net.forward()
        age = age_list[age_predict[0].argmax()]
        self.detected_ages.append(age)

I will not go in details of each and every line of this file because all we are doing is loading a pre trained model and predicting. Here, we append all detected genders within the image and their ages respectively.

Now, comes the decision making process which can be found in Inputs.py. In this file there are logic for swiping right and left via adb. The decision making looks something like this

if len(genders) == 0 and len(ages) == 0:
    self.__swipe_left()
    return

if genders.count('Male') > 0:
    self.__swipe_left()
    return

We dont wanna swipe right if there are no gender or ages detected. If detected it should be only females not male. (lol)

if age > 18:
    self.__swipe_right()
    return

Here, we right swipe all the females whose age is greater than 18 (* should be legal right?)

So folks now you have a bot to select you a match. Happy Tindering!

Note: Please don’t try this at home or anywhere else. Get a life ,make friends, meet people and please get a real girlfriend. This is not going to work in real life. (Source: Trust me)

Conclusion

Technology can sometimes help make mundane things fun. This article’s goal is to showcase how to automate swiping on Tinder with Decision Making Algorithms. Go through our blog to explore more interesting tutorials and articles.

Need help with application development? Book a free consulting session with us.