Skip to content

Commit

Permalink
mt source fixed and working agaon :)
Browse files Browse the repository at this point in the history
  • Loading branch information
niczem committed Jan 7, 2024
1 parent c8f0bff commit 69d2731
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 150 deletions.
96 changes: 48 additions & 48 deletions src/classes/sources/ais/mst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Source from "../Source";
import Source from '../Source'
class MyShipTracking extends Source {
parseLocation = async function (result: any) {
const location = {
Expand All @@ -7,85 +7,85 @@ class MyShipTracking extends Source {
longitude: result.longitude,
course: result.course,
speed: result.speed,
source: "myshiptracking.com",
source_type: "AIS",
};
return location;
};
source: 'myshiptracking.com',
source_type: 'AIS'
}
return location
}

// the method in the sources class does not work if no seconds are in the string
dmsToDecimalDegreesMST = function (dms) {
console.log(dms);
return parseFloat(dms);
};
console.log(dms)
return parseFloat(dms)
}

getLocation = async (mmsi: number) => {
const result = await this.fetch(
"https://www.myshiptracking.com/vessels/mmsi-" + mmsi,
'https://www.myshiptracking.com/vessels/mmsi-' + mmsi,
{
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"accept-language": "de-DE,de;q=0.6",
"cache-control": "max-age=0",
"sec-ch-ua":
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
'accept-language': 'de-DE,de;q=0.6',
'cache-control': 'max-age=0',
'sec-ch-ua':
'"Chromium";v="116", "Not)A;Brand";v="24", "Brave";v="116"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"sec-gpc": "1",
"upgrade-insecure-requests": "1",
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'sec-gpc': '1',
'upgrade-insecure-requests': '1',
cookie:
"usr_lang_exist=1; port_tz=LT; user_tz=MT; user_df=1; session_id_sp_trk=fd1i0pb2mt6i4nra9c1mejsmvk; offset=Europe%2FBerlin; usr_lang_exist=1; io=ilOBwd-Xh9Z1fGzpFaM0",
'usr_lang_exist=1; port_tz=LT; user_tz=MT; user_df=1; session_id_sp_trk=fd1i0pb2mt6i4nra9c1mejsmvk; offset=Europe%2FBerlin; usr_lang_exist=1; io=ilOBwd-Xh9Z1fGzpFaM0'
},
"GET",
);
'GET'
)
const pattern =
/<th>(Longitude|Latitude|Course|Speed|Position Received)<\/th>\s*<td>(.*?)<\/td>/g;
const extractedData: any = {};
let match, parsedDate;
/<th>(Longitude|Latitude|Course|Speed|Position Received)<\/th>\s*<td>(.*?)<\/td>/g
const extractedData: any = {}
let match, parsedDate

while ((match = pattern.exec(result)) !== null) {
const key = match[1];
const value = match[2];
extractedData[key] = value;
const key = match[1]
const value = match[2]
extractedData[key] = value
}

console.log(extractedData["Position Received"]);
console.log(123);
console.log(extractedData['Position Received'])
console.log(123)

// Extract the date and time string using regular expression
const regex = /title="([^"]+)"/;
const matchd = extractedData["Position Received"].match(regex);
const regex = /title="([^"]+)"/
const matchd = extractedData['Position Received'].match(regex)

if (matchd?.[1]) {
const dateTimeString = matchd[1];
const dateTimeString = matchd[1]

// Parse the extracted date and time string
const [datePart, timePart] = dateTimeString.split(" ");
const [year, month, day] = datePart.split("-");
const [hour, minute] = timePart.split(":");
const [datePart, timePart] = dateTimeString.split(' ')
const [year, month, day] = datePart.split('-')
const [hour, minute] = timePart.split(':')

// JavaScript months are 0-based, so subtract 1 from the month
parsedDate = new Date(year, month - 1, day, hour, minute);
parsedDate = new Date(year, month - 1, day, hour, minute)

console.log(parsedDate); // This will log the parsed date object
console.log(parsedDate) // This will log the parsed date object
} else {
console.log("Date not found in the input string.");
console.log('Date not found in the input string.')
}

const position = {
latitude: this.dmsToDecimalDegreesMST(extractedData.Latitude),
longitude: this.dmsToDecimalDegreesMST(extractedData.Longitude),
course: 10,
speed: parseFloat(extractedData.Speed),
timestamp: new Date(parsedDate).toISOString(),
};
console.log(position);
return position;
};
timestamp: new Date(parsedDate).toISOString()
}
console.log(position)
return position
}
}

