Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Created July 29, 2024 16:47
Show Gist options
  • Save Xnuvers007/0500a00321bc2b05443e6b0c243a9d7e to your computer and use it in GitHub Desktop.
Save Xnuvers007/0500a00321bc2b05443e6b0c243a9d7e to your computer and use it in GitHub Desktop.
instagram photo downloader
from flask import Flask, request, jsonify
import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import urlparse
class InstagramScraperAPI:
URL_REGEX = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}|[A-Z0-9-]{2,})|' # domain...
r'localhost|' # localhost
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
def __init__(self, app=None):
self.app = app
if app is not None:
self.init_app(app)
def init_app(self, app):
self.app = app
self.register_routes()
def prepend_scheme(self, url):
parsed_url = urlparse(url)
if not parsed_url.scheme:
return 'https://' + url
return url
def is_valid_instagram_url(self, url):
url = self.prepend_scheme(url) # Ensure the URL has the scheme
return url.startswith('https://www.instagram.com/p/') and self.is_valid_url(url)
def is_valid_url(self, url):
return re.match(self.URL_REGEX, url) is not None
def register_routes(self):
@self.app.route('/igp', methods=['GET'])
def igp():
user_input = request.args.get('u')
if not user_input:
return jsonify({"error": "Missing 'u' parameter",
"example": request.host_url + "igp?u=https://www.instagram.com/p/C7gz-Q3tW3J/"}), 400
user_input = self.prepend_scheme(user_input)
if not self.is_valid_instagram_url(user_input):
return jsonify({"error": "Invalid Instagram URL format. Please provide a valid Instagram post URL.",
"example": request.host_url + "igp?u=https://www.instagram.com/p/C7gz-Q3tW3J/"}), 400
headers = {
'accept': 'application/json, text/javascript, */*; q=0.01',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
}
data = {
'q': user_input,
'downloader': 'image',
}
try:
response = requests.post('https://inoffline.net/search/', headers=headers, data=data, timeout=30)
response.raise_for_status()
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 500
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a', href=True)
cleaned_urls = []
for link in links:
img_url = link.get('href')
clean_url = re.sub(r'\\+', '', img_url)
clean_url = clean_url.replace('"', '') + '\n'
cleaned_urls.append(clean_url.strip())
return jsonify({"image_urls": cleaned_urls}), 200
app = Flask(__name__)
InstagramScraperAPI(app)
if __name__ == '__main__':
app.run(debug=True)
import requests
from bs4 import BeautifulSoup
import re
url = 'https://inoffline.net/search/'
cookies = {
'PHPSESSID': 'gg0skvdsjo022p6eo2vbuibers',
}
headers = {
'accept': 'application/json, text/javascript, */*; q=0.01',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'sec-ch-ua': 'Not)A;Brand";v="99", "Brave";v="127", "Chromium";v="127"',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
}
data = {
'q': 'https://www.instagram.com/p/C7gz-Q3tW3J/?igsh=MTM5Z3d5Y202dThzZw==',
'downloader': 'image',
}
response = requests.post(url, cookies=cookies, headers=headers, data=data)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a', href=True)
for link in links:
img_url = link.get('href')
clean_url = re.sub(r'\\+', '', img_url)
clean_url = clean_url.replace('\"', '') + '\n'
print(clean_url)
else:
print(f"Gagal mengambil halaman. Kode status: {response.status_code}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment