Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
pavelfomin / youtube-dl-ffmpeg.md
Last active February 28, 2024 00:35
Download portion of video / audio with youtube-dl / ffmpeg

Download the audio only

  • utlink='https://www.youtube.com/watch?v=bn9F19Hi1Lk'
  • get all video and audio links for the video
    • youtube-dl -F "$utlink"
    • pick the best mp4a audio only option (e.g. 140)
  • get a download link
    • audio=$(youtube-dl -f 140 -g "$utlink")
  • use audio download link in ffmpeg to download portion of the audio
    • download first 5 mins of the audio
  • ffmpeg -i "$audio" -t 300 -c:a libmp3lame waves300.mp3
@pavelfomin
pavelfomin / producer.java
Last active January 11, 2024 21:03
Spring Kafka Producer using KafkaTemplate, CompletableFuture and KafkaTemplate.flush()
@Getter
private final KafkaTemplate<String, T> kafkaTemplate;
public CompletableFuture<SendResult<String, T>> sendMessage(String key, T message, Integer partition) {
try {
return getKafkaTemplate().send(getTopicName(), partition, key, message);
} catch (Exception e) {
throw new RuntimeException("Failed to send a message to topic: " + getTopicName(), e);
}
@pavelfomin
pavelfomin / ObjectMapperConfiguration.java
Created December 29, 2023 15:27
ObjectMapper MixIn configuration for Avro classes
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.apache.avro.generic.GenericContainer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class ObjectMapperConfiguration {
@pavelfomin
pavelfomin / AvroDataConsumer.java
Last active December 8, 2023 15:50
Spring Kafka configuration for consuming JSON and Avro formats using @KafkaListener.
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.MessageHeaders;
@RequiredArgsConstructor
@Slf4j
public class AvroDataConsumer {
@pavelfomin
pavelfomin / metrics.md
Last active March 21, 2025 18:13
How to use @timed annotation
@pavelfomin
pavelfomin / kafka-metrics.md
Last active December 4, 2023 15:01
Useful Kafka Metrics

Kafka metrics:

  • kafka.consumer.fetch.manager.records.consumed.rate
  • kafka.consumer.fetch.manager.records.lag

Spring Kafka Listener metrics

  • spring.kafka.listener.count
  • spring.kafka.listener.max

Metrics queries:

  • sum(align(1m, ts(kafka.consumer.fetch.manager.records.consumed.rate, space=${env} and service=${service} and dc=${dc})), service, dc)
@pavelfomin
pavelfomin / cors-and-xss.md
Last active February 6, 2024 19:34
CORS and XSS

I did some reading on CORS and I think I understand how they can restrict which origins the requests are coming from. However, allowing the cross origin calls from the browser increases a possibility of XSS:

a person with malicious intent injects some JavaScript into a page to steal users' cookies and send them to a URL he controls, all he has to do is add the following header Access-Control-Allow-Origin: * on the server side to make the request work. https://security.stackexchange.com/questions/108835/how-does-cors-prevent-xss

The scenario that CORS is preventing is different:

For example, the victim logged into their bank's application. Then they were tricked into loading an external website on a new browser tab. The external website then used the victim's cookie credentials and relayed data to the bank application

@pavelfomin
pavelfomin / Confluent-Schema-Registry-Metadata.md
Last active September 7, 2023 15:10
Confluent Schema Registry Metadata

Confluent Schema Registry Metadata

Prior to v7.4, schema registry did not support any custom metadata associated with a schema uploaded to the schema registry.

So the following mapping between

  • the version of a jar artifact containing a schema content
  • and the id and version of the schema in the schema registry can only be derived by comparing the content of the schemas in different schema registry environments (using POST /subjects/(string: subject)).
@pavelfomin
pavelfomin / ClassUtil.java
Last active August 30, 2023 21:30
Get jar locations of all classes extending a given class
import org.reflections.Reflections;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.net.URL;
import java.util.Set;
public class ClassUtil {
public <T> MultiValueMap<String, Class<? extends T>> getSubTypeLocationsOf(final String prefix, final Class<T> type) {