create a bash script that basically tries to find all people who added imgur links to OSM.
to do this you need to download the info from taginfo on all uses of imgur:
download that and any subsequent pages until it returns empy.
Those are all instances of imgur being used on osm, on ways/nodes/relations
the output of that will look like:
{"url":,"data_until":"<DATE_TIME>,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},{"key":"image","value":,"count_all":1},
and so on
you then want to extract the actual objects that use those values:
overpass_query() {
local imgur_url="1"
echo "[out:xml][timeout:25];
(
<REDACTED>"];
way[\"image\"=\"imgur_url"];
relation["image"="$imgur_url"];
);
out meta;"
}
Function to query
query_overpass() {
local imgur_url="1"localquery=(overpass_query "imgur_url")
local url=<URL>
local outfile="overpass_results/overpass_result_(echo "imgur_url" | md5sum | cut -d' ' -f1).xml"
if [ ! -f "outfile" ]; then
echo "Querying Overpass API for imgur URL: imgquery" "outfile"
sleep 1
fi
}
Function to parse Overpass result
parse_overpass_result() {
local overpass_result_file="overpass_result_file"
}
Function to get history of an object
get_history() {
local type="1"localid="2"
local history_file="histories/history_{type}_{id}.xml"
if [ ! -f "{type} url" -o "$history_file"
sleep 1
fi
}
Function to find who added the imgur URL
find_imgur_adder() {
local type="1"localid="2"
local imgur_url="3"
local history_file="histories/history_{type}_${id}.xml"
code
local previous_has_imgur=0
local versions=$(xmlstarlet sel -t -m "/osm/${type}" -v '@version' -n "$history_file")
for version in $versions; do
local imgur_in_version=$(xmlstarlet sel -t -m "/osm/${type}[@version='$version']/tag[@k='image' and @v='$imgur_url']" -v '@v' -n "$history_file")
if [ -n "$imgur_in_version" ] && [ "$previous_has_imgur" -eq 0 ]; then
# imgur URL was added in this version
local user=$(xmlstarlet sel -t -m "/osm/${type}[@version='$version']" -v '@user' "$history_file")
echo "$imgur_url,$type,$id,$version,$user" >> results.csv
return
fi
if [ -n "$imgur_in_version" ]; then
previous_has_imgur=1
else
previous_has_imgur=0
fi
done
}
Main processing loop
while IFS= read -r imgur_url; do
echo "Processing imgur URL: $imgur_url"
code
# Query Overpass API
overpass_result_file="overpass_results/overpass_result_$(echo "$imgur_url" | md5sum | cut -d' ' -f1).xml"
query_overpass "$imgur_url"
# Parse Overpass result
parse_overpass_result "$overpass_result_file" > "objects_$(echo "$imgur_url" | md5sum | cut -d' ' -f1).txt"
# For each object, get history and find who added the imgur URL
while IFS=, read -r type id version user; do
get_history "$type" "$id"
find_imgur_adder "$type" "$id" "$imgur_url"
done < "objects_$(echo "$imgur_url" | md5sum | cut -d' ' -f1).txt"
done < imgur_urls.txt
Explanation:
Step 1:Download all imgur URLs from the taginfo API, paging through results until no more data is returned.
Step 2:Extract the imgur URLs and save them .
Step 3:For each imgur URL:Query the Overpass API to find all OSM objects (nodes, ways, relations) that have the imgur URL in their tags.Save the results to an XML file.
Query the Overpass API to find all OSM objects (nodes, ways, relations) that have the imgur URL in their tags.
Save the results to an XML file.
Step 4:For each object found:Download its full history from the OSM API.Parse the history to find the version where the imgur URL was first added.Record the user who added it along with the imgur URL and object details.
Download its full history from the OSM API.
Parse the history to find the version where the imgur URL was first added.
Record the user who added it along with the imgur URL and object details.
Step 5:Compile the results intoresults.csv.
Step 1:Download all imgur URLs from the taginfo API, paging through results until no more data is returned.
Step 2:Extract the imgur URLs and save them .
Step 3:For each imgur URL:
Query the Overpass API to find all OSM objects (nodes, ways, relations) that have the imgur URL in their tags.
Save the results to an XML file.
Query the Overpass API to find all OSM objects (nodes, ways, relations) that have the imgur URL in their tags.
Save the results to an XML file.
Step 4:For each object found:
Download its full history from the OSM API.
Parse the history to find the version where the imgur URL was first added.
Record the user who added it along with the imgur URL and object details.
Download its full history from the OSM API.
Parse the history to find the version where the imgur URL was first added.
Record the user who added it along with the imgur URL and object details.
Step 5:Compile the results intoresults.csv.
Notes:
The script usesjqfor JSON parsing,xmlstarletfor XML parsing, andcurlfor HTTP requests.
To prevent overwhelming the OSM API, the script includessleepcommands to space out requests.
Results are saved incrementally, so you can resume the script if interrupted.
Make sure to havejqandxmlstarletinstalled on your system.
The script usesjqfor JSON parsing,xmlstarletfor XML parsing, andcurlfor HTTP requests.
To prevent overwhelming the OSM API, the script includessleepcommands to space out requests.
Results are saved incrementally, so you can resume the script if interrupted.
Make sure to havejqandxmlstarletinstalled on your system.
Output:
The finalresults.csvwill contain lines in the format:
This CSV lists each imgur URL found in OSM and the user who added it.
Certainly! Here's an updated bash script that includes error handling, delays to prevent API overload, the ability to resume after failure, immediate writing to the results file, and informative command-line outputs.
command -v jq >/dev/null 2>&1 || { echo >&2 "jq is required but not installed. Aborting."; exit 1; }
command -v xmlstarlet >/dev/null 2>&1 || { echo >&2 "xmlstarlet is required but not installed. Aborting."; exit 1; }
Create directories to store data
mkdir -p taginfo_pages overpass_results histories
Initialize results file if it doesn't exist
results_file="results.csv"
if [ ! -f "resultsfile"];thenecho"imgururl,objecttype,objectid,version,user">"results_file"
fi
Step 1: Download all uses of imgur from taginfo
echo "Starting to fetch imgur URLs from taginfo..."
page=1
while true; do
url=
output_file="taginfo_pages/page_$page.json"
code
if [ -f "$output_file" ]; then
echo "Taginfo page $page already downloaded. Skipping."
else
echo "Fetching taginfo page $page..."
if ! curl -s "$url" -o "$output_file"; then
echo "Error fetching taginfo page $page. Exiting."
exit 1
fi
# Check for empty data
data_length=$(jq '.data | length' "$output_file")
if [ "$data_length" -eq 0 ]; then
echo "No more data on taginfo page $page. Stopping pagination."
rm "$output_file" # Remove empty page file
break
fi
sleep 1 # Delay to prevent overloading the API
fi
page=$((page + 1))
done
Combine all data into one file
combined_data_file="all_imgur_data.json"
if [ -f "combined_data_file"; then
echo "Error combining data files. Exiting."
exit 1
fi
fi
Extract imgur URLs
imgur_urls_file="imgur_urls.txt"
if [ -f "combined_data_file" > "$imgur_urls_file"; then
echo "Error extracting imgur URLs. Exiting."
exit 1
fi
fi
Function to create Overpass QL query
overpass_query() {
local imgur_url="1"
echo "[out:xml][timeout:25];
(
node[\"image\"=\"imgur_url"];
way["image"="imgur_url\"];
relation[\"image\"=\"imgur_url"];
);
out meta;"
}
Function to query
query_overpass() {
local imgur_url="1"localhash=(echo -n "imgur_url" | md5sum | cut -d' ' -f1)
local outfile="overpass_results/overpass_result_hash.xml"
code
if [ -f "$outfile" ]; then
echo "Overpass result for imgur URL '$imgur_url' already exists. Skipping."
else
echo "Querying Overpass API for imgur URL: $imgur_url"
local query
query=$(overpass_query "$imgur_url")
local url=<URL>
if ! curl -s -X POST -d "data=$query" "$url" -o "$outfile"; then
echo "Error querying Overpass API for imgur URL: $imgur_url. Skipping."
return 1
fi
sleep 1 # Delay to prevent overloading the API
fi
}
Function to parse Overpass result
parse_overpass_result() {
local overpass_result_file="overpass_result_file"; then
echo "Error parsing Overpass result file: $overpass_result_file"
return 1
fi
}
Function to get history of an object
get_history() {
local type="1"localid="2"
local history_file="histories/history_{type}_{id}.xml"
code
if [ -f "$history_file" ]; then
echo "History for ${type} ${id} already exists. Skipping."
else
local url=<URL>
echo "Fetching history for ${type} ${id}..."
if ! curl -s "$url" -o "$history_file"; then
echo "Error fetching history for ${type} ${id}. Skipping."
return 1
fi
sleep 1 # Delay to prevent overloading the API
fi
}
Function to find who added the imgur URL
find_imgur_adder() {
local type="1"localid="2"
local imgur_url="3"
local history_file="histories/history_{type}_${id}.xml"
code
if [ ! -f "$history_file" ]; then
echo "History file $history_file does not exist. Cannot find who added the imgur URL."
return 1
fi
local versions
versions=$(xmlstarlet sel -t -m "/osm/${type}" -v '@version' -n "$history_file" | sort -n)
local previous_has_imgur=0
for version in $versions; do
local imgur_in_version
imgur_in_version=$(xmlstarlet sel -t -m "/osm/${type}[@version='$version']/tag[@k='image']" -v '@v' -n "$history_file" | grep -F "$imgur_url")
if [ -n "$imgur_in_version" ] && [ "$previous_has_imgur" -eq 0 ]; then
# imgur URL was added in this version
local user
user=$(xmlstarlet sel -t -m "/osm/${type}[@version='$version']" -v '@user' "$history_file")
echo "Imgur URL '$imgur_url' added to ${type} ${id} in version ${version} by user '${user}'."
echo "\"$imgur_url\",$type,$id,$version,\"$user\"" >> "$results_file"
return 0
fi
if [ -n "$imgur_in_version" ]; then
previous_has_imgur=1
else
previous_has_imgur=0
fi
done
echo "Could not find who added imgur URL '$imgur_url' to ${type} ${id}."
return 1
}
Main processing loop
echo "Starting main processing loop..."
total_urls=(wc−l<"imgur_urls_file")
current_url=0
while IFS= read -r imgur_url; do
current_url=((currenturl+1))echo"Pcurrent_url/imgur_url"
code
# Query Overpass API
hash=$(echo -n "$imgur_url" | md5sum | cut -d' ' -f1)
overpass_result_file="overpass_results/overpass_result_$hash.xml"
if ! query_overpass "$imgur_url"; then
echo "Skipping imgur URL due to Overpass API error: $imgur_url"
continue
fi
# Parse Overpass result
objects_file="objects_$hash.txt"
if [ -f "$objects_file" ]; then
echo "Objects file for imgur URL '$imgur_url' already exists. Skipping parsing."
else
echo "Parsing Overpass result for imgur URL: $imgur_url"
if ! parse_overpass_result "$overpass_result_file" > "$objects_file"; then
echo "Error parsing Overpass result. Skipping imgur URL: $imgur_url"
continue
fi
fi
# For each object, get history and find who added the imgur URL
while IFS=, read -r type id version user; do
# Check if we already have this result to prevent duplicates
if grep -q "^\"$imgur_url\",$type,$id," "$results_file"; then
echo "Result for $type $id with imgur URL '$imgur_url' already in results. Skipping."
continue
fi
if ! get_history "$type" "$id"; then
continue
fi
if ! find_imgur_adder "$type" "$id" "$imgur_url"; then
continue
fi
done < "$objects_file"
done < "$imgur_urls_file"
echo "Processing complete. Results saved to $results_file."
Explanation of Changes:
Error Handling:Added checks after each critical operation (e.g., downloading files, parsing data).If an error occurs, the script reports it and either continues or exits gracefully.Checks if files exist before attempting to read them.
Added checks after each critical operation (e.g., downloading files, parsing data).
If an error occurs, the script reports it and either continues or exits gracefully.
Checks if files exist before attempting to read them.
Delays:Addedsleep 1after each API request to avoid overloading APIs.
Addedsleep 1after each API request to avoid overloading APIs.
Resumability:Before performing actions, the script checks if the output files already exist.If they do, it skips that step, allowing the script to resume where it left off.Results are appended toresults.csvimmediately, and the script checks for duplicates to prevent reprocessing.
Before performing actions, the script checks if the output files already exist.
If they do, it skips that step, allowing the script to resume where it left off.
Results are appended toresults.csvimmediately, and the script checks for duplicates to prevent reprocessing.
Command-Line Outputs:Addedechostatements to provide informative messages about the script's progress.Outputs include what the script is currently processing and any actions taken or skipped.
Addedechostatements to provide informative messages about the script's progress.
Outputs include what the script is currently processing and any actions taken or skipped.
Immediate Writing to File:Results are appended toresults.csvimmediately after being found.This ensures that if the script stops unexpectedly, the collected data is not lost.
Results are appended toresults.csvimmediately after being found.
This ensures that if the script stops unexpectedly, the collected data is not lost.
Preventing Duplicates:Before adding a new line toresults.csv, the script checks if the result already exists.
Before adding a new line toresults.csv, the script checks if the result already exists.
Quoting Fields in CSV:Fields that may contain commas or special characters are enclosed in double quotes to ensure CSV integrity.
Fields that may contain commas or special characters are enclosed in double quotes to ensure CSV integrity.
Tool Checks:At the beginning of the script, it checks ifjqandxmlstarletare installed.
At the beginning of the script, it checks ifjqandxmlstarletare installed.
Error Handling:
Added checks after each critical operation (e.g., downloading files, parsing data).
If an error occurs, the script reports it and either continues or exits gracefully.
Checks if files exist before attempting to read them.
Added checks after each critical operation (e.g., downloading files, parsing data).
If an error occurs, the script reports it and either continues or exits gracefully.
Checks if files exist before attempting to read them.
Delays:
Addedsleep 1after each API request to avoid overloading APIs.
Addedsleep 1after each API request to avoid overloading APIs.
Resumability:
Before performing actions, the script checks if the output files already exist.
If they do, it skips that step, allowing the script to resume where it left off.
Results are appended toresults.csvimmediately, and the script checks for duplicates to prevent reprocessing.
Before performing actions, the script checks if the output files already exist.
If they do, it skips that step, allowing the script to resume where it left off.
Results are appended toresults.csvimmediately, and the script checks for duplicates to prevent reprocessing.
Command-Line Outputs:
Addedechostatements to provide informative messages about the script's progress.
Outputs include what the script is currently processing and any actions taken or skipped.
Addedechostatements to provide informative messages about the script's progress.
Outputs include what the script is currently processing and any actions taken or skipped.
Immediate Writing to File:
Results are appended toresults.csvimmediately after being found.
This ensures that if the script stops unexpectedly, the collected data is not lost.
Results are appended toresults.csvimmediately after being found.
This ensures that if the script stops unexpectedly, the collected data is not lost.
Preventing Duplicates:
Before adding a new line toresults.csv, the script checks if the result already exists.
Before adding a new line toresults.csv, the script checks if the result already exists.
Quoting Fields in CSV:
Fields that may contain commas or special characters are enclosed in double quotes to ensure CSV integrity.
Fields that may contain commas or special characters are enclosed in double quotes to ensure CSV integrity.
Tool Checks:
At the beginning of the script, it checks ifjqandxmlstarletare installed.
At the beginning of the script, it checks ifjqandxmlstarletare installed.
Usage Instructions:
Save the script to a file, e.g.,find_imgur_users.sh.
Make the script executable:bashCopychmod+x find_imgur_users.sh
Run the script:bashCopy./find_imgur_users.sh
The script will output progress messages to the console.
If the script is interrupted, you can rerun it, and it will pick up where it left off.
The results will be saved incrementally toresults.csv.
Save the script to a file, e.g.,find_imgur_users.sh.
The script will output progress messages to the console.
If the script is interrupted, you can rerun it, and it will pick up where it left off.
The results will be saved incrementally toresults.csv.
Requirements:
Dependencies:jqfor JSON parsing.xmlstarletfor XML parsing.curlfor HTTP requests.
jqfor JSON parsing.
xmlstarletfor XML parsing.
curlfor HTTP requests.
API Considerations:The script includes delays to prevent overloading the APIs.Be mindful of the usage policies of the Overpass API and the OSM API.If you encounter rate limits, you may need to increase the delay durations.
The script includes delays to prevent overloading the APIs.
Be mindful of the usage policies of the Overpass API and the OSM API.
If you encounter rate limits, you may need to increase the delay durations.
Dependencies:
jqfor JSON parsing.
xmlstarletfor XML parsing.
curlfor HTTP requests.
jqfor JSON parsing.
xmlstarletfor XML parsing.
curlfor HTTP requests.
API Considerations:
The script includes delays to prevent overloading the APIs.
Be mindful of the usage policies of the Overpass API and the OSM API.
If you encounter rate limits, you may need to increase the delay durations.
The script includes delays to prevent overloading the APIs.
Be mindful of the usage policies of the Overpass API and the OSM API.
If you encounter rate limits, you may need to increase the delay durations.
plaintextCopyStarting to fetch imgur URLs from taginfo...
Fetching taginfo page 1...
Fetching taginfo page 2...
No more data on taginfo page 2. Stopping pagination.
Combining taginfo pages into one file...
Extracting imgur URLs...
Starting main processing loop...
Processing imgur URL (1/50):
Querying Overpass API for imgur URL:
Parsing Overpass result for imgur URL:
Fetching history for node 123456789...
Imgur URL added to node 123456789 in version 3 by user 'ExampleUser'.
Processing imgur URL (2/50):
Overpass result for imgur URL already exists. Skipping.
Objects file for imgur URL already exists. Skipping parsing.
Result for node with imgur URL already in results. Skipping.
...
Processing complete. Results saved to results.csv.
Important Notes:
Respect OSM Policies:Ensure your use complies with OpenStreetMap's API usage policies.Consider adding longer delays if necessary.
Ensure your use complies with OpenStreetMap's API usage policies.
Consider adding longer delays if necessary.
Data Privacy:Be cautious with user data. Use it responsibly and in accordance with OSM's terms of use.
Be cautious with user data. Use it responsibly and in accordance with OSM's terms of use.
Script Limitations:The script assumes that is used for imgur URLs.If imgur URLs are stored in other tags, the script would need to be adjusted.
The script assumes that is used for imgur URLs.
If imgur URLs are stored in other tags, the script would need to be adjusted.
Respect OSM Policies:
Ensure your use complies with OpenStreetMap's API usage policies.
Consider adding longer delays if necessary.
Ensure your use complies with OpenStreetMap's API usage policies.
Consider adding longer delays if necessary.
Data Privacy:
Be cautious with user data. Use it responsibly and in accordance with OSM's terms of use.
Be cautious with user data. Use it responsibly and in accordance with OSM's terms of use.
Script Limitations:
The script assumes that is used for imgur URLs.
If imgur URLs are stored in other tags, the script would need to be adjusted.
The script assumes that is used for imgur URLs.
If imgur URLs are stored in other tags, the script would need to be adjusted.