Scraping DDG dengan python

from duckduckgo_search import DDGS
from pathlib import Path
from fastprogress.fastprogress import progress_bar
import requests
import uuid
from PIL import Image as PImage

root = Path().cwd()/"jilbabcantik"  #isikan nama folder
keywords = 'jilbab cantik'          #isikan kata kunci
max_results=20

links=[]
with DDGS(proxies="socks5://localhost:9150", timeout=20) as ddgs:
    ddgs_images_gen = ddgs.images(
      keywords,
      safesearch="off",
    )
    for r in ddgs_images_gen:    
        #print(r["image"])
        links.append(r["image"])
        if len(links) >= max_results:
            break


if(len(links) == 0):
    print("Tidak ada yang bisa diunduh!");

uuid_names=True
path = Path(root)
path.mkdir(parents=True, exist_ok=True)

print("Mengunduh hasil ke dalam", path)
pbar = progress_bar(links)
pbar.comment = 'Gambar yang diunduh'

i = 1
mk_uniq = lambda : '_' + str(uuid.uuid4())[:8] if uuid_names else ''
mk_fp = lambda x: path/(str(x).zfill(3) + mk_uniq() + ".jpg")
is_file = lambda x: len(list(path.glob(str(x).zfill(3) + '*.jpg'))) > 0

while is_file(i): i += 1 

results = []

for link in pbar:
      try:
        resp = requests.get(link)
        fp = mk_fp(i)
        fp.write_bytes(resp.content)

        try:
          img = PImage.open(fp)
          img.verify()
          img.close()
          results.append(Path(fp))
        except Exception as e:
          print(e)
          print(fp, " tidak valid")
          fp.unlink()
      except Exception as e:
         print(e)
         print("Terjadi pengecualian saat mengambil ", link)

      i += 1


Mengubah script python menjadi executable

Menggunakan Linux

Gunakan pyinstaller, pasang dengan:

pip install pyinstaller

Jika gagal, maka:

Buka: https://github.com/pyinstaller/pyinstaller/releases
Download: PyInstaller-3.3.1.tar.gz lalu extract dan masuk kedirektori tersebut.
Eksekusi dengan perintah:

sudo python setup.py build 
sudo python setup.py install

Selanjutnya ubah script python ke executable dengan

pyinstaller --onefile <script_phyton>.py

Jika dikatakan bootloader belum tersedia, misalnya untuk Linux ARM 32Bbit, maka harus dibuat dahulu. Pada hasil download PyInstaller ada direktori bootloader. Masuk ke direktori tersebut dan jalankan perintah:

sudo python ./waf all
cd ../PyInstaller/bootloader
cp -R Linux-32bit-arm/ /home/pi/.local/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-32bit-arm/
 

sumber: https://pyinstaller.readthedocs.io/en/stable/bootloader-building.html

Menerima paket UDP dan meneruskan ke URL atau website

Gunakan script phyton ini:

import socket, urllib2

UDP_IP = "127.0.0.1"
UDP_PORT = 50000

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
#sock.bind((UDP_IP, UDP_PORT))
sock.bind(("", UDP_PORT)) #bind, receive from any addresss

while True:
 data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
 print "received message:", data
 stringdata = str(data)
 if stringdata[0] == "+":
 value = stringdata[2]
 karakter = ord(loket)
 karakter = karakter + 1;
 value = chr(karakter)
 url = "http://localhost/getUDP.php?var="+value
 print "url:", url
 response = urllib2.urlopen(url)
 headers = response.info()
 data = response.read()
 print data