├── InstaTracker.py ├── LICENSE └── README.md /InstaTracker.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Instagram UserID to Username (InstaTrack) 3 | Snippet By Snbig 4 | https://github.com/Snbig/InstaTrack/ 5 | ''' 6 | import argparse, hashlib, json, re, requests 7 | 8 | authtokens = tuple() 9 | 10 | def checkTokens(): 11 | if not authtokens: 12 | getTokens() 13 | 14 | 15 | def getTokens(): 16 | r = requests.get('https://instagram.com/', headers={ 17 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0', }).text 18 | rhx_gis = json.loads(re.compile('window._sharedData = ({.*?});', re.DOTALL).search(r).group(1))['nonce'] 19 | 20 | ppc = re.search(r'ConsumerLibCommons.js/(.*?).js', r).group(1) 21 | r = requests.get('https://www.instagram.com/static/bundles/metro/ConsumerLibCommons.js/' + ppc + '.js').text 22 | query_hash = re.findall(r'{value:!0}\);(?:var|const|let) .=\"([0-9a-f]{32})\"', r)[1] 23 | 24 | global authtokens 25 | authtokens = tuple((rhx_gis, query_hash)) 26 | 27 | 28 | def const_gis(query): 29 | checkTokens() 30 | t = authtokens[0] + ':' + query 31 | x_instagram_gis = hashlib.md5(t.encode("utf-8")).hexdigest() 32 | return x_instagram_gis 33 | 34 | 35 | def usernameToUserId(user): 36 | r = requests.get('https://www.instagram.com/web/search/topsearch/?query=' + user, headers={ 37 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'}).text 38 | 39 | if json.loads(r).get("message") == 'rate limited': 40 | print( 41 | '[x] Rate limit reached!\n[#] Unchecked Username: {}\n[!] Try again in a few minutes.\n'.format(user)) 42 | exit() 43 | 44 | try: 45 | for i in range(len(json.loads(r)['users'])): 46 | if json.loads(r)['users'][i]['user']['username'] == user: 47 | return json.loads(r)['users'][i]['user']['pk'] 48 | except IndexError: 49 | return False 50 | 51 | 52 | def useridToUsername(userid): 53 | header = { 54 | 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 Instagram 12.0.0.16.90 (iPhone9,4; iOS 10_3_3; en_US; en-US; scale=2.61; gamut=wide; 1080x1920)', 55 | 'X-Requested-With': 'XMLHttpRequest'} 56 | r = requests.get( 57 | f'https://i.instagram.com/api/v1/users/{userid}/info/', 58 | headers=header) 59 | 60 | if r.status_code == 404: 61 | return False 62 | 63 | j = json.loads(r.text) 64 | 65 | if j.get("status") != 'ok': 66 | print('[x] Rate limit reached!\n[#] Unchecked ID: {}\n[!] Try again in a few minutes..\n'.format(userid)) 67 | exit() 68 | try: 69 | return j['user']['username'] 70 | except IndexError: 71 | return False 72 | 73 | 74 | def main(): 75 | parser = argparse.ArgumentParser(prog='InstaTracker.py') 76 | 77 | parser.add_argument('-u', '--user', action='store', dest='username', 78 | help='Set Instagram username', type=str) 79 | parser.add_argument('-i', '--id', action='store', dest='id', 80 | help='Set Instagram userID', type=int) 81 | parser.add_argument('-f', '--list', action='store', dest='file', 82 | help='Import username/userID per line as .txt file', 83 | type=str) 84 | args = parser.parse_args() 85 | 86 | if args.file is not None: 87 | result = list() 88 | 89 | try: 90 | with open(args.file, 'r') as file: 91 | elements = file.readlines() 92 | except FileNotFoundError: 93 | print('[-] File Not Found :(') 94 | return 0 95 | 96 | print("Processing...\n") 97 | with open('result.txt', 'w') as file: 98 | for e in elements: 99 | e = e.strip() 100 | if e.isdigit(): 101 | username = useridToUsername(e) 102 | if username: 103 | result.append('{}:{}'.format(e, username)) 104 | file.write('{}:{}\n'.format(e, username)) 105 | else: 106 | print('[-] "{}" Not Found!\n'.format(e)) 107 | else: 108 | userid = usernameToUserId(e) 109 | if userid: 110 | result.append('{}:{}'.format(userid, e)) 111 | file.write('{}:{}\n'.format(userid, e)) 112 | else: 113 | print('[-] "{}" Not Found!\n'.format(e)) 114 | 115 | print('[++] Result saved as result.txt') 116 | return 0 117 | 118 | if args.id is not None: 119 | username = useridToUsername(args.id) 120 | if not username: 121 | print('[-] UserID does not exist') 122 | else: 123 | print('[+] Username: {}'.format(username)) 124 | 125 | if args.username is not None: 126 | userid = usernameToUserId(args.username) 127 | if not userid: 128 | print('[-] Username does not exist') 129 | else: 130 | print('[+] UserID: {}'.format(userid)) 131 | if args.id is None and args.username is None: 132 | parser.print_help() 133 | 134 | 135 | if __name__ == '__main__': 136 | main() 137 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Hamed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # InstaTrack 2 | Convert Instagram user ID to username & vice versa 3 | ## Author 4 | * **Hamed** - [Github](https://github.com/Snbig) 5 | 6 | ## Changelog 7 | 8 | - **2020-07-05** - Query_hash page changed by Instagram. Code updated.(see also [#15](/../../issues/15) 9 | - **2020-03-03** - The Issue with resolving some special usernames to user ids fixed. (see also [#11](/../../issues/11)) 10 | - **2019-11-23** - 'query_hash' parameter regex updated duo to instagram API parameter change. (see also [#8](/../../issues/8)) | Tnx to [@Vukkk](https://github.com/Vukkk) 11 | - **2019-06-26** - List lookup added to code (see also [#3](/../../issues/3)) 12 | - **2019-06-01** - Command line options added to code (see also [#2](/../../issues/2)) 13 | - **2019-05-30** - Code patched duo to instagram API parameter change. 14 | 15 | ## License 16 | 17 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details 18 | --------------------------------------------------------------------------------