Skip to content

Commit 71fcf57

Browse files
committed
Store user-agents.json.gz data as Clixml file
- removed lazy loading
1 parent ef42942 commit 71fcf57

10 files changed

+93
-61
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Fake-UserAgent/data/user-agents.json
21
Fake-UserAgent/data/user-agents.dat
2+
temp/

Fake-UserAgent/Fake-UserAgent.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
@{
33
RootModule = 'Fake-UserAgent.psm1'
4-
ModuleVersion = '0.1.0'
4+
ModuleVersion = '0.2.0'
55
GUID = 'ef6d1c27-66ac-4f6c-8cc6-2bbae0f24543'
66
Author = 'Alan Plocieniak'
77
CompanyName = 'Alan Plocieniak'
8-
Copyright = '(c) 2025 Alan Plocieniak. All rights reserved.'
8+
Copyright = '(c) Alan Plocieniak. All rights reserved.'
99
Description = 'PowerShell module for generating random User Agents.'
1010
PowerShellVersion = '5.0'
1111
FunctionsToExport = '*'

Fake-UserAgent/Private/Initialize-Dataset.ps1

Lines changed: 0 additions & 39 deletions
This file was deleted.
-203 KB
Binary file not shown.

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Fake-UserAgent
2+
[![PowerShell Gallery](https://img.shields.io/powershellgallery/v/Fake-UserAgent?label=Fake-UserAgent&color=blue&logo=data:image/svg%2bxml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciICB2aWV3Qm94PSIwIDAgNDggNDgiIHdpZHRoPSI0OHB4IiBoZWlnaHQ9IjQ4cHgiPjxwYXRoIGZpbGw9IiMwMjc3YmQiIGQ9Ik0xOS44NDcsNDEuOTU2Yy01LjYyOS0wLjAwMi0xMS4yNTksMC4wMjQtMTYuODg4LTAuMDEzYy0yLjg1NS0wLjAxOS0zLjM3NC0wLjctMi43MzEtMy41MjUgY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5LjEyNSAzNyAyNSAzNyAyMS43NSAzN3oiLz48L3N2Zz4=)](https://www.powershellgallery.com/packages/Fake-UserAgent)
23

34
PowerShell module for generating random **User Agents**.
45

@@ -26,8 +27,14 @@ platform : iPhone
2627

2728
## How it works
2829

29-
- module comes with `user-agents.json.gz` downloaded from [**user-agents**](https://github.com/intoli/user-agents/)
30-
- during first cmdlet call data will be unpacked and converted into **user-agents.dat** with help of [**uap-csharp**](https://github.com/ua-parser/uap-csharp/)
30+
Module comes with bundled `user-agents.dat` file which contains different User Agent entries.
31+
32+
The process of creating it:
33+
- Download the version of **user-agents.json** from [**user-agents**](https://github.com/intoli/user-agents/)
34+
- Preprocess using [**uap-csharp**](https://github.com/ua-parser/uap-csharp/)
35+
- Serialize into an XML-based representation (Common Language Infrastructure (CLI)) of all the objects
36+
- Optimize for smaller size
37+
- Store as a file `user-agents.dat`
3138

3239
## Acknowledgements
3340
This project makes use of the following project

Tests/Toggl.API.Tests.ps1

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,10 @@ Describe 'Fake-UserAgent.Tests' {
55
Context "Get-UserAgentData Tests" {
66

77
BeforeAll {
8-
$gzPath = '.\Fake-UserAgent\data\user-agents.json.gz'
9-
$jsonPath = '.\Fake-UserAgent\data\user-agents.json'
108
$datPath = '.\Fake-UserAgent\data\user-agents.dat'
11-
12-
Remove-Item -Path $jsonPath -ErrorAction SilentlyContinue
13-
Remove-Item -Path $datPath -ErrorAction SilentlyContinue
14-
}
15-
16-
It "should have initial state" {
17-
Test-Path $gzPath | Should -Be $true
18-
Test-Path $jsonPath | Should -Be $false
19-
Test-Path $datPath | Should -Be $false
20-
}
21-
22-
It "should not throw exception during initialization" {
23-
{ Get-UserAgent } | Should -Not -Throw
249
}
2510

2611
It "should have initialized state" {
27-
Test-Path $gzPath | Should -Be $true
28-
Test-Path $jsonPath | Should -Be $false
2912
Test-Path $datPath | Should -Be $true
3013
}
3114

@@ -49,4 +32,24 @@ Describe 'Fake-UserAgent.Tests' {
4932
$agent.platform | Should -Be "iPhone"
5033
}
5134
}
35+
36+
Context "UA file update Tests" {
37+
38+
BeforeAll {
39+
$datPath = '.\Fake-UserAgent\data\user-agents.dat'
40+
Remove-Item -Path $datPath -ErrorAction SilentlyContinue
41+
}
42+
43+
It "should have initial state" {
44+
Test-Path $datPath | Should -Be $false
45+
}
46+
47+
It "should download and update user agents" {
48+
{ .\update-data.ps1 } | Should -Not -Throw
49+
}
50+
51+
It "should have initialized state" {
52+
Test-Path $datPath | Should -Be $true
53+
}
54+
}
5255
}

scripts/Initialize-Dataset.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Initialize-Dataset {
2+
param (
3+
[string]$uaJsonPath = "user-agents.json"
4+
)
5+
Process {
6+
7+
Get-Content $uaJsonPath -Raw | ConvertFrom-Json | % {
8+
$ua_result = $uaParser.Parse($_.userAgent)
9+
[PSCustomObject]@{
10+
ua = $_.userAgent
11+
percent = [decimal]($_.weight * 100)
12+
type = $_.deviceCategory
13+
device_brand = $ua_result.Device.Brand
14+
browser = $ua_result.UserAgent.Family
15+
browser_version = [version]::new($ua_result.UserAgent.Major, $ua_result.UserAgent.Minor, $ua_result.UserAgent.Patch, $ua_result.UserAgent.PatchMinor)
16+
browser_version_major_minor = [version]::new($ua_result.UserAgent.Major, $ua_result.UserAgent.Minor)
17+
os = $ua_result.OS.Family
18+
os_version = [version]::new($ua_result.OS.Major, $ua_result.OS.Minor, $ua_result.OS.Patch, $ua_result.OS.PatchMinor)
19+
platform = $_.platform
20+
}
21+
}
22+
}
23+
}
File renamed without changes.

update-data.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Write-Host 'Updating user-agent data...'
2+
$uaJsonUri = "https://raw.githubusercontent.com/intoli/user-agents/main/src/user-agents.json.gz"
3+
4+
Write-Host 'Importing required modules...'
5+
. .\scripts\ConvertFrom-Gzip.ps1
6+
. .\scripts\Initialize-Dataset.ps1
7+
Add-Type -Path .\tools\UAParser.dll
8+
9+
Write-Host 'Cleaning up temp directory...'
10+
$temp = "temp"
11+
if (Test-Path $temp) {
12+
Remove-Item -Path $temp -Recurse -Force
13+
}
14+
New-Item -ItemType Directory -Path $temp | Out-Null
15+
Push-Location -Path $temp
16+
17+
18+
Write-Host 'Downloading user-agents.json...'
19+
Invoke-WebRequest -Uri $uaJsonUri -OutFile "user-agents.json.gz"
20+
21+
Write-Host 'Decompressing user-agents.json...'
22+
ConvertFrom-Gzip -InputObject (Get-Item "user-agents.json.gz") -RemoveInputFile
23+
24+
Write-Host 'Processing user-agents.json...'
25+
$processed = Initialize-Dataset -uaJsonPath "user-agents.json"
26+
27+
Pop-Location
28+
29+
Write-Host 'Exporting user-agents.dat...'
30+
Push-Location .\Fake-UserAgent\data
31+
$processed | Export-Clixml -Path user-agents.clixml -NoClobber
32+
33+
Write-Host 'Optimizing file size...'
34+
Get-Content .\user-agents.clixml -Raw | ForEach-Object { $_ -replace '\s+(?=<)', '' } | Set-Content .\user-agents.dat
35+
Remove-Item .\user-agents.clixml
36+
Pop-Location
37+
38+
Write-Host 'Done!'

0 commit comments

Comments
 (0)