-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr.py
executable file
·47 lines (39 loc) · 1.17 KB
/
flickr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/local/bin/python3
import requests
import sys
import webbrowser
import os
import shutil
from bs4 import BeautifulSoup
headers = {'User-Agent': 'masterbyte'}
session = requests.Session()
if len(sys.argv[1:]) > 0:
search_term = ' '.join(sys.argv[1:])
else:
search_term = input("Enter the search term:\n> ")
try:
os.mkdir(search_term.replace(" ","_"))
except OSError:
pass
def view(path):
if sys.platform.startswith("linux"):
os.system("xdg-open "+path)
elif sys.platform == "darwin":
os.system("open "+path)
else:
webbrowser.open(path)
r = session.get(f"https://api.flickr.com/services/feeds/photos_public.gne?tags={search_term}",headers=headers)
s = BeautifulSoup(r.text,'lxml')
links = s.findAll("link",{"rel":"enclosure","type":"image/jpeg"})
try:
for link in links[:5]:
fname = os.path.join(search_term.replace(" ","_"), f"img{links.index(link)}")
url = link.get('href')
img = session.get(url, headers=headers)
open(fname,"wb+").write(img.content)
view(fname)
input("Continue? ('Enter')\n")
except:
sys.exit("Bye!")
finally:
shutil.rmtree(search_term.replace(" ", "_"))