export default MyShipTracking;
export default MyShipTracking
136 changes: 34 additions & 102 deletions src/classes/sources/ais/mt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Source from "../Source";
import Source from '../Source'
class Marinetraffic extends Source {
parseLocation = async function (result: any) {
const location = {
Expand All @@ -7,111 +7,43 @@ class Marinetraffic extends Source {
longitude: result.longitude,
course: result.course,
speed: result.speed,
source: "Marinetraffic",
source_type: "AIS",
};
return location;
};

getLocation = async (mmsi: number) => {
async function autoScroll(page) {
await page.evaluate(async () => {
await new Promise<void>((resolve, reject) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;

if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});
source: 'Marinetraffic',
source_type: 'AIS'
}
return location
}

const browser = await this.getBrowser();
const page = await browser.newPage();
getLocation = async (mmsi: number) => {
const browser = await this.getBrowser()
const page = await browser.newPage()

const url =
"https://www.marinetraffic.com/en/ais/details/ships/mmsi:310627000";
await page.goto(url);

await page.waitForSelector("#vesselDetails_summarySection");

// Scroll to the bottom of the page
await autoScroll(page);

const text_content_course = await page.$eval(
"#vesselDetails_latestPositionSection",
(div) => div.textContent,
);

const courseRegex = /Speed\/Course: (\d+\.\d+) kn \/ (\d+) °/;
const course_match = text_content_course.match(courseRegex);
let course, timestamp, latitude, longitude, speed;

if (course_match) {
course = parseFloat(course_match[2]);
} else {
throw new Error("could not parse course from marinetraffic");
}
console.log("course", course);

const timestampRegex =
/Position Received: (\d{4}-\d{2}-\d{2} \d{2}:\d{2} \D{2} {2}\D{3})/;
const match_time = text_content_course.match(timestampRegex);
if (match_time) {
timestamp = match_time[1].replace("LT", "");
console.log(timestamp);
} else {
throw new Error("Could not parse timestamp from marinetraffic");
}

const text_content_pos = await page.$eval(
"#vesselDetails_summarySection",
(div) => div.textContent,
);

const regex_pos =
/position (\d+° \d+' \d+\.\d+" [NS]), (\d+° \d+' \d+\.\d+" [EW])/;
const matches = text_content_pos.match(regex_pos);
if (matches && matches.length === 3) {
latitude = matches[1];
longitude = matches[2];

console.log("Latitude:", latitude);
console.log("Longitude:", longitude);
} else {
console.log(matches);
throw new Error("could not parse location from marinetraffic");
}

const speedRegex = /currently sailing at ([\d.]+) knots/;
const speedMatch = text_content_pos.match(speedRegex);

if (speedMatch && speedMatch.length === 2) {
speed = parseFloat(speedMatch[1]);

console.log("Speed:", speed, "knots");
} else {
throw new Error("Speed not found in the text.");
}

await browser.close();
'https://www.marinetraffic.com/en/ais/details/ships/mmsi:' + mmsi
await page.goto(url)
// let parsedData = null;

const waitForResponse = new Promise((resolve) => {
page.on('response', async (response) => {
const request = response.request()
if (request.url().includes('latestPosition')) {
const jsonresult = await response.text()
const parsedData = JSON.parse(jsonresult)
resolve(parsedData)
}
})
})

const parsedData: any = await waitForResponse
browser.close()
const result = {
course: parseFloat(course),
speed: parseFloat(speed),
latitude: this.convertRawCoordinatesIntoDecimal(latitude),
longitude: this.convertRawCoordinatesIntoDecimal(longitude),
timestamp: new Date(timestamp).toISOString(),
};
console.log(result);
return await this.parseLocation(result);
};
course: parseFloat(parsedData.course),
speed: parseFloat(parsedData.speed),
latitude: parseFloat(parsedData.lat),
longitude: parseFloat(parsedData.lon),
timestamp: new Date(parsedData.lastPos * 1000).toISOString() // assuming lastPos is in seconds
}
return await this.parseLocation(result)
}
}

export default Marinetraffic;
export default Marinetraffic

0 comments on commit 69d2731

Please sign in to comment.