All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mngsk.devicedetector.client.bot.BotParser Maven / Gradle / Ivy

Go to download

The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, feed readers, media players, PIMs, ...), operating systems, brands and models.

There is a newer version: 1.0.10
Show newest version
package io.github.mngsk.devicedetector.client.bot;

import java.util.Optional;
import java.util.regex.Matcher;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.github.mngsk.devicedetector.client.AbstractClientParser;
import io.github.mngsk.devicedetector.client.Client;

public class BotParser extends AbstractClientParser {

	public BotParser() {
		this(new ObjectMapper(new YAMLFactory()));
	}

	public BotParser(ObjectMapper objectMapper) {
		super("bot", "regexes/bots.yml", objectMapper);
	}

	@Override
	public Optional parse(String userAgent) {
		for (BotRegex regex : super.regexes) {
			Matcher matcher = regex.getPattern().matcher(userAgent);
			if (matcher.find()) {
				String name = regex.getName();
				String category = regex.getCategory();
				String url = regex.getUrl();
				BotProducer producer = regex.getProducer();

				return Optional.of(new Bot(name, category, url, producer));
			}
		}
		return Optional.empty();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy