1- import httpx
2- import json
3- import csv
41import argparse
5- import os
2+ import csv
3+ import json
64import subprocess
5+ import time
76
7+ import httpx
8+ import pandas
9+
10+ import sheets
811
9- def get_name (user_id : str , curl_args ):
12+
13+ def get_name (user_id : str , curl_args , rec_limit = 10 ):
1014 try :
1115 if type (user_id ) != str or user_id .isdigit ():
1216 status , output = subprocess .getstatusoutput (
@@ -22,10 +26,14 @@ def get_name(user_id: str, curl_args):
2226 user_id = user_id [1 ][4 ][0 ]
2327 return user_id
2428 except httpx .ConnectError :
25- print ('ConnectError' )
29+ if rec_limit == 0 :
30+ print ('ConnectError' )
31+ return ""
32+ time .sleep (0.1 )
33+ return get_name (user_id , curl_args , rec_limit - 1 )
2634 return ""
2735
28- def get_link (user_id : str , curl_args ):
36+ def get_link (user_id : str , curl_args , rec_limit = 10 ):
2937 try :
3038 if type (user_id ) != str or user_id .isdigit ():
3139 status , output = subprocess .getstatusoutput (
@@ -41,7 +49,10 @@ def get_link(user_id: str, curl_args):
4149 user_id = user_id [- 1 ][- 1 ]
4250 return user_id
4351 except httpx .ConnectError :
44- print ('ConnectError' )
52+ if rec_limit == 0 :
53+ print ('ConnectError' )
54+ return ""
55+ time .sleep (0.1 )
4556 return ""
4657
4758def get_id_by_name (user_id : str , curl_args ):
@@ -56,12 +67,12 @@ def get_id_by_name(user_id: str, curl_args):
5667 user_id = user_id [1 ][31 ]
5768 return user_id
5869
59- def get_awards_by_id (user_id : str | int , key : str , curl_args , timeout ) -> dict :
70+ def get_awards_by_id (user_id : str | int , key : str , curl_args , timeout , rec_limit = 10 ) -> dict :
6071 print (f'Processing id { user_id } ' )
6172 try :
6273 if not (type (user_id ) != str or user_id .isdigit ()):
6374 user_id = get_id_by_name (user_id , curl_args )
64-
75+
6576 c = httpx .get (f'https://developerprofiles-pa.clients6.google.com/v1/awards?access_token&locale&obfuscatedProfileId={ user_id } &useBadges=true&key={ key } ' ,
6677 headers = {
6778 'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0' ,
@@ -96,28 +107,31 @@ def get_awards_by_id(user_id: str | int, key: str, curl_args, timeout) -> dict:
96107 }
97108 return award_titles
98109 except httpx .ConnectError :
99- print ('ConnectError' )
100- return {}
110+ if rec_limit == 0 :
111+ print ('ConnectError' )
112+ return {}
113+ time .sleep (0.1 )
114+ return get_awards_by_id (user_id , key , curl_args , timeout , rec_limit - 1 )
101115
102116
103- def get_awards (ids : [str | int ], key : str , curl_args , timeout ) -> dict [set ]:
104- awards = {user_id : get_awards_by_id (user_id , key , curl_args , timeout ) for user_id in ids }
117+ def get_awards (ids : [str | int ], key : str , curl_args , timeout , rec_limit = 10 ) -> dict [set ]:
118+ awards = {user_id : get_awards_by_id (user_id , key , curl_args , timeout , rec_limit ) for user_id in ids }
105119 return awards
106120
107121
108- def write_to_local_csv (awards : dict [set ], curl_args , fname : str = 'result.csv' ) -> None :
122+ def write_to_local_csv (awards : dict [set ], curl_args , fname : str = 'result.csv' , rec_limit = 10 ) -> None :
109123 column_names = set ()
110124 default_columns = [
111- 'id' ,
112- 'name' ,
125+ 'id' ,
126+ 'name' ,
113127 'link' ,
114- 'public_profile' ,
115- 'profile created' ,
128+ 'public_profile' ,
129+ 'profile created' ,
116130 ]
117131
118132 for user_awards in awards .values ():
119133 column_names .update (user_awards )
120- column_names = default_columns + list (column_names )
134+ column_names = default_columns + sorted ( list (column_names ) )
121135 with open (fname , 'w' , newline = '' ) as csvfile :
122136 award_writer = csv .writer (csvfile )
123137 award_writer .writerow (
@@ -127,10 +141,10 @@ def write_to_local_csv(awards: dict[set], curl_args, fname: str = 'result.csv')
127141 for user_awards in awards .items ():
128142 row = [
129143 get_id_by_name (user_awards [0 ], curl_args ),
130- get_name (user_awards [0 ], curl_args ),
131- get_link (user_awards [0 ], curl_args ),
132- 1 if len (user_awards [1 ]) else 0 ,
133- user_awards [1 ].get ('Joined the Google Developer Program' ),
144+ get_name (user_awards [0 ], curl_args , rec_limit ),
145+ get_link (user_awards [0 ], curl_args , rec_limit ),
146+ 1 if len (user_awards [1 ]) else 0 ,
147+ user_awards [1 ].get ('Joined the Google Developer Program' ),
134148 ]
135149 for award_name in column_names [len (default_columns ):]:
136150 row .append (user_awards [1 ][award_name ] if award_name in user_awards [1 ] else 'No' )
@@ -150,10 +164,49 @@ def write_to_local_csv(awards: dict[set], curl_args, fname: str = 'result.csv')
150164 parser .add_argument ('-k' , '--key' )
151165 parser .add_argument ('-c' , '--curl_args' )
152166 parser .add_argument ('-t' , '--timeout' , type = float , default = 1 )
153- args = parser .parse_args ()
167+ parser .add_argument ('-r' , '--repeat' , type = int , default = 10 )
168+
169+ parser .add_argument ('--google_token' , type = str , required = False , help = 'Specify path to google token file' )
170+ parser .add_argument ('--table_id' , type = str , required = False )
171+ parser .add_argument ('--sheet_id' , type = str , required = False )
172+ parser .add_argument ('--input_sheet_id' , type = str , required = False )
173+ parser .add_argument ('--input_column_number' , type = int , required = False )
174+ parser .add_argument ('--input_column_skip' , type = int , required = False , default = 0 )
154175
155- with open (args .ids_file ) as file :
156- lines = [line .rstrip () for line in file ]
176+ parser .add_argument ('--yandex_token' , type = str , required = False )
177+ parser .add_argument ('--yandex_path' , type = str , required = False )
178+
179+ args = parser .parse_args ()
180+ lines = None
181+ if args .ids_file :
182+ with open (args .ids_file ) as file :
183+ lines = [line .rstrip () for line in file ]
157184 # ids = lines
158- q = get_awards (lines , args .key , args .curl_args , args .timeout )
159- write_to_local_csv (q , args .curl_args , args .output )
185+ elif args .google_token and args .table_id and args .input_sheet_id :
186+ lines = sheets .read_ids_from_table (
187+ args .google_token ,
188+ args .table_id ,
189+ args .input_sheet_id ,
190+ args .input_column_number
191+ )
192+ lines = sheets .cut_lines (lines , args .input_column_skip )
193+ else :
194+ print ('set ids file or google table input' )
195+ q = get_awards (lines , args .key , args .curl_args , args .timeout , args .repeat )
196+ write_to_local_csv (q , args .curl_args , args .output , args .repeat )
197+ if args .google_token and args .table_id and args .sheet_id :
198+ sheets .write_data_to_table (
199+ pandas .read_csv (args .output ),
200+ args .google_token ,
201+ args .table_id ,
202+ args .sheet_id
203+ )
204+
205+ if args .yandex_token and args .yandex_path :
206+ import yandex_disk
207+ yandex_disk .DiskManager (
208+ yatoken = args .yandex_token
209+ ).upload (
210+ args .output ,
211+ args .yandex_path
212+ )
0 commit comments