Automating Tinder?! Perks Of Being A Programmer

Engineering
Sabin Sharma Paudel

It may seem hilarious but yes I built a bot to use my Tinder. You might ponder what was the main motivation. Actually just wanted to flex my next match by saying *‘Yeh my bot matched us, Wanna see my bot’ **wink.

Note: Use this line at your own risk. If you wanna be called a creep , you are in the right place. Hey Hey no one called me creep alright. It was my friend okay lol

* In a dramatic scene

Ladies And Gentlemen behold for the future of dating, T-Man your tinder wing-man.

* More intensely

Is using tinder not fun!!!!!, wanna spice things up in your dating life!!!. Use T-Man to select partners for you. Let the bot do all the swiping when you are relaxing . Are you one of those busy heads who do not have time to swipe right and left . T-Man is just the product for you.

Okay Okay , lets get technical now. In this article I will be explaining all the bits and bytes of the bot.

Bot Doing Its Job

Like any other article I will follow 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 are 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.

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 a 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 power to control the device. We can just take a screenshot after forcing 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 person within an image is 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’t wanna swipe right to all the images , or do you? *wink . 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

  1. clone the repo
git clone https://github.com/dcostersabin/T-Man.git
  1. go inside T-Man
cd T-Man
  1. 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 files 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 device and password as its parameter. Firstly, it checks if the phone is awake or sleep.

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 press 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 else redirect device to 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 tinder app if it is present in the device.

Now , Let us assume that we are inside the app and we need to capture the image of whats on the screen. Its done with the help of 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 lol)

So folks now you have a bot to select you a match.

note: Please don’t try this at home or anywhere , get a life ,make friends and please get a real girlfriend. This aint gonna work in real life believe me lol.

Share Article

Few more blogs...