Skip to content

Instantly share code, notes, and snippets.

View mcgill-a's full-sized avatar

Alex McGill mcgill-a

  • Scott Logic
  • Edinburgh
View GitHub Profile
@mcgill-a
mcgill-a / index.ts
Last active September 27, 2023 16:43
What it should've looked like all along
io.on("connection", async (socket: Socket) => {
socket.on("position", (user: string) => {
userService.position(JSON.parse(user) as User)
});
});
userService.change.subscribe((change: UserChange) => {
io.sockets.emit(SocketChannel.user, JSON.stringify(change))
});
@mcgill-a
mcgill-a / index.ts
Last active October 2, 2023 17:44
A mistake with my backend Socket.IO Server
// when a user connects to the WebSocket server
io.on("connection", async (socket: Socket) => {
// listen for users with new location data
socket.on("position", (user: string) => {
// update our reference of the user and trigger a change event
userService.position(JSON.parse(user) as User);
});
// listen for those change events
@mcgill-a
mcgill-a / android-geolocation.ts
Last active September 25, 2023 13:52
A minimal frontend snippet for using GPS data in Angular
// https://github.com/capacitor-community/background-geolocation
this.BackgroundGeolocation.addWatcher(
{
requestPermissions: true,
stale: false,
distanceFilter: 10
},
(current, error) => {
if (!current || error) return;
const { latitude, longitude, time } = current;
@mcgill-a
mcgill-a / user.service.ts
Created September 25, 2023 12:51
A minimal frontend service for updating user data in Angular
@Injectable({ providedIn: 'root' })
constructor(private socketService: SocketService) {
this.subs.add(
this.socketService.userChange.subscribe((change: UserChange) => {
const {user, action} = change;
switch (change.action) {
case UserAction.join:
case TeamAction.position:
this._users.set(user.email, user);
@mcgill-a
mcgill-a / socket.service.ts
Last active September 25, 2023 12:36
A minimal frontend service for listening to web socket changes in Angular
@Injectable({ providedIn: 'root' })
export class SocketService {
constructor(private socket: Socket) {}
public readonly userChange = this.socket
.fromEvent<string>(SocketChannel.user)
.pipe(map((x) => JSON.parse(x) as UserChange));
}
@mcgill-a
mcgill-a / user.service.ts
Last active September 25, 2023 14:57
A minimal backend user service
export class UserService {
private _change = new Subject<UserChange>();
private _users = new Map<string, User>();
public readonly change = this._change.asObservable();
public get users(): User[] {
return Array.from(this._users.values());
}
@mcgill-a
mcgill-a / index.ts
Created September 25, 2023 09:31
A minimal backend controller
userService.change.subscribe((change) =>
io.sockets.emit(SocketChannel.user, JSON.stringify(change))
);
app.post("/users/join", (req, res) => {
try {
res.json(userService.join(<User>req.body));
} catch (error) {
res.status(400).send(error);
}
@mcgill-a
mcgill-a / link_investors.py
Last active July 4, 2020 17:22
Generate a CSV that links investors with companies they invest in from a list of target companies
@mcgill-a
mcgill-a / get_laws.py
Created June 26, 2020 12:48
Extract laws from any textual PDF document within a specific directory + sub directories
import sys
import csv
import argparse
import re
import io
import os
import os.path
from tqdm import tqdm
from pathlib import Path
from pdfminer.converter import TextConverter
@mcgill-a
mcgill-a / get_files.py
Created June 15, 2020 16:10
Download, unzip, and categorise the files scraped from the Environmental Impact Statement (EIS) Database exported to a CSV file
import sys, csv, requests, zipfile, io, re
from tqdm import tqdm
from pathlib import Path
def load_data(filename):
arr = []
with open(filename, 'r', encoding='UTF-8') as file:
reader = csv.reader(file)
for row in reader: