-
Notifications
You must be signed in to change notification settings - Fork 2
/
instagram.py
86 lines (64 loc) · 2.65 KB
/
instagram.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
from selenium import webdriver
import requests, json
from time import gmtime, strftime
def get_json_obj(end_cursor, hashtag):
url = "https://www.instagram.com/graphql/query/"
querystring = {"query_id": "17875800862117404",
"variables": "{\"tag_name\":\""+hashtag+"\",\"first\":8,\"after\":\"" + end_cursor + "\"}"}
headers = {
'cache-control': "no-cache"
}
response = requests.request("GET", url, headers=headers, params=querystring)
json_resp_obj = json.loads(response.text)
return json_resp_obj
def get_first_endcursor(hashtag):
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/explore/tags/'+hashtag+'/')
end_cursor_value = driver.execute_script(
'return window._sharedData[\'entry_data\'][\'TagPage\'][0]["tag"]["media"]["page_info"]["end_cursor"]')
driver.quit()
return end_cursor_value
def get_content(hashtag):
first_encursor = get_first_endcursor(hashtag)
json_resp_obj = get_json_obj(first_encursor, hashtag)
status = json_resp_obj['status']
if status == 'ok':
check_next_page = json_resp_obj['data']['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page']
if check_next_page:
for value in xrange(counter):
next_end_cursor = json_resp_obj['data']['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor']
json_resp_obj = get_json_obj(next_end_cursor, hashtag)
node_length = len(json_resp_obj['data']['hashtag']['edge_hashtag_to_media']['edges'])
for index in range(0, node_length):
list_of_urls.append(
json_resp_obj['data']['hashtag']['edge_hashtag_to_media']['edges'][index]['node'][
'display_url'])
def download_content(url, folder_name):
location = os.getcwd()+"/"+folder_name
local_filename = url.split('/')[-1]
r = requests.get(url, stream=True)
with open(os.path.join(location, local_filename), 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return local_filename
list_of_urls = []
print '''
____ _ _ __ ___ ________
____
_____/ __ \_ __
/ ___/ / / / |/_/
/ / / /_/ /> <
/_/ /_____/_/|_|
____ _ _ __ ___ ________
'''
hashtag = raw_input('Enter Hashtag:')
counter = input('Enter count of content:')
get_content(hashtag)
folder_name = hashtag + "-"+strftime("%Y-%m-%d %H:%M:%S", gmtime())
if not os.path.exists(folder_name):
os.makedirs(folder_name)
for urls in list_of_urls:
download_content(urls, folder_name)
print urls